CsrfConstraintValidator::validate()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
ccs 7
cts 8
cp 0.875
rs 10
cc 4
nc 3
nop 2
crap 4.0312
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