CsrfConstraintValidator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 1
b 0
f 0
dl 0
loc 16
ccs 7
cts 8
cp 0.875
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 11 4
1
<?php
2
3
namespace Bdf\Form\Csrf;
4
5
use Symfony\Component\Security\Csrf\CsrfToken;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
9
10
/**
11
 * @internal
12
 */
13
class CsrfConstraintValidator extends ConstraintValidator
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18 12
    public function validate($value, Constraint $constraint)
19
    {
20 12
        if (!$constraint instanceof CsrfConstraint) {
21
            throw new UnexpectedTypeException($constraint, CsrfConstraint::class);
22
        }
23
24 12
        if (!$value instanceof CsrfToken || !$constraint->manager->isTokenValid($value)) {
25 10
            $this->context->buildViolation($constraint->message)
26 10
                ->setParameter('{{ value }}', $this->formatValue($value))
27 10
                ->setCode(CsrfConstraint::INVALID_TOKEN_ERROR)
28 10
                ->addViolation()
29
            ;
30
        }
31 12
    }
32
}
33