Passed
Push — issue#785 ( 9749bb...a56237 )
by Guilherme
04:58
created

EmailValidator::validate()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 5
nop 2
dl 0
loc 26
ccs 0
cts 23
cp 0
crap 42
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\ValidationBundle\Validator\Constraints;
12
13
use Egulias\EmailValidator\Validation\RFCValidation;
14
use Symfony\Component\Validator\Constraint;
15
use Symfony\Component\Validator\Context\ExecutionContextInterface;
16
use Symfony\Component\Validator\Exception\RuntimeException;
17
18
/**
19
 * Class EmailValidator
20
 * @package LoginCidadao\ValidationBundle\Validator\Constraints
21
 *
22
 * TODO: remove after update to Symfony 4.1
23
 */
24
class EmailValidator extends \Symfony\Component\Validator\Constraints\EmailValidator
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function validate($value, Constraint $constraint)
30
    {
31
        if ($constraint->strict) {
32
            if (!class_exists('\Egulias\EmailValidator\EmailValidator') || !class_exists('\Egulias\EmailValidator\Validation\RFCValidation')) {
33
                throw new RuntimeException('Strict email validation requires egulias/email-validator');
34
            }
35
36
            $strictValidator = new \Egulias\EmailValidator\EmailValidator();
37
38
            if (!$strictValidator->isValid($value, new RFCValidation())) {
39
                if ($this->context instanceof ExecutionContextInterface) {
0 ignored issues
show
introduced by
$this->context is always a sub-type of Symfony\Component\Valida...ecutionContextInterface. If $this->context can have other possible types, add them to vendor/symfony/symfony/s...ConstraintValidator.php:37.
Loading history...
40
                    $this->context->buildViolation($constraint->message)
41
                        ->setParameter('{{ value }}', $this->formatValue($value))
42
                        ->setCode(Email::INVALID_FORMAT_ERROR)
43
                        ->addViolation();
44
                } else {
45
                    $this->buildViolation($constraint->message)
46
                        ->setParameter('{{ value }}', $this->formatValue($value))
47
                        ->setCode(Email::INVALID_FORMAT_ERROR)
48
                        ->addViolation();
49
                }
50
51
                return;
52
            }
53
        } else {
54
            parent::validate($value, $constraint);
55
        }
56
    }
57
}
58