Test Failed
Pull Request — master (#173)
by
unknown
08:11
created

DNSCheckValidation::getWarnings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Egulias\EmailValidator\Validation;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Exception\InvalidEmail;
7
use Egulias\EmailValidator\Warning\NoDNSMXRecord;
8
use Egulias\EmailValidator\Exception\NoDNSRecord;
9
10
class DNSCheckValidation implements EmailValidation
11
{
12
    /**
13
     * @var array
14
     */
15
    private $warnings = [];
16
17
    /**
18
     * @var InvalidEmail
19
     */
20
    private $error;
21
22 12
    public function isValid($email, EmailLexer $emailLexer)
23
    {
24
        // use the input to check DNS if we cannot extract something similar to a domain
25 12
        $host = $email;
26
27
        // Arguable pattern to extract the domain. Not aiming to validate the domain nor the email
28 12
        if (false !== $lastAtPos = strrpos($email, '@')) {
29 11
            $host = substr($email, $lastAtPos + 1);
30 11
        }
31
32 12
        return $this->checkDNS($host);
33
    }
34
35 1
    public function getError()
36
    {
37 1
        return $this->error;
38
    }
39
40 1
    public function getWarnings()
41
    {
42 1
        return $this->warnings;
43
    }
44
45 12
    protected function checkDNS($host)
46
    {
47 12
        if ( defined('INTL_IDNA_VARIANT_UTS46') ) {
48
            $variant = INTL_IDNA_VARIANT_UTS46
49 12
        } else {
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '}'
Loading history...
50 12
            $variant = INTL_IDNA_VARIANT_2003;
51
        }
52 12
        
53 11
        $host = rtrim(idn_to_ascii($host, IDNA_DEFAULT, $variant), '.') . '.';
54 11
55 11
        $Aresult = true;
56 3
        $MXresult = checkdnsrr($host, 'MX');
57 3
58 11
        if (!$MXresult) {
59 12
            $this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
60
            $Aresult = checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA');
61
            if (!$Aresult) {
62
                $this->error = new NoDNSRecord();
63
            }
64
        }
65
        return $MXresult || $Aresult;
66
    }
67
}
68