Total Complexity | 6 |
Total Lines | 60 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
15 | final class Notification implements Countable, IteratorAggregate |
||
16 | { |
||
17 | private $errors = []; |
||
18 | |||
19 | /** |
||
20 | * Adds new error to the notification. |
||
21 | */ |
||
22 | 1 | public function addError(string $message, array $context = []): void |
|
23 | { |
||
24 | 1 | $this->errors[] = new Error($message, $context); |
|
25 | } |
||
26 | |||
27 | /** |
||
28 | * Returns whether the notification has any errors. |
||
29 | */ |
||
30 | 2 | public function hasErrors(): bool |
|
31 | { |
||
32 | 2 | return !empty($this->errors); |
|
33 | } |
||
34 | |||
35 | /** |
||
36 | * Flushes the notification errors. |
||
37 | */ |
||
38 | 1 | public function flushErrors(): array |
|
39 | { |
||
40 | 1 | $errors = $this->errors; |
|
41 | 1 | $this->errors = []; |
|
42 | |||
43 | 1 | return $errors; |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * Returns list of error messages only. |
||
48 | */ |
||
49 | 1 | public function getErrorMessages(): array |
|
56 | ); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Counts the errors in the notification. |
||
61 | */ |
||
62 | 2 | public function count(): int |
|
63 | { |
||
64 | 2 | return count($this->errors); |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Builds the iterator by errors. |
||
69 | * |
||
70 | * @return Traversable|Error[] |
||
71 | */ |
||
72 | 1 | public function getIterator(): Traversable |
|
75 | } |
||
76 | } |
||
77 |