Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| LoginRespondedEvent | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| from | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AqwSocketClient\Events; |
| 6 | |
| 7 | use AqwSocketClient\Enums\DelimitedMessageType; |
| 8 | use AqwSocketClient\Interfaces\EventInterface; |
| 9 | use AqwSocketClient\Interfaces\MessageInterface; |
| 10 | use AqwSocketClient\Messages\DelimitedMessage; |
| 11 | use AqwSocketClient\Objects\Identifiers\SocketIdentifier; |
| 12 | use Override; |
| 13 | |
| 14 | /** |
| 15 | * Represents the **response** received from the server after a client |
| 16 | * attempts a login operation. |
| 17 | * |
| 18 | * This event signals whether the authentication was successful or failed. |
| 19 | */ |
| 20 | final class LoginRespondedEvent implements EventInterface |
| 21 | { |
| 22 | public function __construct( |
| 23 | public readonly bool $success, |
| 24 | public readonly ?SocketIdentifier $socketId = null, |
| 25 | ) {} |
| 26 | |
| 27 | /** |
| 28 | * @return ?LoginRespondedEvent |
| 29 | * |
| 30 | * @throws InvalidArgumentException When socket id in data is negative or zero. |
| 31 | */ |
| 32 | #[Override] |
| 33 | public static function from(MessageInterface $message): ?EventInterface |
| 34 | { |
| 35 | if ($message instanceof DelimitedMessage && $message->type === DelimitedMessageType::LoginResponse) { |
| 36 | $success = (bool) $message->data[0]; |
| 37 | $socketId = $success ? new SocketIdentifier((int) $message->data[1]) : null; |
| 38 | |
| 39 | return new self($success, $socketId); |
| 40 | } |
| 41 | |
| 42 | return null; |
| 43 | } |
| 44 | } |