Lines
100.00%
10 / 10
Functions and Methods
100.00%
3 / 3
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| LoginSucceededEvent | 100.00% | 10 / 10 | 100.00% | 3 / 3 | 7 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| from | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 5 | |||||
| summary | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Server\Event\Session; | |
| 6 | ||
| 7 | use Lorekeeper\Core\Player\PlayerSpawnId; | |
| 8 | use Lorekeeper\Server\Event\Event; | |
| 9 | use Lorekeeper\Server\Event\NamedAfterClass; | |
| 10 | use Lorekeeper\Server\Exception\MalformedEventException; | |
| 11 | use Lorekeeper\Server\Message\DelimitedMessage; | |
| 12 | use Lorekeeper\Server\Message\Message; | |
| 13 | use Override; | |
| 14 | use Psl\Option; | |
| 15 | use Psl\Type; | |
| 16 | use Psl\Type\Exception\CoercionException; | |
| 17 | ||
| 18 | /** | |
| 19 | * The server logged our player in. | |
| 20 | */ | |
| 21 | final readonly class LoginSucceededEvent implements Event | |
| 22 | { | |
| 23 | use NamedAfterClass; | |
| 24 | ||
| 25 | public function __construct( | |
| 26 | public PlayerSpawnId $spawnId, | |
| 27 | ) {} | |
| 28 | ||
| 29 | /** @return Option\Option<static> */ | |
| 30 | #[Override] | |
| 31 | public static function from(Message $message): Option\Option | |
| 32 | { | |
| 33 | if (!$message instanceof DelimitedMessage || ($message->data[1] ?? null) !== 'loginResponse') { | |
| 34 | return Option\none(); | |
| 35 | } | |
| 36 | ||
| 37 | if (($message->data[3] ?? null) !== 'true') { | |
| 38 | return Option\none(); | |
| 39 | } | |
| 40 | ||
| 41 | try { | |
| 42 | $spawnId = Type\positive_int()->coerce($message->data[4] ?? null); | |
| 43 | } catch (CoercionException $exception) { | |
| 44 | throw new MalformedEventException(static::class, 'spawn id is missing or invalid', $exception); | |
| 45 | } | |
| 46 | ||
| 47 | return Option\some(new static(new PlayerSpawnId($spawnId))); | |
| 48 | } | |
| 49 | ||
| 50 | #[Override] | |
| 51 | public function summary(): string | |
| 52 | { | |
| 53 | return "Logged in as spawn {$this->spawnId->value}"; | |
| 54 | } | |
| 55 | } |