EmailParser::addLongEmailWarning()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

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