Lines
100.00%
23 / 23
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ItemArrivals | 100.00% | 23 / 23 | 100.00% | 2 / 2 | 12 | 100.00% | 1 / 1 | |||
| of | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 5 | |||||
| kind | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 7 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Server\Event\Inventory\Helper; | |
| 6 | ||
| 7 | use Lorekeeper\Server\Event\Event; | |
| 8 | use Lorekeeper\Server\Exception\MalformedEventException; | |
| 9 | use Lorekeeper\Server\Message\JsonMessage; | |
| 10 | use Lorekeeper\Server\Message\Message; | |
| 11 | use Psl\Type; | |
| 12 | use Psl\Type\Exception\CoercionException; | |
| 13 | ||
| 14 | /** | |
| 15 | * @psalm-type Entry = array{ItemID: positive-int, sName?: non-empty-string, bTemp?: bool, iQty: positive-int, iQtyNow?: positive-int, ...} | |
| 16 | */ | |
| 17 | final readonly class ItemArrivals | |
| 18 | { | |
| 19 | /** | |
| 20 | * @param class-string<Event> $event | |
| 21 | * | |
| 22 | * @throws MalformedEventException | |
| 23 | * | |
| 24 | * @return null|list<array{ItemArrival, Entry}> | |
| 25 | */ | |
| 26 | public static function of(Message $message, string $event): ?array | |
| 27 | { | |
| 28 | if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'addItems') { | |
| 29 | return null; | |
| 30 | } | |
| 31 | ||
| 32 | try { | |
| 33 | $entries = Type\dict(Type\positive_int(), Type\shape([ | |
| 34 | 'ItemID' => Type\optional(Type\positive_int()), | |
| 35 | 'sName' => Type\optional(Type\non_empty_string()), | |
| 36 | 'bTemp' => Type\optional(Type\bool()), | |
| 37 | 'iQty' => Type\positive_int(), | |
| 38 | 'iQtyNow' => Type\optional(Type\positive_int()), | |
| 39 | ], allowUnknownFields: true))->coerce($message->data['items'] ?? null); | |
| 40 | } catch (CoercionException $exception) { | |
| 41 | throw new MalformedEventException($event, 'items are missing or invalid', $exception); | |
| 42 | } | |
| 43 | ||
| 44 | $sorted = []; | |
| 45 | foreach ($entries as $id => $entry) { | |
| 46 | $sorted[] = [self::kind($entry, $event), ['ItemID' => $id, ...$entry]]; | |
| 47 | } | |
| 48 | ||
| 49 | return $sorted; | |
| 50 | } | |
| 51 | ||
| 52 | /** | |
| 53 | * @param array{sName?: non-empty-string, bTemp?: bool, iQtyNow?: positive-int, ...} $entry | |
| 54 | * @param class-string<Event> $event | |
| 55 | * | |
| 56 | * @throws MalformedEventException | |
| 57 | */ | |
| 58 | private static function kind(array $entry, string $event): ItemArrival | |
| 59 | { | |
| 60 | return match (true) { | |
| 61 | array_key_exists('sName', $entry) && ($entry['bTemp'] ?? false) => ItemArrival::QuestItemReceived, | |
| 62 | array_key_exists('sName', $entry) => throw new MalformedEventException( | |
| 63 | $event, | |
| 64 | 'a new item came straight into the inventory, which is not read yet', | |
| 65 | ), | |
| 66 | array_key_exists('iQtyNow', $entry) => ItemArrival::ItemIncreased, | |
| 67 | array_key_exists('ItemID', $entry) => ItemArrival::QuestItemIncreased, | |
| 68 | default => throw new MalformedEventException($event, 'an added item matches no known shape'), | |
| 69 | }; | |
| 70 | } | |
| 71 | } |