CsrfListener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A onKernelRequest() 0 20 4
1
<?php
2
3
namespace Knp\RadBundle\EventListener;
4
5
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
6
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
7
8
class CsrfListener
9
{
10
    private $csrfProvider;
11
    private $intention;
12
13
    public function __construct(CsrfProviderInterface $csrfProvider, $intention = 'link')
14
    {
15
        $this->csrfProvider = $csrfProvider;
16
        $this->intention    = $intention;
17
    }
18
19
    public function onKernelRequest(GetResponseEvent $event)
20
    {
21
        $request = $event->getRequest();
22
        if (false === $request->attributes->get('_check_csrf', false)) {
23
            return;
24
        }
25
        if (!$request->request->has('_link_token')) {
26
            throw new \InvalidArgumentException(
27
                'The CSRF token verification is activated but you did not send a token. Please submit a request with a valid csrf token.'
28
            );
29
        }
30
31
        $token = $request->request->get('_link_token');
32
33
        if (!$this->csrfProvider->isCsrfTokenValid($this->intention, $token)) {
34
            throw new \InvalidArgumentException(
35
                'The CSRF token is invalid. Please submit a request with a valid csrf token.'
36
            );
37
        }
38
    }
39
}
40