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

MessageIDParser::preLeftParsing()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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