Passed
Push — master ( e37941...93d395 )
by Hirofumi
03:03
created

Heartbeater   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 1
dl 0
loc 59
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 8 2
A setHeart() 0 4 1
A setChannels() 0 4 1
A heartbeat() 0 10 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Shippinno\Heartbeat;
5
6
class Heartbeater
7
{
8
    /**
9
     * @var Heart
10
     */
11
    protected $heart;
12
13
    /**
14
     * @var array
15
     */
16
    protected $channels;
17
18
    /**
19
     * @var Heartbeater
20
     */
21
    private static $instance;
22
23
    /**
24
     * @return Heartbeater
25
     */
26 3
    public static function instance(): Heartbeater
27
    {
28 3
        if (is_null(self::$instance)) {
29 1
            self::$instance = new Heartbeater;
30
        }
31
32 3
        return self::$instance;
33
    }
34
35
    /**
36
     * @param Heart $heart
37
     */
38 2
    public function setHeart(Heart $heart): void
39
    {
40 2
        $this->heart = $heart;
41 2
    }
42
43
    /**
44
     * @param array $channels
45
     */
46 1
    public function setChannels(array $channels): void
47
    {
48 1
        $this->channels = $channels;
49 1
    }
50
51
    /**
52
     * @param string $channelName
53
     */
54 3
    public function heartbeat(string $channelName): void
55
    {
56 3
        if (is_null($this->heart)) {
57 1
            return;
58
        }
59 2
        if (!isset($this->channels[$channelName])) {
60 1
            return;
61
        }
62 1
        $this->heart->beat($this->channels[$channelName]);
63 1
    }
64
}
65