Total Complexity | 15 |
Total Lines | 108 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
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 |
||
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 |
||
114 | } |
||
115 | } |