Completed
Push — master ( 355225...679235 )
by Zlatin
01:59
created

Paginator::pagination()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 5.0113

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 34
ccs 24
cts 26
cp 0.9231
rs 8.439
cc 5
eloc 25
nc 9
nop 2
crap 5.0113
1
<?php
2
3
namespace Pagination\Util;
4
5
use Silex\Application;
6
use Symfony\Component\Routing\Router;
7
use Symfony\Component\HttpFoundation\Request;
8
9
class Paginator
10
{
11
12
    /**
13
     *
14
     * @var Application $app
15
     */
16
    private $app;
17
18
    /**
19
     *
20
     * @var Router $router
21
     */
22
    private $router;
23
24
    /**
25
     *
26
     * @var Request $request
27
     */
28
    private $request;
29
30
    /**
31
     *
32
     * @var array $options
33
     */
34
    private $options;
35
36
    /**
37
     * 
38
     * @param Application $app
39
     */
40 3
    public function __construct(Application $app)
41
    {
42 3
        $this->app = $app;
43 3
        $this->router = $app['url_generator'];
44 3
        $this->request = $app['request'];
45 3
    }
46
47
    /**
48
     * 
49
     * @param \Doctrine\ORM\QueryBuilder $query
50
     * @param array $options
51
     * 
52
     * @return array
53
     * 
54
     * @throws \Pagination\Exception\UnknownAdapterException
55
     */
56 2
    public function pagination($query, $options = array())
57
    {
58
59 2
        $this->options = array_replace($this->app['paginator.options'], $options);
60
61 2
        switch (true) {
62 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...
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
63
                $adapter = new \Pagination\Adapter\PaginationORMAdapter();
64
                break;
65 2
            case is_array($query) :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
66 1
                $adapter = new \Pagination\Adapter\PaginationArrayAdapter();
67 1
                break;
68 1
            default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
69 1
                throw new \Pagination\Exception\UnknownAdapterException();
70 1
        }
71
72 1
        $current = $this->request->get('page', 1);
73 1
        $counter = $adapter->getCounter($query);
0 ignored issues
show
Bug introduced by
It seems like $query 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?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
74
75 1
        $items = ceil($counter/$this->options['items_per_page']);
76
        
77
        return array(
78 1
            'first' => $this->createPage(1),
79 1
            'prev' => $current > 1 ? $this->createPage($current - 1) : null,
80 1
            'current' => $current,
81 1
            'items' => $adapter->getItems($query, $current, $this->options['items_per_page']),
0 ignored issues
show
Bug introduced by
It seems like $query 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?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
82 1
            'pages' => $this->getPages($current, $items),
83 1
            'next' => $current < $items ? $this->createPage($current + 1) : null,
84 1
            'last' => $this->createPage($items),
85 1
            'total' => $counter,
86 1
            'total_page' => $items,
87 1
            'options' => $this->options
88 1
        );
89
    }
90
91
    /**
92
     * 
93
     * @param integer $current
94
     * @param integer $items
95
     * 
96
     * @return array
97
     */
98 1
    public function getPages($current, $items)
99
    {
100
        
101 1
        $start = $current - $this->options['offset_page'] > 1 ? $current - $this->options['offset_page'] : 1;
102 1
        $end = $current + $this->options['offset_page'] < $items ? $current + $this->options['offset_page'] : $items;
103
        
104 1
        $pages = array();
105 1
        for ($i = $start; $i <= $end; $i++)
106
        {
107 1
            $pages[] = $this->createPage($i);
108 1
        }
109 1
        if ($start > 1) {
110
            $end_min = 1 + $this->options['offset_page'] >= $start ? $start - 1 : 1 + $this->options['offset_page'];
111
            if ($start - $end_min > 1) {
112
                array_unshift($pages, null);
113
            }
114
            for ($i = $end_min; $i >= 1; $i--)
115
            {
116
                array_unshift($pages, $this->createPage($i));
117
            }
118
        }
119 1
        if ($items - $end >= 1) {
120 1
            if ($items - $this->options['offset_page'] > $end + 1) {
121 1
                $pages[] = null;
122 1
            }
123 1
            $end_max = $items - $this->options['offset_page'] <= $end ? $end + 1 : $items - $this->options['offset_page'];
124 1
            for ($i = $end_max; $i <= $items; $i++)
125
            {
126 1
                $pages[] = $this->createPage($i);
127 1
            }
128 1
        }
129
        
130 1
        return $pages;
131
    }
132
133
    /**
134
     * 
135
     * @param integer $page
136
     * 
137
     * @return array
138
     */
139 1
    public function createPage($page)
140
    {
141
        return array(
142 1
            'page' => $page,
143 1
            'url' => $this->router->generate($this->request->get('_route'), array_merge(
144 1
                $this->request->query->all(),
145 1
                $this->request->get('_route_params'),
146 1
                array('page' => $page)
147 1
            ))
148 1
        );
149
    }
150
151
}
152