Completed
Branch master (928280)
by Alexis
02:27
created

CsrfExtension   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFunctions() 0 6 1
A csrfFields() 0 7 1
1
<?php
2
3
namespace App\Twig;
4
5
use Slim\Csrf\Guard;
6
use Twig\Extension\AbstractExtension;
7
use Twig\TwigFunction;
8
9
class CsrfExtension extends AbstractExtension
10
{
11
    /**
12
     * @var Guard
13
     */
14
    protected $csrf;
15
16
    /**
17
     * Constructor.
18
     *
19
     * @param Guard $csrf
20
     */
21
    public function __construct(Guard $csrf)
22
    {
23
        $this->csrf = $csrf;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getFunctions()
30
    {
31
        return [
32
            new TwigFunction('csrf', [$this, 'csrfFields'], ['is_safe' => ['html']])
33
        ];
34
    }
35
36
    /**
37
     * Gets the HTML for the CSRF fields.
38
     *
39
     * @return string
40
     */
41
    public function csrfFields()
42
    {
43
        return '
44
            <input type="hidden" name="' . $this->csrf->getTokenNameKey() . '" value="' . $this->csrf->getTokenName() . '">
45
            <input type="hidden" name="' . $this->csrf->getTokenValueKey() . '" value="' . $this->csrf->getTokenValue() . '">
46
        ';
47
    }
48
}
49