Completed
Push — master ( 2c69ad...5eb3ba )
by Alexis
01:46
created

CsrfExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Security\TwigExtension;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Twig\Extension\AbstractExtension;
7
use Twig\TwigFunction;
8
9
class CsrfExtension extends AbstractExtension
10
{
11
    /**
12
     * @var string
13
     */
14
    private $csrfName;
15
16
    /**
17
     * @var string
18
     */
19
    private $csrfValue;
20
21
    public function __construct(ServerRequestInterface $request)
22
    {
23
        $this->csrfName = $request->getAttribute('csrf_name');
24
        $this->csrfValue = $request->getAttribute('csrf_value');
25
    }
26
27
    public function getFunctions()
28
    {
29
        return [
30
            new TwigFunction('csrf', [$this, 'csrf'], ['is_safe' => ['html']])
31
        ];
32
    }
33
34
    public function csrf()
35
    {
36
        return '
37
            <input type="hidden" name="csrf_name" value="' . $this->csrfName . '">
38
            <input type="hidden" name="csrf_value" value="' . $this->csrfValue . '">
39
        ';
40
    }
41
}
42