MessageIDParser   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 80.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 76
ccs 25
cts 31
cp 0.8065
rs 10
c 1
b 0
f 0
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A processIDRight() 0 8 1
A parse() 0 7 1
A getRightPart() 0 3 1
A preLeftParsing() 0 6 2
A getLeftPart() 0 3 1
A parseRightFromAt() 0 3 1
A processIDLeft() 0 8 1
A parseLeftFromAt() 0 3 1
A addLongEmailWarning() 0 4 2
1
<?php
2
3
namespace Egulias\EmailValidator;
4
5
use Egulias\EmailValidator\Result\Result;
6
use Egulias\EmailValidator\Parser\IDLeftPart;
7
use Egulias\EmailValidator\Parser\IDRightPart;
8
use Egulias\EmailValidator\Result\ValidEmail;
9
use Egulias\EmailValidator\Result\InvalidEmail;
10
use Egulias\EmailValidator\Warning\EmailTooLong;
11
use Egulias\EmailValidator\Result\Reason\NoLocalPart;
12
13
class MessageIDParser extends Parser
14
{
15
16
    public const EMAILID_MAX_LENGTH = 254;
17
18
    /**
19
     * @var string
20
     */
21
    protected $idLeft = '';
22
23
    /**
24
     * @var string
25
     */
26
    protected $idRight = '';
27
28 9
    public function parse(string $str): Result
29
    {
30 9
        $result = parent::parse($str);
31
32 9
        $this->addLongEmailWarning($this->idLeft, $this->idRight);
33
34 9
        return $result;
35
    }
36
37 9
    protected function preLeftParsing(): Result
38
    {
39 9
        if (!$this->hasAtToken()) {
40
            return new InvalidEmail(new NoLocalPart(), $this->lexer->current->value);
41
        }
42 9
        return new ValidEmail();
43
    }
44
45 9
    protected function parseLeftFromAt(): Result
46
    {
47 9
        return $this->processIDLeft();
48
    }
49
50 7
    protected function parseRightFromAt(): Result
51
    {
52 7
        return $this->processIDRight();
53
    }
54
55 9
    private function processIDLeft(): Result
56
    {
57 9
        $localPartParser = new IDLeftPart($this->lexer);
58 9
        $localPartResult = $localPartParser->parse();
59 9
        $this->idLeft = $localPartParser->localPart();
60 9
        $this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings);
61
62 9
        return $localPartResult;
63
    }
64
65 7
    private function processIDRight(): Result
66
    {
67 7
        $domainPartParser = new IDRightPart($this->lexer);
68 7
        $domainPartResult = $domainPartParser->parse();
69 7
        $this->idRight = $domainPartParser->domainPart();
70 7
        $this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings);
71
72 7
        return $domainPartResult;
73
    }
74
75
    public function getLeftPart(): string
76
    {
77
        return $this->idLeft;
78
    }
79
80
    public function getRightPart(): string
81
    {
82
        return $this->idRight;
83
    }
84
85 9
    private function addLongEmailWarning(string $localPart, string $parsedDomainPart): void
86
    {
87 9
        if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAILID_MAX_LENGTH) {
88
            $this->warnings[EmailTooLong::CODE] = new EmailTooLong();
89
        }
90
    }
91
}
92