Lines
94.28%
33 / 35
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 | 90.00% | 18 / 20 | 0.00% | 0 / 1 | 8.06 | ||
| summary | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 2 | ||
| [Lorekeeper\Server\Event\NamedAfterClass] name | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 1 | ||
| 19 | final readonly class MoreItemsAddedEvent implements Event | |
| 20 | { | |
| 21 | use NamedAfterClass; | |
| 22 | ||
| 23 | /** @param non-empty-list<AddedItem> $items */ | |
| 24 | public function __construct( | |
| 25 | public array $items, | |
| 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) !== 'addItems') { | |
| 33 | return Option\none(); | |
| 34 | } | |
| 35 | ||
| 36 | try { | |
| 37 | $items = Type\dict(Type\positive_int(), Type\shape([ | |
| 38 | 'ItemID' => Type\optional(Type\positive_int()), | |
| 39 | 'iQty' => Type\positive_int(), | |
| 40 | 'iQtyNow' => Type\optional(Type\positive_int()), | |
| 41 | 'sName' => Type\optional(Type\string()), | |
| 42 | ], allowUnknownFields: true))->coerce($message->data['items'] ?? null); | |
| 43 | } catch (CoercionException $exception) { | |
| 44 | throw new MalformedEventException(static::class, 'items are missing or invalid', $exception); | |
| 45 | } | |
| 46 | ||
| 47 | $added = []; | |
| 48 | foreach ($items as $id => $entry) { | |
| 49 | if (array_key_exists('sName', $entry)) { | |
| 50 | continue; | |
| 51 | } | |
| 52 | ||
| 53 | $added[] = new AddedItem( | |
| 54 | new ItemId($entry['ItemID'] ?? $id), | |
| 55 | new Quantity($entry['iQty']), | |
| 56 | array_key_exists('iQtyNow', $entry) ? new Quantity($entry['iQtyNow']) : null, | |
| 57 | ); | |
| 58 | } | |
| 59 | ||
| 60 | return $added === [] ? Option\none() : Option\some(new static($added)); | |
| 61 | } | |
| 62 | ||
| 63 | #[Override] | |
| 64 | public function summary(): string | |
| 65 | { | |
| 66 | return 'Got ' | |
| 67 | . implode(', ', array_map( | |
| 68 | static fn(AddedItem $added): string => ( | |
| 69 | "{$added->quantity->value} more of item {$added->item->value}" | |
| 70 | . ($added->held === null ? '' : ", {$added->held->value} held now") | |
| 71 | ), | |
| 72 | $this->items, | |
| 73 | )); | |
| 74 | } | |
| 75 | } |
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 | } |