Lines 100.00% 33 / 33
Methods 100.00% 6 / 6
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 from 100.00% 5 / 5 100.00% 1 / 1 6
 summary 100.00% 1 / 1 100.00% 1 / 1 1
 delimited 100.00% 6 / 6 100.00% 1 / 1 3
 json 100.00% 10 / 10 100.00% 1 / 1 2
 moved 100.00% 10 / 10 100.00% 1 / 1 3
23final readonly class PlayerMovedEvent extends PlayerUpdatedEvent
24{
25    public function __construct(
26        PlayerName $name,
27        public MapSpot $spot,
28    ) {
29        parent::__construct($name);
30    }
31
32    /** @return Option\Option<static> */
33    #[Override]
34    public static function from(Message $message): Option\Option
35    {
36        return match (true) {
37            $message instanceof DelimitedMessage && ($message->data[1] ?? null) === 'uotls' => self::delimited(
38                $message,
39            ),
40            $message instanceof JsonMessage && ($message->data['cmd'] ?? null) === 'uotls' => self::json($message),
41            default => Option\none(),
42        };
43    }
44
45    #[Override]
46    public function summary(): string
47    {
48        return "{$this->name->value} moved to {$this->spot->cell->value} ({$this->spot->pad->name})";
49    }
50
51    /**
52     * @throws MalformedEventException
53     *
54     * @return Option\Option<static>
55     */
56    private static function delimited(DelimitedMessage $message): Option\Option
57    {
58        $update = $message->data[4] ?? '';
59        $frame = [];
60        $side = [];
61        $cell = preg_match('/(?:^|,)strFrame:([^,]*)/', $update, $frame) === 1 ? $frame[1] : null;
62        $pad = preg_match('/(?:^|,)strPad:([^,]*)/', $update, $side) === 1 ? $side[1] : null;
63
64        return self::moved($message->data[3] ?? null, $cell, $pad);
65    }
66
67    /**
68     * @throws MalformedEventException
69     *
70     * @return Option\Option<static>
71     */
72    private static function json(JsonMessage $message): Option\Option
73    {
74        try {
75            $update = Type\shape([
76                'unm' => Type\optional(Type\mixed()),
77                'o' => Type\optional(Type\shape([
78                    'strFrame' => Type\optional(Type\mixed()),
79                    'strPad' => Type\optional(Type\mixed()),
80                ], allowUnknownFields: true)),
81            ], allowUnknownFields: true)->coerce($message->data);
82        } catch (CoercionException $exception) {
83            throw new MalformedEventException(static::class, 'player update is invalid', $exception);
84        }
85
86        return self::moved($update['unm'] ?? null, $update['o']['strFrame'] ?? null, $update['o']['strPad'] ?? null);
87    }
88
89    /**
90     * @throws MalformedEventException
91     *
92     * @return Option\Option<static>
93     */
94    private static function moved(mixed $name, mixed $cell, mixed $pad): Option\Option
95    {
96        if ($pad === null) {
97            return Option\none();
98        }
99
100        try {
101            $name = Type\non_empty_string()->coerce($name);
102            $cell = Type\non_empty_string()->coerce($cell);
103            $pad = Type\non_empty_string()->coerce($pad);
104        } catch (CoercionException $exception) {
105            throw new MalformedEventException(static::class, 'player name, cell or pad is missing', $exception);
106        }
107
108        return Option\some(
109            new static(new PlayerName($name), new MapSpot(new MapCell($cell), MapPads::read($pad, static::class))),
110        );
111    }
112}