Passed
Push — 3.x ( 451b43...8e526a )
by Eduardo Gulias
02:05
created

MessageIDValidation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 53.33%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 42
ccs 8
cts 15
cp 0.5333
rs 10
c 0
b 0
f 0

3 Methods

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