Test Failed
Pull Request — master (#12)
by wujunze
03:09
created

Broker::setData()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 38
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 19
c 1
b 0
f 1
nc 32
nop 2
dl 0
loc 38
rs 8.8333
1
<?php
2
declare(strict_types=1);
3
4
namespace Seasx\SeasLogger\Kafka;
5
6
class Broker
7
{
8
    /**
9
     * @var int
10
     */
11
    private $groupBrokerId;
12
    /**
13
     * @var mixed[][]
14
     */
15
    private $topics = [];
16
    /**
17
     * @var string[]
18
     */
19
    private $brokers = [];
20
21
    /**
22
     * Broker constructor.
23
     * @param array $configs
24
     */
25
    public function __construct(array $configs)
26
    {
27
        foreach ($configs as $name => $value) {
28
            if (property_exists($this, $name)) {
29
                $this->$name = $value;
30
            }
31
        }
32
    }
33
34
    /**
35
     * @return int
36
     */
37
    public function getGroupBrokerId(): int
38
    {
39
        return $this->groupBrokerId;
40
    }
41
42
    /**
43
     * @param int $brokerId
44
     */
45
    public function setGroupBrokerId(int $brokerId): void
46
    {
47
        $this->groupBrokerId = $brokerId;
48
    }
49
50
    /**
51
     * @param array $topics
52
     * @param array $brokersResult
53
     * @return bool
54
     */
55
    public function setData(array $topics, array $brokersResult): bool
56
    {
57
        $brokers = [];
58
59
        foreach ($brokersResult as $value) {
60
            $brokers[$value['nodeId']] = $value['host'] . ':' . $value['port'];
61
        }
62
63
        $changed = false;
64
65
        if (serialize($this->brokers) !== serialize($brokers)) {
66
            $this->brokers = $brokers;
67
68
            $changed = true;
69
        }
70
71
        $newTopics = [];
72
        foreach ($topics as $topic) {
73
            if ((int)$topic['errorCode'] !== Protocol::NO_ERROR) {
74
                continue;
75
            }
76
77
            $item = [];
78
79
            foreach ($topic['partitions'] as $part) {
80
                $item[$part['partitionId']] = $part['leader'];
81
            }
82
83
            $newTopics[$topic['topicName']] = $item;
84
        }
85
86
        if (serialize($this->topics) !== serialize($newTopics)) {
87
            $this->topics = $newTopics;
88
89
            $changed = true;
90
        }
91
92
        return $changed;
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    public function getTopics(): array
99
    {
100
        return $this->topics;
101
    }
102
103
    /**
104
     * @return string[]
105
     */
106
    public function getBrokers(): array
107
    {
108
        return $this->brokers;
109
    }
110
111
    public function clear(): void
112
    {
113
        $this->brokers = [];
114
    }
115
}