FreeEmailValidator::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 free email providers
11
 *
12
 * This validator checks if an email address is from a known free email provider.
13
 * It maintains a list of free email domains from multiple sources and can be
14
 * configured to use only local lists or fetch from remote sources.
15
 */
16
class FreeEmailValidator extends AProviderValidator
17
{
18
    /**
19
     * Array of client-provided free email providers
20
     *
21
     * @var array<string> Array of client-provided free email providers
22
     */
23
    protected array $freeEmailListProviders = [];
24
25
    /**
26
     * Array of URLs containing lists of free email addresses and their formats
27
     *
28
     * @var array<array{format: string, url: string}> Array of URLs containing a list of free email addresses and the format of that list
29
     */
30
    protected static array $providers = [
31
        [
32
            'format' => 'txt',
33
            'url' => 'https://gist.githubusercontent.com/tbrianjones/5992856/raw/93213efb652749e226e69884d6c048e595c1280a/free_email_provider_domains.txt'
34
        ],
35
    ];
36
37
    /**
38
     * Validates an email address against known free email providers
39
     *
40
     * Checks if validating against free email domains is enabled. If so, gets the list of email domains
41
     * and checks if the domain is one of them.
42
     *
43
     * @param EmailAddress $email The email address to validate
44
     * @return bool True if the domain is not a free email provider or validation is disabled, false if it is a free provider
45
     */
46
    public function validate(EmailAddress $email): bool
47
    {
48
        if (!$this->policy->checkFreeEmail()) {
49
            return true;
50
        }
51
52
        if ($this->freeEmailListProviders === []) {
53
            $this->freeEmailListProviders = $this->getList(
54
                $this->policy->checkFreeLocalListOnly(),
55
                $this->policy->getFreeList()
56
            );
57
        }
58
59
        $domain = $email->getDomain();
60
        if ($domain === null) {
61
            return true;
62
        }
63
64
        return !in_array($domain, $this->freeEmailListProviders, true);
65
    }
66
}
67