MultipleValidationWithAnd   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 30
c 1
b 1
f 0
dl 0
loc 106
ccs 30
cts 30
cp 1
rs 10
wmc 15

7 Methods

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