summaryrefslogtreecommitdiffstats
path: root/library/Businessprocess/IcingaDbObject.php
blob: cad459f7e192c73c9481e4c24f8d4f23bcec8e35 (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
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
<?php

namespace Icinga\Module\Businessprocess;

use Icinga\Module\Icingadb\Common\Auth;
use Icinga\Module\Icingadb\Common\Database as IcingadbDatabase;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\Service;
use ipl\Sql\Connection as IcingaDbConnection;
use ipl\Web\Filter\QueryString;

class IcingaDbObject
{
    use IcingadbDatabase;

    use Auth;

    /** @var BpConfig */
    protected $config;

    /** @var IcingaDbConnection */
    protected $conn;

    public function __construct()
    {
        $this->conn = $this->getDb();
    }

    public function fetchHosts($filter = null)
    {

        $hosts = Host::on($this->conn);

        if ($filter !== null) {
            $filterQuery = QueryString::parse($filter);

            $hosts->filter($filterQuery);
        }

        $hosts->orderBy('host.name');

        $this->applyIcingaDbRestrictions($hosts);

        return $hosts;
    }

    public function fetchServices($filter)
    {
        $services = Service::on($this->conn)
            ->with('host');

        if ($filter !== null) {
            $filterQuery = QueryString::parse($filter);

            $services->filter($filterQuery);
        }

        $services->orderBy('service.name');

        $this->applyIcingaDbRestrictions($services);

        return $services;
    }

    public function yieldHostnames($filter = null)
    {
        foreach ($this->fetchHosts($filter) as $host) {
            yield $host->name;
        }
    }

    public function yieldServicenames($host)
    {
        $filter = "host.name=$host";

        foreach ($this->fetchServices($filter) as $service) {
            yield $service->name;
        }
    }

    public static function applyIcingaDbRestrictions($query)
    {
        $object = new self;
        $object->applyRestrictions($query);

        return $object;
    }

    public static function fetchDb()
    {
        $object = new self;
        return $object->getDb();
    }
}