Passed
Branch master (3daac1)
by Vincent
07:53
created

CsrfValueValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Bdf\Form\Csrf;
4
5
use Bdf\Form\ElementInterface;
6
use Bdf\Form\Error\FormError;
7
use Bdf\Form\Validator\ConstraintValueValidator;
8
use Bdf\Form\Validator\ValueValidatorInterface;
9
use Symfony\Component\Validator\Constraint;
10
11
/**
12
 * Class CsrfValueValidator
13
 */
14
final class CsrfValueValidator implements ValueValidatorInterface
15
{
16
    /**
17
     * Invalidate the token after verification ?
18
     *
19
     * @var boolean
20
     */
21
    private $invalidate;
22
23
    /**
24
     * The constraint options
25
     *
26
     * @var array
27
     */
28
    private $options;
29
30
    /**
31
     * CsrfValueValidator constructor.
32
     *
33
     * @param bool $invalidate Always invalidate the token after validation
34
     * @param array $options Constraints options
35
     */
36 21
    public function __construct(bool $invalidate = false, array $options = [])
37
    {
38 21
        $this->invalidate = $invalidate;
39 21
        $this->options = $options;
40 21
    }
41
42
    /**
43
     * {@inheritdoc}
44
     *
45
     * @param CsrfElement $element
46
     * @psalm-suppress MoreSpecificImplementedParamType
47
     */
48 11
    public function validate($value, ElementInterface $element): FormError
49
    {
50
        try {
51 11
            return (new ConstraintValueValidator([new CsrfConstraint($this->options + ['manager' => $element->getTokenManager()])]))->validate($value, $element);
0 ignored issues
show
Bug introduced by
The method getTokenManager() does not exist on Bdf\Form\ElementInterface. It seems like you code against a sub-type of Bdf\Form\ElementInterface such as Bdf\Form\Csrf\CsrfElement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
            return (new ConstraintValueValidator([new CsrfConstraint($this->options + ['manager' => $element->/** @scrutinizer ignore-call */ getTokenManager()])]))->validate($value, $element);
Loading history...
52
        } finally {
53 11
            if ($this->invalidate) {
54 11
                $element->invalidateToken();
0 ignored issues
show
Bug introduced by
The method invalidateToken() does not exist on Bdf\Form\ElementInterface. It seems like you code against a sub-type of Bdf\Form\ElementInterface such as Bdf\Form\Csrf\CsrfElement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
                $element->/** @scrutinizer ignore-call */ 
55
                          invalidateToken();
Loading history...
55
            }
56
        }
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function constraints(): array
63
    {
64
        return []; // Does CsrfConstraint should be returns ?
65
    }
66
}
67