Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 10 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ClientManager | |
0.00% |
0 / 10 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
| add | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getBySocket | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| remove | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Socket\Server |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Socket\Server; |
| 16 | |
| 17 | use phpOMS\Account\NullAccount; |
| 18 | use phpOMS\Socket\Client\ClientConnection; |
| 19 | use phpOMS\Socket\Client\NullClientConnection; |
| 20 | |
| 21 | /** |
| 22 | * Client manager class. |
| 23 | * |
| 24 | * @package phpOMS\Socket\Server |
| 25 | * @license OMS License 2.0 |
| 26 | * @link https://jingga.app |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | class ClientManager |
| 30 | { |
| 31 | private $clients = []; |
| 32 | |
| 33 | /** |
| 34 | * Add client |
| 35 | * |
| 36 | * @param ClientConnection $client Client |
| 37 | * |
| 38 | * @return void |
| 39 | * |
| 40 | * @since 1.0.0 |
| 41 | */ |
| 42 | public function add(ClientConnection $client) : void |
| 43 | { |
| 44 | $this->clients[$client->getId()] = $client; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get client by id |
| 49 | * |
| 50 | * @return mixed |
| 51 | * |
| 52 | * @since 1.0.0 |
| 53 | */ |
| 54 | public function get($id) |
| 55 | { |
| 56 | return $this->clients[$id] ?? new NullClientConnection($id, null); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get client by socket |
| 61 | * |
| 62 | * @param mixed $socket Socket |
| 63 | * |
| 64 | * @return mixed |
| 65 | * |
| 66 | * @since 1.0.0 |
| 67 | */ |
| 68 | public function getBySocket($socket) |
| 69 | { |
| 70 | foreach ($this->clients as $client) { |
| 71 | if ($client->getSocket() === $socket) { |
| 72 | return $client; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return new NullClientConnection(new NullAccount(), null); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Remove client by id |
| 81 | * |
| 82 | * @param mixed $id Client id |
| 83 | * |
| 84 | * @return bool |
| 85 | * |
| 86 | * @since 1.0.0 |
| 87 | */ |
| 88 | public function remove($id) : bool |
| 89 | { |
| 90 | if (isset($this->clients[$id])) { |
| 91 | unset($this->clients[$id]); |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | return false; |
| 97 | } |
| 98 | } |