summaryrefslogtreecommitdiffstats
path: root/library/Director/Objects/IcingaVar.php
blob: 10addf2ff7cbc01c88d5c4e9d2ea1cdf7b150970 (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
<?php

namespace Icinga\Module\Director\Objects;

use Icinga\Module\Director\CustomVariable\CustomVariable;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Db;

class IcingaVar extends DbObject
{
    protected $table = 'icinga_var';

    protected $keyName = 'checksum';

    /** @var CustomVariable */
    protected $var;

    protected $defaultProperties = [
        'checksum'          => null,
        'rendered_checksum' => null,
        'varname'           => null,
        'varvalue'          => null,
        'rendered'          => null
    ];

    protected $binaryProperties = [
        'checksum',
        'rendered_checksum',
    ];

    /**
     * @param CustomVariable $customVar
     * @param Db $db
     *
     * @return static
     */
    public static function forCustomVar(CustomVariable $customVar, Db $db)
    {
        $rendered = $customVar->render();

        $var = static::create(array(
            'checksum'          => $customVar->checksum(),
            'rendered_checksum' => sha1($rendered, true),
            'varname'           => $customVar->getKey(),
            'varvalue'          => $customVar->toJson(),
            'rendered'          => $rendered,
        ), $db);

        $var->var = $customVar;

        return $var;
    }

    /**
     * @param CustomVariable $customVar
     * @param Db $db
     *
     * @return static
     * @throws \Icinga\Module\Director\Exception\DuplicateKeyException
     */
    public static function generateForCustomVar(CustomVariable $customVar, Db $db)
    {
        $var = static::forCustomVar($customVar, $db);
        $var->store();
        return $var;
    }

    protected function onInsert()
    {
        IcingaFlatVar::generateForCustomVar($this->var, $this->getConnection());
    }
}