Lines 100.00% 19 / 19
Methods 100.00% 2 / 2
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 read 100.00% 18 / 18 100.00% 1 / 1 4
17final readonly class QuestAnswer
18{
19    private function __construct(
20        public QuestId $quest,
21        public QuestName $name,
22        public bool $completed,
23        public string $reason,
24    ) {}
25
26    /**
27     * @param class-string<Event> $event
28     *
29     * @throws MalformedEventException
30     *
31     * @return Option\Option<self>
32     */
33    public static function read(Message $message, string $event): Option\Option
34    {
35        if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'ccqr') {
36            return Option\none();
37        }
38
39        try {
40            $answer = Type\shape([
41                'bSuccess' => Type\int(),
42                'QuestID' => Type\positive_int(),
43                'sName' => Type\non_empty_string(),
44                'msg' => Type\optional(Type\string()),
45            ], allowUnknownFields: true)->coerce($message->data);
46        } catch (CoercionException $exception) {
47            throw new MalformedEventException($event, 'quest answer is invalid', $exception);
48        }
49
50        return Option\some(
51            new self(
52                new QuestId($answer['QuestID']),
53                new QuestName($answer['sName']),
54                $answer['bSuccess'] === 1,
55                $answer['msg'] ?? '',
56            ),
57        );
58    }
59}