EmailRule::validate()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.0729

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 4
nop 1
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
crap 5.0729
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Validator\Rule;
13
14
/**
15
 * Check for email
16
 *
17
 * @package Bluz\Validator\Rule
18
 */
19
class EmailRule extends AbstractRule
20
{
21
    /**
22
     * @var string error template
23
     */
24
    protected $description = 'must be a valid email address';
25
26
    /**
27
     * @var bool check DNS record flag
28
     */
29
    protected $checkDns;
30
31
    /**
32
     * Setup validation rule
33
     *
34
     * @param bool $checkDns
35
     */
36 17
    public function __construct(bool $checkDns = false)
37
    {
38 17
        $this->checkDns = $checkDns;
39 17
    }
40
41
    /**
42
     * Check input data
43
     *
44
     * @param  mixed $input
45
     *
46
     * @return bool
47
     */
48 17
    public function validate($input): bool
49
    {
50 17
        if (is_string($input) && filter_var($input, FILTER_VALIDATE_EMAIL)) {
51 6
            [, $domain] = explode('@', $input, 2);
52 6
            if ($this->checkDns) {
53
                return checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A');
54
            }
55 6
            return true;
56
        }
57 11
        return false;
58
    }
59
}
60