SlidingPaginationSubscriber::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Saxulum\PaginationProvider\Subscriber;
4
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Knp\Component\Pager\Event\PaginationEvent;
7
use Saxulum\PaginationProvider\Pagination\SlidingPagination;
8
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
9
use Symfony\Component\HttpKernel\HttpKernelInterface;
10
11
class SlidingPaginationSubscriber implements EventSubscriberInterface
12
{
13
    private $route;
14
    private $params = array();
15
    private $options;
16
17
    public function __construct(array $options)
18
    {
19
        $this->options = $options;
20
    }
21
22
    public function onKernelRequest(GetResponseEvent $event)
23
    {
24
        if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
25
            return;
26
        }
27
28
        $request = $event->getRequest();
29
30
        $this->route = $request->attributes->get('_route');
31
        $this->params = array_merge($request->query->all(), $request->attributes->all());
32
        foreach ($this->params as $key => $param) {
33
            if (substr($key, 0, 1) == '_') {
34
                unset($this->params[$key]);
35
            }
36
        }
37
    }
38
39
    public function pagination(PaginationEvent $event)
40
    {
41
        // default sort field and order
42
        $eventOptions = $event->options;
43
44 View Code Duplication
        if (isset($eventOptions['defaultSortFieldName']) && !isset($this->params[$eventOptions['sortFieldParameterName']])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
            $this->params[$eventOptions['sortFieldParameterName']] = $eventOptions['defaultSortFieldName'];
46
        }
47
48 View Code Duplication
        if (isset($eventOptions['defaultSortDirection']) && !isset($this->params[$eventOptions['sortDirectionParameterName']])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
            $this->params[$eventOptions['sortDirectionParameterName']] = $eventOptions['defaultSortDirection'];
50
        }
51
52
        $pagination = new SlidingPagination($this->params);
53
54
        $pagination->setUsedRoute($this->route);
55
        $pagination->setTemplate($this->options['defaultPaginationTemplate']);
56
        $pagination->setSortableTemplate($this->options['defaultSortableTemplate']);
57
        $pagination->setFiltrationTemplate($this->options['defaultFiltrationTemplate']);
58
        $pagination->setPageRange($this->options['defaultPageRange']);
59
60
        $event->setPagination($pagination);
61
        $event->stopPropagation();
62
    }
63
64
    public static function getSubscribedEvents()
65
    {
66
        return array(
67
            'knp_pager.pagination' => array('pagination', 1)
68
        );
69
    }
70
}
71