SlidingPaginationSubscriber   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 10 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 4
dl 6
loc 60
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onKernelRequest() 0 16 4
B pagination() 6 24 5
A getSubscribedEvents() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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