Passed
Push — master ( f047b9...ae8494 )
by Sergey
08:39
created

TimeHeartbeat   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 132
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A serverBeat() 0 4 1
A clientBeat() 0 4 1
A currentTime() 0 4 1
A setClientBeatFactor() 0 6 1
A shouldSendHeartbeat() 0 8 2
A isServerHeartbeatMissing() 0 8 2
A setServerBeatFactor() 0 6 1
1
<?php
2
3
namespace ButterAMQP\Heartbeat;
4
5
use ButterAMQP\HeartbeatInterface;
6
7
class TimeHeartbeat implements HeartbeatInterface
8
{
9
    /**
10
     * @var int
11
     */
12
    private $heartbeatDelay;
13
14
    /**
15
     * @var int
16
     */
17
    private $lastServerBeat;
18
19
    /**
20
     * @var int
21
     */
22
    private $lastClientBeat;
23
24
    /**
25
     * @var float
26
     */
27
    private $serverBeatFactor = 1.5;
28
29
    /**
30
     * @var float
31
     */
32
    private $clientBeatFactor = 0.75;
33
34
    /**
35
     * @var callable|string
36
     */
37
    private $timeFunction;
38
39
    /**
40
     * @param int             $heartbeatDelay
41
     * @param callable|string $timeFunction
42
     */
43 27
    public function __construct($heartbeatDelay, $timeFunction = 'time')
44
    {
45 27
        $this->heartbeatDelay = $heartbeatDelay;
46 27
        $this->timeFunction = $timeFunction;
47 27
    }
48
49
    /**
50
     * Heartbeat factor for server beats. This is multiplier for heartbeat delay when waiting for server heartbeat.
51
     *
52
     * Possible values:
53
     *  - Value of 1 means server connection will be considered dead if heartbeat didn't arrive in exact time of heartbeat.
54
     *  - Value less than 1 would mean heartbeat would be expected in less than heartbeat delay, which does not make
55
     *    much sense.
56
     *  - Value more than 1 would mean we allow extra delay for server heartbeats, for example factor of 2 would mean
57
     *    we allow server to delay heartbeat up to double of its expected time.
58
     *
59
     * @param float $serverBeatFactor
60
     *
61
     * @return $this
62
     */
63 3
    public function setServerBeatFactor($serverBeatFactor)
64
    {
65 3
        $this->serverBeatFactor = $serverBeatFactor;
66
67 3
        return $this;
68
    }
69
70
    /**
71
     * Heartbeat factor for client beats. This is multiplier for heartbeat delay when deciding to send heartbeat.
72
     *
73
     * Possible values:
74
     *  - Value of 1 means heartbeat would be sent after exactly time of heartbeat delay.
75
     *  - Value less than 1 would mean heartbeat would be send more frequently than expected. It is a good idea to send
76
     *    heartbeats a bit more frequently.
77
     *  - Value more than 1 would mean heartbeat would be send less frequently than expected. This may cause server to
78
     *    close connection because heartbeats will be delayed.
79
     *
80
     * @param float $clientBeatFactor
81
     *
82
     * @return $this
83
     */
84 4
    public function setClientBeatFactor($clientBeatFactor)
85
    {
86 4
        $this->clientBeatFactor = $clientBeatFactor;
87
88 4
        return $this;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 21
    public function serverBeat()
95
    {
96 21
        $this->lastServerBeat = $this->currentTime();
97 21
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 21
    public function clientBeat()
103
    {
104 21
        $this->lastClientBeat = $this->currentTime();
105 21
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 21
    public function shouldSendHeartbeat()
111
    {
112 21
        if ($this->heartbeatDelay == 0) {
113 1
            return false;
114
        }
115
116 20
        return $this->currentTime() - $this->lastClientBeat >= $this->heartbeatDelay * $this->clientBeatFactor;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 4
    public function isServerHeartbeatMissing()
123
    {
124 4
        if ($this->heartbeatDelay == 0) {
125 1
            return false;
126
        }
127
128 3
        return $this->currentTime() - $this->lastServerBeat > $this->heartbeatDelay * $this->serverBeatFactor;
129
    }
130
131
    /**
132
     * @return int
133
     */
134 25
    private function currentTime()
135
    {
136 25
        return call_user_func($this->timeFunction);
137
    }
138
}
139