Lines
95.23%
20 / 21
Methods
75.00%
3 / 4
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| from | 100.00% | 13 / 13 | 100.00% | 1 / 1 | 6 | ||
| summary | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| [Lorekeeper\Server\Event\NamedAfterClass] name | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 1 | ||
| 20 | final readonly class PlayerEnteredEvent implements Event | |
| 21 | { | |
| 22 | use NamedAfterClass; | |
| 23 | ||
| 24 | public function __construct( | |
| 25 | public SocketId $socketId, | |
| 26 | public PlayerName $name, | |
| 27 | ) {} | |
| 28 | ||
| 29 | /** @return Option\Option<static> */ | |
| 30 | #[Override] | |
| 31 | public static function from(Message $message): Option\Option | |
| 32 | { | |
| 33 | if (!$message instanceof XmlMessage) { | |
| 34 | return Option\none(); | |
| 35 | } | |
| 36 | ||
| 37 | $body = $message->dom->getElementsByTagName('body')->item(0); | |
| 38 | if (!$body instanceof DOMElement || $body->getAttribute('action') !== 'uER') { | |
| 39 | return Option\none(); | |
| 40 | } | |
| 41 | ||
| 42 | $user = $body->getElementsByTagName('u')->item(0); | |
| 43 | if (!$user instanceof DOMElement) { | |
| 44 | throw new MalformedEventException(static::class, 'the player is missing'); | |
| 45 | } | |
| 46 | ||
| 47 | try { | |
| 48 | $socketId = Type\positive_int()->coerce($user->getAttribute('i')); | |
| 49 | $name = Type\non_empty_string()->coerce($user->getElementsByTagName('n')->item(0)?->textContent); | |
| 50 | } catch (CoercionException $exception) { | |
| 51 | throw new MalformedEventException(static::class, 'player socket or name is missing or invalid', $exception); | |
| 52 | } | |
| 53 | ||
| 54 | return Option\some(new static(new SocketId($socketId), new PlayerName($name))); | |
| 55 | } | |
| 56 | ||
| 57 | #[Override] | |
| 58 | public function summary(): string | |
| 59 | { | |
| 60 | return "{$this->name->value} (socket {$this->socketId->value}) entered"; | |
| 61 | } | |
| 62 | } |
From Lorekeeper\Server\Event\NamedAfterClass
| 7 | trait NamedAfterClass | |
| 8 | { | |
| 9 | public function name(): string | |
| 10 | { | |
| 11 | $class = static::class; | |
| 12 | ||
| 13 | return (string) preg_replace( | |
| 14 | '/Event$/', | |
| 15 | replacement: '', | |
| 16 | subject: substr($class, (int) strrpos($class, needle: '\\') + 1), | |
| 17 | ); | |
| 18 | } | |
| 19 | } |