Completed
Push — master ( 42db1b...a35559 )
by Paweł
16s
created

Paginator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 69
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C paginate() 0 61 11
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2016 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2016 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Pagination;
18
19
use Knp\Component\Pager\Paginator as BasePaginator;
20
use Knp\Component\Pager\Event;
21
22
class Paginator extends BasePaginator
23
{
24
    /**
25
     * {@inheritdoc}
26
     *
27
     * IMPORTANT: This method expects OFFSET value instead PAGE
28
     */
29
    public function paginate($target, $offset = 1, $limit = 10, array $options = array())
0 ignored issues
show
Coding Style introduced by
paginate uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
30
    {
31
        $limit = intval(abs($limit));
32
        if (!$limit) {
33
            throw new \LogicException('Invalid item per page number, must be a positive number');
34
        }
35
36
        $page = intval(round($offset / $limit), 10);
37
        if ($page < 1) {
38
            $page = 1;
39
        }
40
        $options = array_merge($this->defaultOptions, $options);
41
42
        // normalize default sort field
43
        if (isset($options['defaultSortFieldName']) && is_array($options['defaultSortFieldName'])) {
44
            $options['defaultSortFieldName'] = implode('+', $options['defaultSortFieldName']);
45
        }
46
47
        // default sort field and direction are set based on options (if available)
48
        if (!isset($_GET[$options['sortFieldParameterName']]) && isset($options['defaultSortFieldName'])) {
49
            $_GET[$options['sortFieldParameterName']] = $options['defaultSortFieldName'];
50
51
            if (!isset($_GET[$options['sortDirectionParameterName']])) {
52
                $_GET[$options['sortDirectionParameterName']] = isset($options['defaultSortDirection']) ? $options['defaultSortDirection'] : 'asc';
53
            }
54
        }
55
56
        // before pagination start
57
        $beforeEvent = new Event\BeforeEvent($this->eventDispatcher);
58
        $this->eventDispatcher->dispatch('knp_pager.before', $beforeEvent);
59
        // items
60
        $itemsEvent = new Event\ItemsEvent($offset, $limit);
61
        $itemsEvent->options = &$options;
62
        $itemsEvent->target = &$target;
63
        $this->eventDispatcher->dispatch('knp_pager.items', $itemsEvent);
64
        if (!$itemsEvent->isPropagationStopped()) {
65
            throw new \RuntimeException('One of listeners must count and slice given target');
66
        }
67
        // pagination initialization event
68
        $paginationEvent = new Event\PaginationEvent();
69
        $paginationEvent->target = &$target;
70
        $paginationEvent->options = &$options;
71
        $this->eventDispatcher->dispatch('knp_pager.pagination', $paginationEvent);
72
        if (!$paginationEvent->isPropagationStopped()) {
73
            throw new \RuntimeException('One of listeners must create pagination view');
74
        }
75
        // pagination class can be different, with different rendering methods
76
        $paginationView = $paginationEvent->getPagination();
77
        $paginationView->setCustomParameters($itemsEvent->getCustomPaginationParameters());
78
        $paginationView->setCurrentPageNumber($page);
79
        $paginationView->setItemNumberPerPage($limit);
80
        $paginationView->setTotalItemCount($itemsEvent->count);
81
        $paginationView->setPaginatorOptions($options);
0 ignored issues
show
Documentation introduced by
$options is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
        $paginationView->setItems($itemsEvent->items);
83
84
        // after
85
        $afterEvent = new Event\AfterEvent($paginationView);
86
        $this->eventDispatcher->dispatch('knp_pager.after', $afterEvent);
87
88
        return $paginationView;
89
    }
90
}
91