DateParamRequestListener   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 45
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A onKernelRequest() 0 18 3
A getSubscribedEvents() 0 6 1
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