BannedListValidator::validate()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 4
nc 3
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 a list of banned domains
11
 *
12
 * This validator checks if the domain of an email address is in a list of banned domains.
13
 * The validation is only performed if enabled in the policy. Domain matching supports
14
 * wildcard patterns for flexible banning rules.
15
 */
16
class BannedListValidator extends AValidator
17
{
18
    /**
19
     * Validates an email address against the banned domains list
20
     *
21
     * @param EmailAddress $email The email address to validate
22
     * @return bool True if the domain is not banned or validation is disabled, false if the domain is banned
23
     */
24
    public function validate(EmailAddress $email): bool
25
    {
26
        if ($this->policy->checkBannedListedEmail()) {
27
            $domain = $email->getDomain();
28
            foreach ($this->policy->getBannedList() as $bannedDomain) {
29
                if (fnmatch($bannedDomain, $domain ?? '')) {
30
                    return false;
31
                }
32
            }
33
        }
34
        return true;
35
    }
36
}
37