MessageIDValidation::getError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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