Passed
Push — master ( 815afc...ad6cec )
by Zlatin
02:11
created

Paginator   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 82.14%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 105
ccs 46
cts 56
cp 0.8214
rs 10

3 Methods

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