Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
86.67% |
13 / 15 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
Reload | |
86.67% |
13 / 15 |
|
85.71% |
6 / 7 |
9.19 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setDelay | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
serialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
unserialize | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
toArray | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
jsonSerialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\Model\Message |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\Model\Message; |
16 | |
17 | use phpOMS\Contract\SerializableInterface; |
18 | |
19 | /** |
20 | * Reload class. |
21 | * |
22 | * @package phpOMS\Model\Message |
23 | * @license OMS License 2.0 |
24 | * @link https://jingga.app |
25 | * @since 1.0.0 |
26 | */ |
27 | final class Reload implements \JsonSerializable, SerializableInterface |
28 | { |
29 | /** |
30 | * Message type. |
31 | * |
32 | * @var string |
33 | * @since 1.0.0 |
34 | */ |
35 | public const TYPE = 'reload'; |
36 | |
37 | /** |
38 | * Delay in ms. |
39 | * |
40 | * @var int |
41 | * @since 1.0.0 |
42 | */ |
43 | private int $delay = 0; |
44 | |
45 | /** |
46 | * Constructor. |
47 | * |
48 | * @param int $delay Delay in ms |
49 | * |
50 | * @since 1.0.0 |
51 | */ |
52 | public function __construct(int $delay = 0) |
53 | { |
54 | $this->delay = $delay; |
55 | } |
56 | |
57 | /** |
58 | * Set delay. |
59 | * |
60 | * @param int $delay Delay in ms |
61 | * |
62 | * @return void |
63 | * |
64 | * @since 1.0.0 |
65 | */ |
66 | public function setDelay(int $delay) : void |
67 | { |
68 | $this->delay = $delay; |
69 | } |
70 | |
71 | /** |
72 | * Render message. |
73 | * |
74 | * @return string |
75 | * |
76 | * @since 1.0.0 |
77 | */ |
78 | public function serialize() : string |
79 | { |
80 | return $this->__toString(); |
81 | } |
82 | |
83 | /** |
84 | * {@inheritdoc} |
85 | */ |
86 | public function unserialize(mixed $raw) : void |
87 | { |
88 | if (!\is_string($raw)) { |
89 | return; |
90 | } |
91 | |
92 | $unserialized = \json_decode($raw, true); |
93 | if (!\is_array($unserialized)) { |
94 | return; |
95 | } |
96 | |
97 | $this->delay = $unserialized['time'] ?? 0; |
98 | } |
99 | |
100 | /** |
101 | * Stringify. |
102 | * |
103 | * @return string |
104 | * |
105 | * @since 1.0.0 |
106 | */ |
107 | public function __toString() |
108 | { |
109 | return (string) \json_encode($this->toArray()); |
110 | } |
111 | |
112 | /** |
113 | * Generate message array. |
114 | * |
115 | * @return array<string, mixed> |
116 | * |
117 | * @since 1.0.0 |
118 | */ |
119 | public function toArray() : array |
120 | { |
121 | return [ |
122 | 'type' => self::TYPE, |
123 | 'time' => $this->delay, |
124 | ]; |
125 | } |
126 | |
127 | /** |
128 | * {@inheritdoc} |
129 | */ |
130 | public function jsonSerialize() : mixed |
131 | { |
132 | return $this->toArray(); |
133 | } |
134 | } |