MessageIDValidation   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 53.33%

Importance

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