summaryrefslogtreecommitdiffstats
path: root/library/Director/Db/AppliedServiceSetLoader.php
blob: b1e94087e1f262e29338f683885cb3bbebb9a18c (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
51
52
53
54
55
56
57
58
<?php

namespace Icinga\Module\Director\Db;

use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Objects\HostApplyMatches;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaServiceSet;

class AppliedServiceSetLoader
{
    protected $host;

    public function __construct(IcingaHost $host)
    {
        $this->host = $host;
    }

    /**
     * @return IcingaServiceSet[]
     */
    public static function fetchForHost(IcingaHost $host)
    {
        $loader = new static($host);
        return $loader->fetchAppliedServiceSets();
    }

    /**
     * @return IcingaServiceSet[]
     */
    protected function fetchAppliedServiceSets()
    {
        $sets = array();
        $matcher = HostApplyMatches::prepare($this->host);
        foreach ($this->fetchAllServiceSets() as $set) {
            $filter = Filter::fromQueryString($set->get('assign_filter'));
            if ($matcher->matchesFilter($filter)) {
                $sets[] = $set;
            }
        }

        return $sets;
    }

    /**
     * @return IcingaServiceSet[]
     */
    protected function fetchAllServiceSets()
    {
        $db = $this->host->getDb();
        $query = $db
            ->select()
            ->from('icinga_service_set')
            ->where('assign_filter IS NOT NULL');

        return IcingaServiceSet::loadAll($this->host->getConnection(), $query);
    }
}