Completed
Push — master ( a61873...e5615d )
by Veaceslav
02:02
created

Notification::flushErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Zee Project.
4
 *
5
 * @see https://github.com/zee/
6
 */
7
8
declare(strict_types=1);
9
10
namespace Zee\Errors;
11
12
use ArrayIterator;
13
use Countable;
14
use IteratorAggregate;
15
use Traversable;
16
17
/**
18
 * Class Notification.
19
 */
20
final class Notification implements Countable, IteratorAggregate
21
{
22
    private $errors = [];
23
24
    /**
25
     * Adds new error to the notification.
26
     *
27
     * @param string $message
28
     * @param array $context
29
     */
30 1
    public function addError(string $message, array $context = []): void
31
    {
32 1
        $this->errors[] = new Error($message, $context);
33
    }
34
35
    /**
36
     * Returns whether the notification has any errors.
37
     *
38
     * @return bool
39
     */
40 2
    public function hasErrors(): bool
41
    {
42 2
        return !empty($this->errors);
43
    }
44
45
    /**
46
     * Flushes the notification errors.
47
     */
48 1
    public function flushErrors(): array
49
    {
50 1
        $errors = $this->errors;
51 1
        $this->errors = [];
52
53 1
        return $errors;
54
    }
55
56
    /**
57
     * @return array
58
     */
59 1
    public function getErrorMessages(): array
60
    {
61 1
        return array_map(
62
            function (Error $error) {
63 1
                return $error->getMessage();
64 1
            },
65 1
            $this->errors
66
        );
67
    }
68
69
    /**
70
     * Counts the errors.
71
     *
72
     * @inheritdoc
73
     */
74 2
    public function count(): int
75
    {
76 2
        return count($this->errors);
77
    }
78
79
    /**
80
     * Builds the iterator by errors.
81
     *
82
     * @inheritdoc
83
     *
84
     * @return Traversable|Error[]
85
     */
86 1
    public function getIterator(): Traversable
87
    {
88 1
        return new ArrayIterator($this->errors);
89
    }
90
}
91