Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.30% |
21 / 23 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
Notify | |
91.30% |
21 / 23 |
|
83.33% |
5 / 6 |
8.04 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
serialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
jsonSerialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
unserialize | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
3.07 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
toArray | |
100.00% |
8 / 8 |
|
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 | use phpOMS\Message\NotificationLevel; |
19 | |
20 | /** |
21 | * Notify class. |
22 | * |
23 | * @package phpOMS\Model\Message |
24 | * @license OMS License 2.0 |
25 | * @link https://jingga.app |
26 | * @since 1.0.0 |
27 | */ |
28 | final class Notify implements \JsonSerializable, SerializableInterface |
29 | { |
30 | /** |
31 | * Message type. |
32 | * |
33 | * @var string |
34 | * @since 1.0.0 |
35 | */ |
36 | public const TYPE = 'notify'; |
37 | |
38 | /** |
39 | * Notification title. |
40 | * |
41 | * @var string |
42 | * @since 1.0.0 |
43 | */ |
44 | public string $title = ''; |
45 | |
46 | /** |
47 | * Message. |
48 | * |
49 | * @var string |
50 | * @since 1.0.0 |
51 | */ |
52 | public string $message = ''; |
53 | |
54 | /** |
55 | * Delay in ms. |
56 | * |
57 | * @var int |
58 | * @since 1.0.0 |
59 | */ |
60 | public int $delay = 0; |
61 | |
62 | /** |
63 | * Stay in ms. |
64 | * |
65 | * @var int |
66 | * @since 1.0.0 |
67 | */ |
68 | public int $stay = 0; |
69 | |
70 | /** |
71 | * Level or type. |
72 | * |
73 | * @var string |
74 | * @since 1.0.0 |
75 | */ |
76 | public string $level = NotificationLevel::INFO; |
77 | |
78 | /** |
79 | * Constructor. |
80 | * |
81 | * @param string $msg Message |
82 | * @param string $level Message level |
83 | * |
84 | * @since 1.0.0 |
85 | */ |
86 | public function __construct(string $msg = '', string $level = NotificationLevel::INFO) |
87 | { |
88 | $this->message = $msg; |
89 | $this->level = $level; |
90 | } |
91 | |
92 | /** |
93 | * Render message. |
94 | * |
95 | * @return string |
96 | * |
97 | * @since 1.0.0 |
98 | */ |
99 | public function serialize() : string |
100 | { |
101 | return $this->__toString(); |
102 | } |
103 | |
104 | /** |
105 | * {@inheritdoc} |
106 | */ |
107 | public function jsonSerialize() : mixed |
108 | { |
109 | return $this->toArray(); |
110 | } |
111 | |
112 | /** |
113 | * {@inheritdoc} |
114 | */ |
115 | public function unserialize(mixed $raw) : void |
116 | { |
117 | if (!\is_string($raw)) { |
118 | return; |
119 | } |
120 | |
121 | $unserialized = \json_decode($raw, true); |
122 | if (!\is_array($unserialized)) { |
123 | return; |
124 | } |
125 | |
126 | $this->delay = $unserialized['time'] ?? 0; |
127 | $this->stay = $unserialized['stay'] ?? 0; |
128 | $this->message = $unserialized['msg'] ?? ''; |
129 | $this->title = $unserialized['title'] ?? ''; |
130 | $this->level = $unserialized['level'] ?? 0; |
131 | } |
132 | |
133 | /** |
134 | * Stringify. |
135 | * |
136 | * @return string |
137 | * |
138 | * @since 1.0.0 |
139 | */ |
140 | public function __toString() |
141 | { |
142 | return (string) \json_encode($this->toArray()); |
143 | } |
144 | |
145 | /** |
146 | * Generate message array. |
147 | * |
148 | * @return array<string, mixed> |
149 | * |
150 | * @since 1.0.0 |
151 | */ |
152 | public function toArray() : array |
153 | { |
154 | return [ |
155 | 'type' => self::TYPE, |
156 | 'time' => $this->delay, |
157 | 'stay' => $this->stay, |
158 | 'msg' => $this->message, |
159 | 'title' => $this->title, |
160 | 'level' => $this->level, |
161 | ]; |
162 | } |
163 | } |