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 AmmitPhp\Ammit\Domain;
5
6
class MailMxValidation
7
{
8
    public function isEmailHostValid(string $emailAddress): bool
9
    {
10
        $host = substr($emailAddress, strrpos($emailAddress, '@') + 1);
11
12
        return $this->isHostValid($host);
13
    }
14
15
    public function isEmailFormatValid(string $emailAddress): bool
16
    {
17
        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...
18
19
            return false;
20
        }
21
22
        return true;
23
    }
24
25
    /**
26
     * Check DNS Records for MX type.
27
     */
28
    private static function isMxValid(string $host): bool
29
    {
30
        return checkdnsrr($host, 'MX');
31
    }
32
33
    /**
34
     * Check if one of MX, A or AAAA DNS RR exists.
35
     */
36
    private function isHostValid(string $host): bool
37
    {
38
        return $this->isMxValid($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA'));
39
    }
40
}
41