Lines 100.00% 39 / 39
Methods 100.00% 5 / 5
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 from 100.00% 25 / 25 100.00% 1 / 1 6
 summary 100.00% 1 / 1 100.00% 1 / 1 1
 settings 100.00% 6 / 6 100.00% 1 / 1 3
 [Lorekeeper\Server\Event\NamedAfterClass] name 100.00% 6 / 6 100.00% 1 / 1 1
25final readonly class HeroSettingsLoadedEvent implements Event
26{
27    use NamedAfterClass;
28
29    private const array BITS = [
30        4 => GameSetting::Goto,
31        7 => GameSetting::FriendRequests,
32        8 => GameSetting::PartyInvites,
33        9 => GameSetting::GuildInvites,
34        10 => GameSetting::Whispers,
35        13 => GameSetting::Duels,
36    ];
37
38    public function __construct(
39        public HeroSettingsData $data,
40    ) {}
41
42    /** @return Option\Option<static> */
43    #[Override]
44    public static function from(Message $message): Option\Option
45    {
46        if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'initUserDatas') {
47            return Option\none();
48        }
49
50        $hero = PlayerEntries::hero($message, static::class);
51        if ($hero->isNone()) {
52            return Option\none();
53        }
54
55        try {
56            $entry = Type\shape([
57                'uid' => Type\positive_int(),
58                'data' => Type\shape([
59                    'strUsername' => Type\non_empty_string(),
60                    'ia1' => Type\optional(Type\int()),
61                ], allowUnknownFields: true),
62            ], allowUnknownFields: true)->coerce($hero->unwrap());
63        } catch (CoercionException $exception) {
64            throw new MalformedEventException(static::class, 'hero settings are invalid', $exception);
65        }
66
67        if (!array_key_exists('ia1', $entry['data'])) {
68            return Option\none();
69        }
70
71        return Option\some(
72            new static(
73                new HeroSettingsData(
74                    new PlayerSpawnId($entry['uid']),
75                    new PlayerName($entry['data']['strUsername']),
76                    self::settings($entry['data']['ia1']),
77                ),
78            ),
79        );
80    }
81
82    #[Override]
83    public function summary(): string
84    {
85        return "Settings of {$this->data->name->value}";
86    }
87
88    private static function settings(int $bits): HeroSettings
89    {
90        $allowed = [];
91        foreach (self::BITS as $bit => $setting) {
92            if (($bits & (1 << $bit)) !== 0) {
93                continue;
94            }
95
96            $allowed[] = $setting;
97        }
98
99        return new HeroSettings($allowed);
100    }
101}

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}