Passed
Push — master ( e1c4a9...da8727 )
by Hirofumi
01:57
created

Heartbeater   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 1
dl 0
loc 63
ccs 18
cts 18
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 14 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Shippinno\Heartbeat;
5
6
use Exception;
7
8
class Heartbeater
9
{
10
    /**
11
     * @var Heart
12
     */
13
    protected $heart;
14
15
    /**
16
     * @var array
17
     */
18
    protected $channels;
19
20
    /**
21
     * @var Heartbeater
22
     */
23
    private static $instance;
24
25
    /**
26
     * @return Heartbeater
27
     */
28 4
    public static function instance(): Heartbeater
29
    {
30 4
        if (is_null(self::$instance)) {
31 1
            self::$instance = new Heartbeater;
32
        }
33
34 4
        return self::$instance;
35
    }
36
37
    /**
38
     * @param Heart $heart
39
     */
40 3
    public function setHeart(Heart $heart): void
41
    {
42 3
        $this->heart = $heart;
43 3
    }
44
45
    /**
46
     * @param array $channels
47
     */
48 2
    public function setChannels(array $channels): void
49
    {
50 2
        $this->channels = $channels;
51 2
    }
52
53
    /**
54
     * @param string $channelName
55
     */
56 4
    public function heartbeat(string $channelName): void
57
    {
58 4
        if (is_null($this->heart)) {
59 1
            return;
60
        }
61 3
        if (!isset($this->channels[$channelName])) {
62 1
            return;
63
        }
64
        try {
65 2
            $this->heart->beat($this->channels[$channelName]);
66 1
        } catch (Exception $e) {
67
            // Ignore exceptions.
68
        }
69 2
    }
70
}
71