summaryrefslogtreecommitdiffstats
path: root/library/Director/IcingaConfig/IcingaConfigFile.php
blob: 109eb8a01f0a0b5ad669dd4f0a6d93beaccbf2ef (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php

namespace Icinga\Module\Director\IcingaConfig;

use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Util;

class IcingaConfigFile
{
    public static $table = 'director_generated_file';

    public static $keyName = 'checksum';

    protected $content;

    protected $checksum;

    protected $cntObject = 0;

    protected $cntTemplate = 0;

    protected $cntApply = 0;

    /**
     * @param $content
     *
     * @return self
     */
    public function prepend($content)
    {
        $this->content = $content . $this->content;
        $this->checksum = null;
        return $this;
    }

    public function getContent()
    {
        return $this->content;
    }

    public function setContent($content)
    {
        $this->content = $content;
        $this->checksum = null;
        return $this;
    }

    public function addContent($content)
    {
        if ($this->content === null) {
            $this->content = $content;
        } else {
            $this->content .= $content;
        }
        $this->checksum = null;
        return $this;
    }

    public function getObjectCount()
    {
        return $this->cntObject;
    }

    public function getTemplateCount()
    {
        return $this->cntTemplate;
    }

    public function getApplyCount()
    {
        return $this->cntApply;
    }

    public function getSize()
    {
        return strlen($this->content);
    }

    public function setObjectCount($cnt)
    {
        $this->cntObject = $cnt;
        return $this;
    }

    public function setTemplateCount($cnt)
    {
        $this->cntTemplate = $cnt;
        return $this;
    }

    public function setApplyCount($cnt)
    {
        $this->cntApply = $cnt;
        return $this;
    }

    public function getHexChecksum()
    {
        return bin2hex($this->getChecksum());
    }

    public function getChecksum()
    {
        if ($this->checksum === null) {
            $this->checksum = sha1($this->content, true);
        }

        return $this->checksum;
    }

    public function addLegacyObjects($objects)
    {
        foreach ($objects as $object) {
            $this->addLegacyObject($object);
        }

        return $this;
    }

    public function addObjects($objects)
    {
        foreach ($objects as $object) {
            $this->addObject($object);
        }

        return $this;
    }

    public function addObject(IcingaObject $object)
    {
        $this->content .= $object->toConfigString();
        $this->checksum = null;
        return $this->addObjectStats($object);
    }

    public function addLegacyObject(IcingaObject $object)
    {
        $this->content .= $object->toLegacyConfigString();
        $this->checksum = null;
        return $this->addObjectStats($object);
    }

    protected function addObjectStats(IcingaObject $object)
    {
        if ($object->hasProperty('object_type')) {
            $type = $object->object_type;

            switch ($type) {
                case 'object':
                    $this->cntObject++;
                    break;
                case 'template':
                    $this->cntTemplate++;
                    break;
                case 'apply':
                    $this->cntApply++;
                    break;
            }
        }

        return $this;
    }

    public function __toString()
    {
        return $this->getContent();
    }
}