Passed
Push — 3.x ( 4cccb8...037594 )
by Eduardo Gulias
02:37
created

MultipleValidationWithAnd::isValid()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 9
nop 2
dl 0
loc 18
ccs 12
cts 12
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Egulias\EmailValidator\Validation;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Result\InvalidEmail;
7
use Egulias\EmailValidator\Validation\Exception\EmptyValidationList;
8
use Egulias\EmailValidator\Result\MultipleErrors;
9
10
class MultipleValidationWithAnd implements EmailValidation
11
{
12
    /**
13
     * If one of validations fails, the remaining validations will be skept.
14
     * This means MultipleErrors will only contain a single error, the first found.
15
     */
16
    const STOP_ON_ERROR = 0;
17
18
    /**
19
     * All of validations will be invoked even if one of them got failure.
20
     * So MultipleErrors will contain all causes.
21
     */
22
    const ALLOW_ALL_ERRORS = 1;
23
24
    /**
25
     * @var EmailValidation[]
26
     */
27
    private $validations = [];
28
29
    /**
30
     * @var array
31
     */
32
    private $warnings = [];
33
34
    /**
35
     * @var MultipleErrors|null
36
     */
37
    private $error;
38
39
    /**
40
     * @var int
41
     */
42
    private $mode;
43
44
    /**
45
     * @param EmailValidation[] $validations The validations.
46
     * @param int               $mode        The validation mode (one of the constants).
47
     */
48 9
    public function __construct(array $validations, $mode = self::ALLOW_ALL_ERRORS)
49
    {
50 9
        if (count($validations) == 0) {
51 1
            throw new EmptyValidationList();
52
        }
53
54 8
        $this->validations = $validations;
55 8
        $this->mode = $mode;
56 8
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 8
    public function isValid(string $email, EmailLexer $emailLexer) : bool
62
    {
63 8
        $result = true;
64 8
        foreach ($this->validations as $validation) {
65 8
            $emailLexer->reset();
66 8
            $validationResult = $validation->isValid($email, $emailLexer);
67 8
            $result = $result && $validationResult;
68 8
            $this->warnings = array_merge($this->warnings, $validation->getWarnings());
69 8
            if (!$validationResult) {
70 6
                $this->processError($validation);
71
            }
72
73 8
            if ($this->shouldStop($result)) {
74 8
                break;
75
            }
76
        }
77
78 8
        return $result;
79
    }
80
81 6
    private function initErrorStorage() : void
82
    {
83 6
        if (null === $this->error) {
84 6
            $this->error = new MultipleErrors();
85
        }
86 6
    }
87
88 6
    private function processError(EmailValidation $validation) : void
89
    {
90 6
        if (null !== $validation->getError()) {
91 6
            $this->initErrorStorage();
92
            /** @psalm-suppress PossiblyNullReference */
93 6
            $this->error->addReason($validation->getError()->reason());
94
        }
95 6
    }
96
97 8
    private function shouldStop(bool $result) : bool
98
    {
99 8
        return !$result && $this->mode === self::STOP_ON_ERROR;
100
    }
101
102
    /**
103
     * Returns the validation errors.
104
     */
105 5
    public function getError() : ?InvalidEmail
106
    {
107 5
        return $this->error;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 2
    public function getWarnings() : array
114
    {
115 2
        return $this->warnings;
116
    }
117
}
118