Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
14 / 14 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| ConnectAndLoginScript | |
100.00% |
14 / 14 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handles | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AqwSocketClient\Scripts; |
| 6 | |
| 7 | use AqwSocketClient\Commands\LoginCommand; |
| 8 | use AqwSocketClient\Events\ConnectionEstablishedEvent; |
| 9 | use AqwSocketClient\Events\LoginRespondedEvent; |
| 10 | use AqwSocketClient\Interfaces\CommandInterface; |
| 11 | use AqwSocketClient\Interfaces\EventInterface; |
| 12 | use AqwSocketClient\Objects\Names\PlayerName; |
| 13 | use Override; |
| 14 | |
| 15 | final class ConnectAndLoginScript extends AbstractScript |
| 16 | { |
| 17 | public function __construct( |
| 18 | private readonly PlayerName $playerName, |
| 19 | #[\SensitiveParameter] |
| 20 | private readonly string $token, |
| 21 | ) {} |
| 22 | |
| 23 | #[Override] |
| 24 | public function handles(): array |
| 25 | { |
| 26 | return [ |
| 27 | ConnectionEstablishedEvent::class, |
| 28 | LoginRespondedEvent::class, |
| 29 | ]; |
| 30 | } |
| 31 | |
| 32 | #[Override] |
| 33 | public function handle(EventInterface $event, ClientContext $context): ?CommandInterface |
| 34 | { |
| 35 | if ($event instanceof ConnectionEstablishedEvent) { |
| 36 | return new LoginCommand($this->playerName, $this->token); |
| 37 | } |
| 38 | |
| 39 | if ($event instanceof LoginRespondedEvent) { |
| 40 | if (!$event->success) { |
| 41 | $this->failed(); |
| 42 | return null; |
| 43 | } |
| 44 | |
| 45 | $context->set('socket_id', $event->socketId); |
| 46 | $this->success(); |
| 47 | } |
| 48 | |
| 49 | return null; |
| 50 | } |
| 51 | } |