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

EmailParser::parseLeftFromAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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