Lines
88.88%
8 / 9
Functions and Methods
66.66%
2 / 3
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| PlayerLeftEvent | 88.88% | 8 / 9 | 66.66% | 2 / 3 | 6.05 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| from | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 4 | |||||
| summary | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Server\Event\Player; | |
| 6 | ||
| 7 | use Lorekeeper\Core\Player\PlayerName; | |
| 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 Lorekeeper\Server\SocketId; | |
| 14 | use Override; | |
| 15 | use Psl\Option; | |
| 16 | use Psl\Type; | |
| 17 | use Psl\Type\Exception\CoercionException; | |
| 18 | ||
| 19 | final readonly class PlayerLeftEvent implements Event | |
| 20 | { | |
| 21 | use NamedAfterClass; | |
| 22 | ||
| 23 | public function __construct( | |
| 24 | public SocketId $socketId, | |
| 25 | public PlayerName $name, | |
| 26 | ) {} | |
| 27 | ||
| 28 | /** @return Option\Option<static> */ | |
| 29 | #[Override] | |
| 30 | public static function from(Message $message): Option\Option | |
| 31 | { | |
| 32 | if (!$message instanceof DelimitedMessage || ($message->data[1] ?? null) !== 'exitArea') { | |
| 33 | return Option\none(); | |
| 34 | } | |
| 35 | ||
| 36 | try { | |
| 37 | $socketId = Type\positive_int()->coerce($message->data[3] ?? null); | |
| 38 | $name = Type\non_empty_string()->coerce($message->data[4] ?? null); | |
| 39 | } catch (CoercionException $exception) { | |
| 40 | throw new MalformedEventException(static::class, 'player socket or name is missing or invalid', $exception); | |
| 41 | } | |
| 42 | ||
| 43 | return Option\some(new static(new SocketId($socketId), new PlayerName($name))); | |
| 44 | } | |
| 45 | ||
| 46 | #[Override] | |
| 47 | public function summary(): string | |
| 48 | { | |
| 49 | return "{$this->name->value} (socket {$this->socketId->value}) left"; | |
| 50 | } | |
| 51 | } |