Lines
90.90%
20 / 22
Methods
66.66%
2 / 3
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| read | n/a | 0 / 0 | n/a | 0 / 0 | 0 | ||
| from | 86.66% | 13 / 15 | 0.00% | 0 / 1 | 4.04 | ||
| [Lorekeeper\Server\Event\NamedAfterClass] name | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 1 | ||
| 20 | abstract readonly class RewardedEvent implements Event | |
| 21 | { | |
| 22 | use NamedAfterClass; | |
| 23 | ||
| 24 | public function __construct( | |
| 25 | public Gold $gold, | |
| 26 | public Experience $experience, | |
| 27 | public ClassPoints $classPoints, | |
| 28 | ) {} | |
| 29 | ||
| 30 | /** | |
| 31 | * @param array<array-key, mixed> $data | |
| 32 | * | |
| 33 | * @throws MalformedEventException | |
| 34 | * | |
| 35 | * @return Option\Option<static> | |
| 36 | */ | |
| 37 | abstract protected static function read( | |
| 38 | array $data, | |
| 39 | Gold $gold, | |
| 40 | Experience $experience, | |
| 41 | ClassPoints $classPoints, | |
| 42 | ): Option\Option; | |
| 43 | ||
| 44 | /** @return Option\Option<static> */ | |
| 45 | #[Override] | |
| 46 | final public static function from(Message $message): Option\Option | |
| 47 | { | |
| 48 | if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'addGoldExp') { | |
| 49 | return Option\none(); | |
| 50 | } | |
| 51 | ||
| 52 | try { | |
| 53 | $reward = Type\shape([ | |
| 54 | 'intGold' => Type\optional(Type\uint()), | |
| 55 | 'intExp' => Type\optional(Type\uint()), | |
| 56 | 'iCP' => Type\optional(Type\uint()), | |
| 57 | ], allowUnknownFields: true)->coerce($message->data); | |
| 58 | } catch (CoercionException $exception) { | |
| 59 | throw new MalformedEventException(static::class, 'reward is invalid', $exception); | |
| 60 | } | |
| 61 | ||
| 62 | return static::read( | |
| 63 | $message->data, | |
| 64 | new Gold($reward['intGold'] ?? 0), | |
| 65 | new Experience($reward['intExp'] ?? 0), | |
| 66 | new ClassPoints($reward['iCP'] ?? 0), | |
| 67 | ); | |
| 68 | } | |
| 69 | } |
From Lorekeeper\Server\Event\NamedAfterClass
| 7 | trait 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 | } |