Failed Conditions
Push — issue#797 ( 58f434...96dbc7 )
by Guilherme
04:33
created

EmailValidator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 30 8
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
 * @codeCoverageIgnore
24
 */
25
class EmailValidator extends \Symfony\Component\Validator\Constraints\EmailValidator
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function validate($value, Constraint $constraint)
31
    {
32
        if (null === $value || '' === $value) {
33
            return;
34
        }
35
36
        if ($constraint->strict) {
37
            if (!class_exists('\Egulias\EmailValidator\EmailValidator') || !class_exists('\Egulias\EmailValidator\Validation\RFCValidation')) {
38
                throw new RuntimeException('Strict email validation requires egulias/email-validator');
39
            }
40
41
            $strictValidator = new \Egulias\EmailValidator\EmailValidator();
42
43
            if (!$strictValidator->isValid($value, new RFCValidation())) {
44
                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...
45
                    $this->context->buildViolation($constraint->message)
46
                        ->setParameter('{{ value }}', $this->formatValue($value))
47
                        ->setCode(Email::INVALID_FORMAT_ERROR)
48
                        ->addViolation();
49
                } else {
50
                    $this->buildViolation($constraint->message)
51
                        ->setParameter('{{ value }}', $this->formatValue($value))
52
                        ->setCode(Email::INVALID_FORMAT_ERROR)
53
                        ->addViolation();
54
                }
55
56
                return;
57
            }
58
        } else {
59
            parent::validate($value, $constraint);
60
        }
61
    }
62
}
63