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

CsrfExtension   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 33
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFunctions() 0 6 1
A csrf() 0 7 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