Paginator   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 79.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 102
ccs 46
cts 58
cp 0.7931
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B pagination() 0 36 6
D getPages() 0 30 12
1
<?php
2
3
namespace Pagination\Util;
4
5
use Silex\Application;
6
7
class Paginator
8
{
9
    /**
10
     * @var Application
11
     */
12
    private $app;
13
14
    /**
15
     * @var array
16
     */
17
    private $options;
18
19
    /**
20
     * @param Application $app
21
     */
22 3
    public function __construct(Application $app)
23
    {
24 3
        $this->app = $app;
25 3
    }
26
27
    /**
28
     * @param \Doctrine\ORM\QueryBuilder|array $query
29
     * @param array                      $options
30
     *
31
     * @throws \Pagination\Exception\UnknownAdapterException
32
     *
33
     * @return array
34
     */
35 2
    public function pagination($query, $options = [])
36
    {
37 2
        $this->options = array_replace($this->app['paginator.options'], $options);
38
39 2
        switch (true) {
40 2
            case $query instanceof \Doctrine\ORM\QueryBuilder:
0 ignored issues
show
Bug introduced by
The class Doctrine\ORM\QueryBuilder does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
41
                $adapter = new \Pagination\Adapter\PaginationORMAdapter();
42
                break;
43 2
            case is_array($query):
44 1
                $adapter = new \Pagination\Adapter\PaginationArrayAdapter();
45 1
                break;
46 1
            default:
47 1
                throw new \Pagination\Exception\UnknownAdapterException();
48 1
        }
49
50 1
        $current = 1;
51 1
        if ($this->app['request_stack']->getCurrentRequest()) {
52
            $current = $this->app['request_stack']->getCurrentRequest()->get('page', 1);
53
        }
54 1
        $counter = $adapter->getCounter($query);
0 ignored issues
show
Bug introduced by
It seems like $query defined by parameter $query on line 35 can also be of type array; however, Pagination\Adapter\Pagin...RMAdapter::getCounter() does only seem to accept object<Doctrine\ORM\QueryBuilder>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
55
56 1
        $items = ceil($counter / $this->options['items_per_page']);
57
58
        return [
59 1
            'first'      => 1,
60 1
            'prev'       => $current > 1 ? $current - 1 : null,
61 1
            'current'    => $current,
62 1
            'items'      => $adapter->getItems($query, $current, $this->options['items_per_page']),
0 ignored issues
show
Bug introduced by
It seems like $query defined by parameter $query on line 35 can also be of type array; however, Pagination\Adapter\Pagin...nORMAdapter::getItems() does only seem to accept object<Doctrine\ORM\QueryBuilder>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
63 1
            'pages'      => $this->getPages($current, $items),
64 1
            'next'       => $current < $items ? $current + 1 : null,
65 1
            'last'       => $items,
66 1
            'total'      => $counter,
67 1
            'total_page' => $items,
68 1
            'options'    => $this->options,
69 1
        ];
70
    }
71
72
    /**
73
     * @param int $current
74
     * @param int $items
75
     *
76
     * @return array
77
     */
78 1
    public function getPages($current, $items)
79
    {
80 1
        $start = $current - $this->options['offset_page'] > 1 ? $current - $this->options['offset_page'] : 1;
81 1
        $end = $current + $this->options['offset_page'] < $items ? $current + $this->options['offset_page'] : $items;
82
83 1
        $pages = [];
84 1
        for ($i = $start; $i <= $end; $i++) {
85 1
            $pages[] = $i;
86 1
        }
87 1
        if ($start > 1) {
88
            $end_min = 1 + $this->options['offset_page'] >= $start ? $start - 1 : 1 + $this->options['offset_page'];
89
            if ($start - $end_min > 1) {
90
                array_unshift($pages, null);
91
            }
92
            for ($i = $end_min; $i >= 1; $i--) {
93
                array_unshift($pages, $i);
94
            }
95
        }
96 1
        if ($items - $end >= 1) {
97 1
            if ($items - $this->options['offset_page'] > $end + 1) {
98 1
                $pages[] = null;
99 1
            }
100 1
            $end_max = $items - $this->options['offset_page'] <= $end ? $end + 1 : $items - $this->options['offset_page'];
101 1
            for ($i = $end_max; $i <= $items; $i++) {
102 1
                $pages[] = $i;
103 1
            }
104 1
        }
105
106 1
        return $pages;
107
    }
108
}
109