summaryrefslogtreecommitdiffstats
path: root/library/Director/Deployment/ConditionalConfigRenderer.php
blob: 0b24418a7f4cfc6aa31a2e0ac33c9c4bb0903e98 (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
<?php

namespace Icinga\Module\Director\Deployment;

use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Objects\DirectorActivityLog;

class ConditionalConfigRenderer
{
    /** @var Db */
    protected $db;

    protected $forceRendering = false;

    public function __construct(Db $connection)
    {
        $this->db = $connection;
    }

    public function forceRendering($force = true)
    {
        $this->forceRendering = $force;

        return $this;
    }

    public function getConfig()
    {
        if ($this->shouldGenerate()) {
            return IcingaConfig::generate($this->db);
        }

        return $this->loadLatestActivityConfig();
    }

    protected function loadLatestActivityConfig()
    {
        $db = $this->db;

        return IcingaConfig::loadByActivityChecksum($db->getLastActivityChecksum(), $db);
    }

    protected function shouldGenerate()
    {
        return $this->forceRendering || !$this->configForLatestActivityExists();
    }

    protected function configForLatestActivityExists()
    {
        $db = $this->db;
        try {
            $latestActivity = DirectorActivityLog::loadLatest($db);
        } catch (NotFoundError $e) {
            return false;
        }

        return IcingaConfig::existsForActivityChecksum(
            bin2hex($latestActivity->get('checksum')),
            $db
        );
    }
}