Passed
Pull Request — 3.x (#290)
by Eduardo Gulias
01:49
created

EmailParser::hasAtToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
cc 2
nc 2
nop 0
crap 2
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 181
    public function __construct(EmailLexer $lexer)
29
    {
30 181
        $this->lexer = $lexer;
31 181
    }
32
33 181
    public function parse(string $str) : Result
34
    {
35 181
        $result = parent::parse($str);
36
37 181
        $this->addLongEmailWarning($this->localPart, $this->domainPart);
38
39 181
        return $result;
40
    }
41
    
42 177
    protected function preLeftParsing(): Result
43
    {
44 177
        if (!$this->hasAtToken()) {
45 1
            return new InvalidEmail(new NoLocalPart(), $this->lexer->token["value"]);
46
        }
47 176
        return new ValidEmail();
48
    }
49
50 176
    protected function parseLeftFromAt(): Result
51
    {
52 176
        return $this->processLocalPart();
53
    }
54
55 140
    protected function parseRightFromAt(): Result
56
    {
57 140
        return $this->processDomainPart();
58
    }
59
60 176
    private function processLocalPart() : Result
61
    {
62 176
        $localPartParser = new LocalPart($this->lexer);
63 176
        $localPartResult = $localPartParser->parse();
64 176
        $this->localPart = $localPartParser->localPart();
65 176
        $this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings);
66
67 176
        return $localPartResult;
68
    }
69
70 140
    private function processDomainPart() : Result
71
    {
72 140
        $domainPartParser = new DomainPart($this->lexer);
73 140
        $domainPartResult = $domainPartParser->parse();
74 140
        $this->domainPart = $domainPartParser->domainPart();
75 140
        $this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings);
76
        
77 140
        return $domainPartResult;
78
    }
79
80 4
    public function getDomainPart() : string
81
    {
82 4
        return $this->domainPart;
83
    }
84
85 4
    public function getLocalPart() : string
86
    {
87 4
        return $this->localPart;
88
    }
89
90 181
    private function addLongEmailWarning(string $localPart, string $parsedDomainPart) : void
91
    {
92 181
        if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAIL_MAX_LENGTH) {
93
            $this->warnings[EmailTooLong::CODE] = new EmailTooLong();
94
        }
95 181
    }
96
}
97