MailMxValidation::isHostValid()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 3
eloc 2
nc 3
nop 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Imedia\Ammit\Domain;
5
6
/**
7
 * @author Guillaume MOREL <[email protected]>
8
 */
9
class MailMxValidation
10
{
11
    public function isEmailHostValid(string $emailAddress): bool
12
    {
13
        $host = substr($emailAddress, strrpos($emailAddress, '@') + 1);
14
15
        return $this->isHostValid($host);
16
    }
17
18
    public function isEmailFormatValid(string $emailAddress): bool
19
    {
20
        if (! filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return (bool) filter_var...FILTER_VALIDATE_EMAIL);.
Loading history...
21
22
            return false;
23
        }
24
25
        return true;
26
    }
27
28
    /**
29
     * Check DNS Records for MX type.
30
     */
31
    private static function isMxValid(string $host): bool
32
    {
33
        return checkdnsrr($host, 'MX');
34
    }
35
36
    /**
37
     * Check if one of MX, A or AAAA DNS RR exists.
38
     */
39
    private function isHostValid(string $host): bool
40
    {
41
        return $this->isMxValid($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA'));
42
    }
43
}
44