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

RFCValidation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 17
dl 0
loc 44
ccs 12
cts 15
cp 0.8
rs 10
c 1
b 1
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 17 3
A getError() 0 3 1
A getWarnings() 0 3 1
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