summaryrefslogtreecommitdiffstats
path: root/library/Director/Deployment/DeploymentGracePeriod.php
blob: 6cde25af21408fd0bf3f15afecc86fe694bd24dd (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\Deployment;

use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\DirectorDeploymentLog;

class DeploymentGracePeriod
{
    /** @var int */
    protected $graceTimeSeconds;

    /** @var Db */
    protected $db;

    /**
     * @param int $graceTimeSeconds
     * @param Db $db
     */
    public function __construct($graceTimeSeconds, Db $db)
    {
        $this->graceTimeSeconds = $graceTimeSeconds;
        $this->db = $db;
    }

    /**
     * Whether we're still within a grace period
     * @return bool
     */
    public function isActive()
    {
        if ($deployment = $this->lastDeployment()) {
            return $deployment->getDeploymentTimestamp() > $this->getGracePeriodStart();
        }

        return false;
    }

    protected function getGracePeriodStart()
    {
        return time() - $this->graceTimeSeconds;
    }

    public function getRemainingGraceTime()
    {
        if ($this->isActive()) {
            if ($deployment = $this->lastDeployment()) {
                return $deployment->getDeploymentTimestamp() - $this->getGracePeriodStart();
            } else {
                return null;
            }
        }

        return 0;
    }

    protected function lastDeployment()
    {
        return DirectorDeploymentLog::optionalLatest($this->db);
    }
}