summaryrefslogtreecommitdiffstats
path: root/library/Director/Objects/IcingaFlatVar.php
blob: 3bbf81c8034dedd611c5e64c3bbc3d71893175a9 (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
<?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 IcingaFlatVar extends DbObject
{
    protected $table = 'icinga_flat_var';

    protected $keyName = [
        'var_checksum',
        'flatname_checksum'
    ];

    protected $defaultProperties = [
        'var_checksum'      => null,
        'flatname_checksum' => null,
        'flatname'          => null,
        'flatvalue'         => null,
    ];

    protected $binaryProperties = [
        'var_checksum',
        'flatname_checksum',
    ];

    public static function generateForCustomVar(CustomVariable $var, Db $db)
    {
        $flatVars = static::forCustomVar($var, $db);
        foreach ($flatVars as $flat) {
            $flat->store();
        }

        return $flatVars;
    }

    public static function forCustomVar(CustomVariable $var, Db $db)
    {
        $flat = [];
        $varSum = $var->checksum();
        $var->flatten($flat, $var->getKey());
        $flatVars = [];

        foreach ($flat as $name => $value) {
            $flatVar = static::create([
                'var_checksum'      => $varSum,
                'flatname_checksum' => sha1($name, true),
                'flatname'          => $name,
                'flatvalue'         => $value,
            ], $db);

            $flatVar->store();
            $flatVars[] = $flatVar;
        }

        return $flatVars;
    }
}