Completed
Pull Request — master (#95)
by Eduardo Gulias
02:00
created

EmailValidator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 26
Bugs 2 Features 0
Metric Value
wmc 12
c 26
b 2
f 0
lcom 1
cbo 5
dl 0
loc 88
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isValid() 0 14 1
A hasWarnings() 0 4 1
A getWarnings() 0 4 1
A getError() 0 4 1
A checkDNS() 0 16 3
A addTLDWarnings() 0 9 4
1
<?php
2
3
namespace Egulias\EmailValidator;
4
5
use Egulias\EmailValidator\Warning\NoDNSMXRecord;
6
use Egulias\EmailValidator\Warning\NoDNSRecord;
7
use Egulias\EmailValidator\Warning\DomainLiteral;
8
use Egulias\EmailValidator\Warning\TLD;
9
use Egulias\EmailValidator\Validation\EmailValidation;
10
11
class EmailValidator
12
{
13
    const ERR_DEPREC_REACHED     = 151;
14
15
    /**
16
     * @var EmailLexer
17
     */
18
    private $lexer;
19
    protected $warnings;
20
    protected $error;
21
    protected $threshold = 255;
22
23
    public function __construct()
24
    {
25
        $this->lexer = new EmailLexer();
26
    }
27
28
    /**
29
     * @param                 $email
30
     * @param EmailValidation $emailValidation
31
     * @return bool
32
     */
33
    public function isValid($email, EmailValidation $emailValidation)
34
    {
35
        $isValid = $emailValidation->isValid($email, $this->lexer);
36
        $this->warnings = $emailValidation->getWarnings();
37
        $this->error = $emailValidation->getError();
38
        return $isValid;
39
40
//        if ($this->hasWarnings() && ((int) max($this->warnings) > $this->threshold)) {
41
//            $this->error = self::ERR_DEPREC_REACHED;
42
//
43
//            return false;
44
//        }
45
46
    }
47
48
    /**
49
     * @return boolean
50
     */
51
    public function hasWarnings()
52
    {
53
        return !empty($this->warnings);
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public function getWarnings()
60
    {
61
        return $this->warnings;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getError()
68
    {
69
        return $this->error;
70
    }
71
72
    protected function checkDNS()
73
    {
74
        $checked = true;
75
        $MXresult = checkdnsrr(trim($this->parser->getParsedDomainPart()), 'MX');
0 ignored issues
show
Bug introduced by
The property parser does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
76
77
        if (!$MXresult) {
78
            $this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
79
            $Aresult = checkdnsrr(trim($this->parser->getParsedDomainPart()), 'A');
80
            if (!$Aresult) {
81
                $this->warnings[NoDNSRecord::CODE] = new NoDNSRecord();
82
                $checked = false;
83
                $this->addTLDWarnings();
84
            }
85
        }
86
        return $checked;
87
    }
88
89
    protected function addTLDWarnings()
90
    {
91
        if (!isset($this->warnings[NoDNSMXRecord::CODE]) &&
92
            !isset($this->warnings[NoDNSRecord::CODE]) &&
93
            isset($this->warnings[DomainLiteral::CODE])
94
        ) {
95
            $this->warnings[TLD::CODE] = new TLD();
96
        }
97
    }
98
}
99