Skip to content

Commit 8fabf69

Browse files
fix: get client (#11)
when getting a client by the brokerId/index it will return a string based on the $brokers array instead of the $clients array
1 parent 3b8530b commit 8fabf69

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/Broker.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,15 @@ public function getClient(?int $brokerId = null): ClientInterface
9696
if (null === $brokerId) {
9797
return $this->getRandomClient();
9898
} elseif (isset($this->brokers[$brokerId])) {
99-
return $this->brokers[$brokerId];
99+
return $this->getClientByIndex($brokerId);
100100
} else {
101101
throw new InvalidArgumentException(sprintf('Not found brokerId %s', $brokerId));
102102
}
103103
}
104104

105-
public function getRandomClient(): ClientInterface
105+
public function getClientByIndex(int $index): ClientInterface
106106
{
107107
$brokers = $this->getBrokers();
108-
$index = array_rand($brokers, 1);
109108
$url = parse_url($brokers[$index]);
110109
if (!$url) {
111110
throw new InvalidArgumentException(sprintf('Invalid bootstrapServer %s', $brokers[$index]));
@@ -123,6 +122,11 @@ public function getRandomClient(): ClientInterface
123122
return $this->clients[$index];
124123
}
125124

125+
public function getRandomClient(): ClientInterface
126+
{
127+
return $this->getClientByIndex(array_rand($this->getBrokers(), 1));
128+
}
129+
126130
/**
127131
* @return string|string[]
128132
*/

0 commit comments

Comments
 (0)