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

RFCValidation::isValid()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.1825

Importance

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