RFCValidation::getError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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