Passed
Push — 3.x ( 451b43...8e526a )
by Eduardo Gulias
02:05
created

MessageIDValidation::getError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Egulias\EmailValidator\Validation;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\MessageIDParser;
7
use Egulias\EmailValidator\Result\InvalidEmail;
8
use Egulias\EmailValidator\Result\Reason\ExceptionFound;
9
10
class MessageIDValidation implements EmailValidation
11
{
12
13
    /**
14
     * @var array
15
     */
16
    private $warnings = [];
17
18
    /**
19
     * @var ?InvalidEmail
20
     */
21
    private $error;
22
23 9
    public function isValid(string $email, EmailLexer $emailLexer): bool
24
    {
25 9
        $parser = new MessageIDParser($emailLexer);
26
        try {
27 9
            $result = $parser->parse($email);
28 9
            $this->warnings = $parser->getWarnings();
29 9
            if ($result->isInvalid()) {
30
                /** @psalm-suppress PropertyTypeCoercion */
31 6
                $this->error = $result;
32 9
                return false;
33
            }
34
        } catch (\Exception $invalid) {
35
            $this->error = new InvalidEmail(new ExceptionFound($invalid), '');
36
            return false;
37
        }
38
39 3
        return true;
40
    }
41
42
    public function getWarnings(): array
43
    {
44
        return $this->warnings;
45
    }
46
47
    public function getError(): ?InvalidEmail
48
    {
49
        return $this->error;
50
    }
51
}
52