Lines
90.90%
10 / 11
Functions and Methods
66.66%
2 / 3
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ItemUnwornEvent | 90.90% | 10 / 11 | 66.66% | 2 / 3 | 6.03 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| from | 100.00% | 9 / 9 | 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\Item; | |
| 6 | ||
| 7 | use Lorekeeper\Core\Item\ItemId; | |
| 8 | use Lorekeeper\Server\Event\Event; | |
| 9 | use Lorekeeper\Server\Event\NamedAfterClass; | |
| 10 | use Lorekeeper\Server\Exception\MalformedEventException; | |
| 11 | use Lorekeeper\Server\Message\JsonMessage; | |
| 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 ItemUnwornEvent implements Event | |
| 20 | { | |
| 21 | use NamedAfterClass; | |
| 22 | ||
| 23 | public function __construct( | |
| 24 | public SocketId $player, | |
| 25 | public ItemId $itemId, | |
| 26 | ) {} | |
| 27 | ||
| 28 | /** @return Option\Option<static> */ | |
| 29 | #[Override] | |
| 30 | public static function from(Message $message): Option\Option | |
| 31 | { | |
| 32 | if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'unwearItem') { | |
| 33 | return Option\none(); | |
| 34 | } | |
| 35 | ||
| 36 | try { | |
| 37 | $data = Type\shape([ | |
| 38 | 'uid' => Type\positive_int(), | |
| 39 | 'ItemID' => Type\positive_int(), | |
| 40 | ], allowUnknownFields: true)->coerce($message->data); | |
| 41 | } catch (CoercionException $exception) { | |
| 42 | throw new MalformedEventException(static::class, 'unworn item is missing or invalid', $exception); | |
| 43 | } | |
| 44 | ||
| 45 | return Option\some(new static(new SocketId($data['uid']), new ItemId($data['ItemID']))); | |
| 46 | } | |
| 47 | ||
| 48 | #[Override] | |
| 49 | public function summary(): string | |
| 50 | { | |
| 51 | return "Socket {$this->player->value} took off item {$this->itemId->value}"; | |
| 52 | } | |
| 53 | } |