SKeepalive::kaShutdown()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Service keepalive
4
 * User: moyo
5
 * Date: 18/09/2017
6
 * Time: 10:28 PM
7
 */
8
9
namespace Carno\Consul\Chips;
10
11
use Carno\Consul\Contracts\Defaults;
12
use Carno\Consul\Features\Keepalive;
13
14
trait SKeepalive
15
{
16
    /**
17
     * @var int
18
     */
19
    private $keepalive = null;
20
21
    /**
22
     * @var Keepalive
23
     */
24
    private $daemon = null;
25
26
    /**
27
     * @param int $heartbeat
28
     * @return self
29
     */
30
    public function setKeepalive(int $heartbeat) : self
31
    {
32
        $this->keepalive = $heartbeat;
33
        return $this;
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getKeepalive() : array
40
    {
41
        return $this->keepalive ? [
42
            'TTL' => sprintf('%ds', $this->keepalive + Defaults::KA_TTL_REDUNDANCY),
43
            'DeregisterCriticalServiceAfter' => sprintf('%dm', Defaults::KA_CRITICAL_TIMEOUT),
44
        ] : [];
45
    }
46
47
    /**
48
     */
49
    protected function kaStartup() : void
50
    {
51
        $this->daemon && $this->daemon->shutdown();
52
        $this->keepalive && $this->daemon = new Keepalive($this, $this->keepalive);
53
    }
54
55
    /**
56
     */
57
    protected function kaShutdown() : void
58
    {
59
        if ($this->daemon) {
60
            $this->daemon->shutdown();
61
            $this->daemon = null;
62
        }
63
    }
64
}
65