DisposableEmailValidator::validate()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 19
rs 9.9332
c 1
b 0
f 0
cc 4
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EmailValidator\Validator;
6
7
use EmailValidator\EmailAddress;
8
9
/**
10
 * Validates email addresses against known disposable email providers
11
 *
12
 * This validator checks if an email address is from a known disposable email provider.
13
 * It maintains a list of disposable email domains from multiple sources and can be
14
 * configured to use only local lists or fetch from remote sources.
15
 */
16
class DisposableEmailValidator extends AProviderValidator
17
{
18
    /**
19
     * Array of client-provided disposable email providers
20
     *
21
     * @var array<string> Array of client-provided disposable email providers
22
     */
23
    protected array $disposableEmailListProviders = [];
24
25
    /**
26
     * Array of URLs containing lists of disposable email addresses and their formats
27
     *
28
     * @var array<array{format: string, url: string}> Array of URLs containing a list of disposable email addresses and the format of that list
29
     */
30
    protected static array $providers = [
31
        [
32
            'format' => 'txt',
33
            'url' => 'https://raw.githubusercontent.com/martenson/disposable-email-domains/master/disposable_email_blocklist.conf'
34
        ],
35
        [
36
            'format' => 'json',
37
            'url' => 'https://raw.githubusercontent.com/ivolo/disposable-email-domains/master/wildcard.json'
38
        ],
39
    ];
40
41
    /**
42
     * Validates an email address against known disposable email providers
43
     *
44
     * Checks if validating against disposable domains is enabled. If so, gets the list of disposable domains
45
     * and checks if the domain is one of them.
46
     *
47
     * @param EmailAddress $email The email address to validate
48
     * @return bool True if the domain is not a disposable email provider or validation is disabled, false if it is a disposable provider
49
     */
50
    public function validate(EmailAddress $email): bool
51
    {
52
        if (!$this->policy->checkDisposableEmail()) {
53
            return true;
54
        }
55
56
        if ($this->disposableEmailListProviders === []) {
57
            $this->disposableEmailListProviders = $this->getList(
58
                $this->policy->checkDisposableLocalListOnly(),
59
                $this->policy->getDisposableList()
60
            );
61
        }
62
63
        $domain = $email->getDomain();
64
        if ($domain === null) {
65
            return true;
66
        }
67
68
        return !in_array($domain, $this->disposableEmailListProviders, true);
69
    }
70
}
71