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

CsrfExtension::csrfFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
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