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

MessageIDValidation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 53.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 40
ccs 8
cts 15
cp 0.5333
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

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