Lines 100.00% 49 / 49
Methods 100.00% 4 / 4
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 from 100.00% 41 / 41 100.00% 1 / 1 6
 summary 100.00% 1 / 1 100.00% 1 / 1 1
 [Lorekeeper\Server\Event\NamedAfterClass] name 100.00% 6 / 6 100.00% 1 / 1 1
31final readonly class HeroDataLoadedEvent implements Event
32{
33    use NamedAfterClass;
34
35    public function __construct(
36        public HeroData $data,
37    ) {}
38
39    /** @return Option\Option<static> */
40    #[Override]
41    public static function from(Message $message): Option\Option
42    {
43        if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'initUserDatas') {
44            return Option\none();
45        }
46
47        $hero = PlayerEntries::hero($message, static::class);
48        if ($hero->isNone()) {
49            return Option\none();
50        }
51
52        try {
53            $entry = Type\shape([
54                'uid' => Type\positive_int(),
55                'data' => Type\shape([
56                    'strUsername' => Type\non_empty_string(),
57                    'intLevel' => Type\int_range(min: 1, max: 100),
58                    'intExp' => Type\uint(),
59                    'intExpToLevel' => Type\uint(),
60                    'intGold' => Type\uint(),
61                    'intCoins' => Type\uint(),
62                    'iUpg' => Type\int(),
63                    'iUpgDays' => Type\int(),
64                    'iBagSlots' => Type\positive_int(),
65                    'iBankSlots' => Type\positive_int(),
66                ], allowUnknownFields: true),
67            ], allowUnknownFields: true)->coerce($hero->unwrap());
68        } catch (CoercionException $exception) {
69            throw new MalformedEventException(static::class, 'hero data is missing or invalid', $exception);
70        }
71
72        $data = $entry['data'];
73
74        return Option\some(
75            new static(
76                new HeroData(
77                    new PlayerSpawnId($entry['uid']),
78                    new PlayerName($data['strUsername']),
79                    new PlayerLevel($data['intLevel']),
80                    new Gauge(
81                        new Experience($data['intExp']),
82                        new Experience(max($data['intExp'], $data['intExpToLevel'])),
83                    ),
84                    new HeroWallet(new Gold($data['intGold']), new AdventureCoins($data['intCoins'])),
85                    $data['iUpg'] > 0 && $data['iUpgDays'] >= 0,
86                    new Quantity($data['iBagSlots']),
87                    new Quantity($data['iBankSlots']),
88                    QuestGroups::held($data),
89                ),
90            ),
91        );
92    }
93
94    #[Override]
95    public function summary(): string
96    {
97        return "Data of {$this->data->name->value}";
98    }
99}

From Lorekeeper\Server\Event\NamedAfterClass

7trait 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}