Completed
Push — master ( bf7162...7a0230 )
by Oskar
06:03
created

CsrfDisablingExtension::getExtendedType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Dkplus\CsrfApiUnprotectionBundle;
3
4
use Dkplus\CsrfApiUnprotectionBundle\UnprotectionRule\UnprotectionRule;
5
use Symfony\Component\Form\AbstractTypeExtension;
6
use Symfony\Component\Form\Extension\Core\Type\FormType;
7
use Symfony\Component\HttpFoundation\RequestStack;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
final class CsrfDisablingExtension extends AbstractTypeExtension
11
{
12
    /** @var RequestStack */
13
    private $requests;
14
15
    /** @var UnprotectionRule */
16
    private $unprotectionRule;
17
18
    public static function getExtendedTypes()
19
    {
20
        return [FormType::class];
21
    }
22
23
    /**
24
     * @param RequestStack   $requests
25
     * @param UnprotectionRule $unprotectionRule
26
     */
27 4
    public function __construct(RequestStack $requests, UnprotectionRule $unprotectionRule)
28
    {
29 4
        $this->requests         = $requests;
30 4
        $this->unprotectionRule = $unprotectionRule;
31 4
    }
32
33 1
    public function getExtendedType()
34
    {
35 1
        return FormType::class;
36
    }
37
38 3
    public function configureOptions(OptionsResolver $resolver)
39
    {
40 3
        if ($this->requests->getMasterRequest()
41 3
            && ! $this->unprotectionRule->matches($this->requests->getMasterRequest())
42
        ) {
43 1
            return;
44
        }
45
46 2
        $resolver->setDefaults(['csrf_protection' => false]);
47 2
    }
48
}
49