summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Util/FeatureStatus.php
blob: 94bf6d476b035f7516b3a617a42d6087e6862ee3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php

/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Util;

use ArrayObject;
use Icinga\Module\Icingadb\Command\Object\ToggleObjectFeatureCommand;

class FeatureStatus extends ArrayObject
{
    public function __construct(string $type, $summary)
    {
        $prefix = "{$type}s";

        $featureStatus = [
            ToggleObjectFeatureCommand::FEATURE_ACTIVE_CHECKS =>
                $this->getFeatureStatus('active_checks_enabled', $prefix, $summary),
            ToggleObjectFeatureCommand::FEATURE_PASSIVE_CHECKS =>
                $this->getFeatureStatus('passive_checks_enabled', $prefix, $summary),
            ToggleObjectFeatureCommand::FEATURE_NOTIFICATIONS =>
                $this->getFeatureStatus('notifications_enabled', $prefix, $summary),
            ToggleObjectFeatureCommand::FEATURE_EVENT_HANDLER =>
                $this->getFeatureStatus('event_handler_enabled', $prefix, $summary),
            ToggleObjectFeatureCommand::FEATURE_FLAP_DETECTION =>
                $this->getFeatureStatus('flapping_enabled', $prefix, $summary)
        ];

        parent::__construct($featureStatus, ArrayObject::ARRAY_AS_PROPS);
    }

    protected function getFeatureStatus(string $feature, string $prefix, $summary): int
    {
        $key = "{$prefix}_{$feature}";
        $value = (int) $summary->$key;

        if ($value === 0) {
            return 0;
        }

        $totalKey = "{$prefix}_total";
        $total = (int) $summary->$totalKey;

        if ($value === $total) {
            return 1;
        }

        return 2;
    }
}