Passed
Push — master ( d1423b...0bb58d )
by Zlatin
03:48
created

Paginator::pagination()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 6.0944

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
ccs 25
cts 29
cp 0.8621
rs 8.439
cc 6
eloc 27
nc 17
nop 2
crap 6.0944
1
<?php
2
3
namespace Pagination\Util;
4
5
use Silex\Application;
6
use Symfony\Component\HttpFoundation\Request;
7
8
class Paginator
9
{
10
    /**
11
     * @var Application
12
     */
13
    private $app;
14
15
    /**
16
     * @var array
17
     */
18
    private $options;
19
20
    /**
21
     * @param Application $app
22
     */
23 3
    public function __construct(Application $app)
24
    {
25 3
        $this->app = $app;
26 3
    }
27
28
    /**
29
     * @param \Doctrine\ORM\QueryBuilder|array $query
30
     * @param array                      $options
31
     *
32
     * @throws \Pagination\Exception\UnknownAdapterException
33
     *
34
     * @return array
35
     */
36 2
    public function pagination($query, $options = [])
37
    {
38 2
        $this->options = array_replace($this->app['paginator.options'], $options);
39
40 2
        switch (true) {
41 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...
42
                $adapter = new \Pagination\Adapter\PaginationORMAdapter();
43
                break;
44 2
            case is_array($query):
45 1
                $adapter = new \Pagination\Adapter\PaginationArrayAdapter();
46 1
                break;
47 1
            default:
48 1
                throw new \Pagination\Exception\UnknownAdapterException();
49 1
        }
50
51 1
        $current = 1;
52 1
        if ($this->app['request_stack']->getCurrentRequest()) {
53
            $current = $this->app['request_stack']->getCurrentRequest()->get('page', 1);
54
        }
55 1
        $counter = $adapter->getCounter($query);
0 ignored issues
show
Bug introduced by
It seems like $query defined by parameter $query on line 36 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...
56
57 1
        $items = ceil($counter / $this->options['items_per_page']);
58
59
        return [
60 1
            'first'      => 1,
61 1
            'prev'       => $current > 1 ? $current - 1 : null,
62 1
            'current'    => $current,
63 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 36 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...
64 1
            'pages'      => $this->getPages($current, $items),
65 1
            'next'       => $current < $items ? $current + 1 : null,
66 1
            'last'       => $items,
67 1
            'total'      => $counter,
68 1
            'total_page' => $items,
69 1
            'options'    => $this->options,
70 1
        ];
71
    }
72
73
    /**
74
     * @param int $current
75
     * @param int $items
76
     *
77
     * @return array
78
     */
79 1
    public function getPages($current, $items)
80
    {
81 1
        $start = $current - $this->options['offset_page'] > 1 ? $current - $this->options['offset_page'] : 1;
82 1
        $end = $current + $this->options['offset_page'] < $items ? $current + $this->options['offset_page'] : $items;
83
84 1
        $pages = [];
85 1
        for ($i = $start; $i <= $end; $i++) {
86 1
            $pages[] = $i;
87 1
        }
88 1
        if ($start > 1) {
89
            $end_min = 1 + $this->options['offset_page'] >= $start ? $start - 1 : 1 + $this->options['offset_page'];
90
            if ($start - $end_min > 1) {
91
                array_unshift($pages, null);
92
            }
93
            for ($i = $end_min; $i >= 1; $i--) {
94
                array_unshift($pages, $i);
95
            }
96
        }
97 1
        if ($items - $end >= 1) {
98 1
            if ($items - $this->options['offset_page'] > $end + 1) {
99 1
                $pages[] = null;
100 1
            }
101 1
            $end_max = $items - $this->options['offset_page'] <= $end ? $end + 1 : $items - $this->options['offset_page'];
102 1
            for ($i = $end_max; $i <= $items; $i++) {
103 1
                $pages[] = $i;
104 1
            }
105 1
        }
106
107 1
        return $pages;
108
    }
109
}
110