Notification::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * {@see https://github.com/zee/ Zee Project (c)}
4
 */
5
6
declare(strict_types=1);
7
8
namespace Zee\Errors;
9
10
use ArrayIterator;
11
use Countable;
12
use IteratorAggregate;
13
use Traversable;
14
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
50
    {
51 1
        return array_map(
52
            function (Error $error) {
53 1
                return $error->getMessage();
54 1
            },
55 1
            $this->errors
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
73
    {
74 1
        return new ArrayIterator($this->errors);
75
    }
76
}
77