NotTemporaryEmailValidator::validate()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 16
cts 16
cp 1
rs 8.9197
cc 4
eloc 12
nc 4
nop 2
crap 4
1
<?php
2
3
namespace EmanueleMinotto\TemporaryEmailValidator;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\ConstraintValidator;
7
8
/**
9
 * Validator for temporary emails.
10
 */
11
class NotTemporaryEmailValidator extends ConstraintValidator
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 4242
    public function validate($value, Constraint $constraint)
17
    {
18 4242
        if (empty(trim($value))) {
19 6
            return;
20
        }
21
22 4236
        $domains = json_decode(
23 4236
            file_get_contents(__DIR__.'/../data/domains.json'),
24 1412
            true
25 2824
        );
26
27 4236
        foreach ($domains as $domain) {
28 4236
            if (preg_match('/@'.preg_quote($domain).'$/', $value)) {
29 4230
                $this->context->addViolation($constraint->message, [
30 4230
                    '%email%' => $value,
31 4230
                    '%domain%' => $domain,
32 2820
                ]);
33
34 4232
                return;
35
            }
36 2822
        }
37 6
    }
38
}
39