Failed Conditions
Pull Request — master (#262)
by Guilherme
10:25 queued 04:26
created

EmailValidator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 26 6
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 ($constraint->strict) {
33
            if (!class_exists('\Egulias\EmailValidator\EmailValidator') || !class_exists('\Egulias\EmailValidator\Validation\RFCValidation')) {
34
                throw new RuntimeException('Strict email validation requires egulias/email-validator');
35
            }
36
37
            $strictValidator = new \Egulias\EmailValidator\EmailValidator();
38
39
            if (!$strictValidator->isValid($value, new RFCValidation())) {
40
                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...
41
                    $this->context->buildViolation($constraint->message)
42
                        ->setParameter('{{ value }}', $this->formatValue($value))
43
                        ->setCode(Email::INVALID_FORMAT_ERROR)
44
                        ->addViolation();
45
                } else {
46
                    $this->buildViolation($constraint->message)
47
                        ->setParameter('{{ value }}', $this->formatValue($value))
48
                        ->setCode(Email::INVALID_FORMAT_ERROR)
49
                        ->addViolation();
50
                }
51
52
                return;
53
            }
54
        } else {
55
            parent::validate($value, $constraint);
56
        }
57
    }
58
}
59