DateParamRequestListener::onKernelRequest()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Alchemy\RestBundle\EventListener;
4
5
use Alchemy\Rest\Request\DateParser;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
8
use Symfony\Component\HttpKernel\KernelEvents;
9
10
class DateParamRequestListener implements EventSubscriberInterface
11
{
12
13
    /**
14
     * @var DateParser
15
     */
16
    private $dateParser;
17
18
    /**
19
     * @param DateParser $parser
20
     */
21 4
    public function __construct(DateParser $parser = null)
22
    {
23 4
        $this->dateParser = $parser ?: new DateParser\FormatDateParser();
24 4
    }
25
26
    /**
27
     * @param GetResponseEvent $event
28
     */
29 4
    public function onKernelRequest(GetResponseEvent $event)
30
    {
31 4
        $request = $event->getRequest();
32 4
        $attributes = $request->attributes;
33
34 4
        if (! $attributes->has('_dates')) {
35 2
            return;
36
        }
37
38 2
        $dateKeys = $attributes->get('_dates', array(), true);
39
40 2
        foreach ($dateKeys as $key) {
41 2
            $rawValue = $request->get($key, null);
42 2
            $parsedValue = $this->dateParser->parseDate($rawValue);
43
44 2
            $request->attributes->set($key, $parsedValue);
45 2
        }
46 2
    }
47
48 2
    public static function getSubscribedEvents()
49
    {
50
        return array(
51 2
            KernelEvents::REQUEST => array('onKernelRequest', -2048)
52 2
        );
53
    }
54
}
55