PaginationParamRequestListener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 44
ccs 19
cts 19
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onKernelRequest() 0 18 3
A getSubscribedEvents() 0 6 1
1
<?php
2
3
namespace Alchemy\RestBundle\EventListener;
4
5
use Alchemy\RestBundle\Rest\Request\PaginationOptionsFactory;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
8
use Symfony\Component\HttpKernel\KernelEvents;
9
10
class PaginationParamRequestListener implements EventSubscriberInterface
11
{
12
    /**
13
     * @var PaginationOptionsFactory
14
     */
15
    private $factory;
16
17
    /**
18
     * @param PaginationOptionsFactory $factory
19
     */
20 4
    public function __construct(PaginationOptionsFactory $factory)
21
    {
22 4
        $this->factory = $factory;
23 4
    }
24
25
    /**
26
     * @param GetResponseEvent $event
27
     */
28 4
    public function onKernelRequest(GetResponseEvent $event)
29
    {
30 4
        $request = $event->getRequest();
31 4
        $attributes = $request->attributes;
32
33 4
        if (($config = $attributes->get('_paginate', false)) === false) {
34 2
            return;
35
        }
36
37 2
        if (! is_array($config)) {
38 2
            $config = array();
39 2
        }
40
41 2
        $request->attributes->set(
42 2
            'pagination',
43 2
            $this->factory->create($request->query->all(), $config)
44 2
        );
45 2
    }
46
47 2
    public static function getSubscribedEvents()
48
    {
49
        return array(
50 2
            KernelEvents::REQUEST => array('onKernelRequest', -2048)
51 2
        );
52
    }
53
}
54