File: HostServiceTermValidator.php

package info (click to toggle)
icingaweb2-module-businessprocess 2.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,720 kB
  • sloc: php: 10,971; javascript: 2,019; sh: 115; xml: 49; makefile: 18
file content (97 lines) | stat: -rw-r--r-- 2,973 bytes parent folder | download | duplicates (2)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

namespace Icinga\Module\Businessprocess\Web\Form\Validator;

use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\ServiceNode;
use Icinga\Module\Businessprocess\State\IcingaDbState;
use Icinga\Module\Businessprocess\State\MonitoringState;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use ipl\I18n\Translation;
use ipl\Validator\BaseValidator;
use ipl\Web\FormElement\TermInput\Term;
use LogicException;

class HostServiceTermValidator extends BaseValidator
{
    use Translation;

    /** @var ?BpNode */
    protected $parent;

    /**
     * Set the affected process
     *
     * @param BpNode $parent
     *
     * @return $this
     */
    public function setParent(BpNode $parent): self
    {
        $this->parent = $parent;

        return $this;
    }

    public function isValid($terms)
    {
        if ($this->parent === null) {
            throw new LogicException('Missing parent process. Cannot validate terms.');
        }

        if (! is_array($terms)) {
            $terms = [$terms];
        }

        $isValid = true;
        $testConfig = (new BpConfig())
            ->setBackend($this->parent->getBpConfig()->getBackend());

        foreach ($terms as $term) {
            /** @var Term $term */
            [$hostName, $serviceName] = BpConfig::splitNodeName($term->getSearchValue());
            if ($serviceName !== null && $serviceName !== 'Hoststatus') {
                $node = $testConfig->createService($hostName, $serviceName);
            } else {
                $node = $testConfig->createHost($hostName);
                if ($serviceName === null) {
                    $term->setSearchValue(BpConfig::joinNodeName($hostName, 'Hoststatus'));
                }
            }

            if ($this->parent->hasChild($term->getSearchValue())) {
                $term->setMessage($this->translate('Already defined in this process'));
                $isValid = false;
            } else {
                $testConfig->getNode('__unbound__')
                    ->addChild($node);
            }
        }

        if ($testConfig->getBackend() instanceof MonitoringBackend) {
            MonitoringState::apply($testConfig);
        } else {
            IcingaDbState::apply($testConfig);
        }

        foreach ($terms as $term) {
            /** @var Term $term */
            $node = $testConfig->getNode($term->getSearchValue());
            if ($node->isMissing()) {
                if ($node instanceof ServiceNode) {
                    $term->setMessage($this->translate('Service not found'));
                } else {
                    $term->setMessage($this->translate('Host not found'));
                }

                $isValid = false;
            } else {
                $term->setLabel($node->getAlias());
                $term->setClass($node->getObjectClassName());
            }
        }

        return $isValid;
    }
}