Csrf   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 89
ccs 24
cts 24
cp 1
rs 10
c 1
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 38 1
A generateCodeForFormBuilder() 0 23 4
A applyOnFormBuilder() 0 14 4
1
<?php
2
3
namespace Bdf\Form\Attribute\Form;
4
5
use Attribute;
6
use Bdf\Form\Aggregate\FormBuilderInterface;
7
use Bdf\Form\Attribute\AttributeForm;
8
use Bdf\Form\Attribute\Processor\CodeGenerator\AttributesProcessorGenerator;
9
use Bdf\Form\Csrf\CsrfElementBuilder;
10
11
/**
12
 * Add a CSRF check on the form
13
 *
14
 * It's also possible to define a property of type `CsrfElement`, but configuration of message, tokenId and invalidate
15
 * will not be possible.
16
 *
17
 * Note: this attribute is not repeatable
18
 *
19
 * This attribute is equivalent to call :
20
 * <code>
21
 * $builder->csrf('csrf')->message('Token invalide');
22
 * </code>
23
 *
24
 * Usage:
25
 * <code>
26
 * #[Csrf(name: 'csrf', message: 'Token invalide')]
27
 * class MyForm extends AttributeForm
28
 * {
29
 * }
30
 * </code>
31
 *
32
 * @see FormBuilderInterface::csrf() The called method
33
 * @see CsrfElementBuilder
34
 */
35
#[Attribute(Attribute::TARGET_CLASS)]
36
final class Csrf implements FormBuilderAttributeInterface
37
{
38 11
    public function __construct(
39
        /**
40
         * The token input name
41
         *
42
         * @var non-empty-string
43
         * @readonly
44
         */
45
        private string $name = '_token',
46
        /**
47
         * The token id
48
         * By default is value is the class name of `CsrfElement`
49
         *
50
         * @var string|null
51
         * @readonly
52
         *
53
         * @see CsrfTokenManagerInterface::getToken() The parameter tokenId will be used as parameter of this method
54
         * @see CsrfElementBuilder::tokenId() The called method if defined
55
         */
56
        private ?string $tokenId = null,
57
        /**
58
         * The error message to display if the token do not correspond
59
         *
60
         * @var string|null
61
         * @readonly
62
         *
63
         * @see CsrfElementBuilder::message() The called method if defined
64
         */
65
        private ?string $message = null,
66
        /**
67
         * Always invalidate the CSRF token after submission
68
         *
69
         * @var bool|null
70
         * @readonly
71
         *
72
         * @see CsrfElementBuilder::invalidate() The called method if defined
73
         */
74
        private ?bool $invalidate = null,
75
    ) {
76 11
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 5
    public function applyOnFormBuilder(AttributeForm $form, FormBuilderInterface $builder): void
82
    {
83 5
        $csrf = $builder->csrf($this->name);
84
85 5
        if ($this->tokenId !== null) {
86 1
            $csrf->tokenId($this->tokenId);
87
        }
88
89 5
        if ($this->message !== null) {
90 1
            $csrf->message($this->message);
91
        }
92
93 5
        if ($this->invalidate !== null) {
94 1
            $csrf->invalidate($this->invalidate);
95
        }
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 6
    public function generateCodeForFormBuilder(AttributesProcessorGenerator $generator, AttributeForm $form): void
102
    {
103 6
        $parameters = [$this->name];
104 6
        $line = '$builder->csrf(?)';
105
106 6
        if ($this->tokenId !== null) {
107 2
            $line .= '->tokenId(?)';
108 2
            $parameters[] = $this->tokenId;
109
        }
110
111 6
        if ($this->message !== null) {
112 2
            $line .= '->message(?)';
113 2
            $parameters[] = $this->message;
114
        }
115
116 6
        if ($this->invalidate !== null) {
117 2
            $line .= '->invalidate(?)';
118 2
            $parameters[] = $this->invalidate;
119
        }
120
121 6
        $line .= ';';
122
123 6
        $generator->line($line, $parameters);
124
    }
125
}
126