-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5a546c
commit 90e15dc
Showing
9 changed files
with
220 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace LaravelQless\Queue; | ||
|
||
use ArrayIterator; | ||
use Qless\Client; | ||
|
||
/** | ||
* Class QlessConnectionHandler | ||
* @package LaravelQless\Queue | ||
*/ | ||
class QlessConnectionHandler | ||
{ | ||
|
||
/** @var Client[] */ | ||
private $clients; | ||
|
||
/** @var ArrayIterator */ | ||
private $clientIterator; | ||
|
||
/** | ||
* QlessConnectionHandler constructor. | ||
* @param Client[] $clients | ||
*/ | ||
public function __construct(array $clients) | ||
{ | ||
$this->init($clients); | ||
} | ||
|
||
private function init(array $clients): void | ||
{ | ||
foreach ($clients as $client) { | ||
if (!$client instanceof Client) { | ||
continue; | ||
} | ||
$this->clients[] = $client; | ||
|
||
} | ||
if (empty($this->clients)) { | ||
throw new \Exception("No configs found"); | ||
} | ||
|
||
$this->clientIterator = new ArrayIterator($this->clients); | ||
} | ||
|
||
public function getRandomClient(): Client | ||
{ | ||
return $this->clients[array_rand($this->clients)]; | ||
} | ||
|
||
public function getAllClients(): array | ||
{ | ||
return $this->clients; | ||
} | ||
|
||
public function getCurrentClient(): Client | ||
{ | ||
if ($this->clientIterator->current() === null) { | ||
return $this->getNextClient(); | ||
} | ||
|
||
return $this->clientIterator->current(); | ||
} | ||
|
||
public function getNextClient(): Client | ||
{ | ||
if ($this->clientIterator->current() === null) { | ||
$this->clientIterator->rewind(); | ||
} | ||
|
||
$currentClient = $this->clientIterator->current(); | ||
$this->clientIterator->next(); | ||
|
||
return $currentClient; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.