Completed
Push — 1.x ( f8a550...c002e4 )
by Akihito
03:35
created

AuraSqlQueryPager::offsetGet()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4.0015

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 29
ccs 21
cts 22
cp 0.9545
rs 8.5806
cc 4
eloc 20
nc 2
nop 1
crap 4.0015
1
<?php
2
/**
3
 * This file is part of the Ray.AuraSqlModule package
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\AuraSqlModule\Pagerfanta;
8
9
use Aura\Sql\ExtendedPdoInterface;
10
use Aura\SqlQuery\Common\Select;
11
use Aura\SqlQuery\Common\SelectInterface;
12
use Pagerfanta\Exception\LogicException;
13
use Pagerfanta\Pagerfanta;
14
use Pagerfanta\View\ViewInterface;
15
use Ray\AuraSqlModule\Annotation\PagerViewOption;
16
use Ray\AuraSqlModule\Exception\NotInitialized;
17
18
class AuraSqlQueryPager implements AuraSqlQueryPagerInterface, \ArrayAccess
19
{
20
    private $pdo;
21
22
    /**
23
     * @var ViewInterface
24
     */
25
    private $view;
26
27
    /**
28
     * @var RouteGeneratorInterface
29
     */
30
    private $routeGenerator;
31
32
    /**
33
     * @var array
34
     */
35
    private $viewOptions;
36
37
    /**
38
     * @var SelectInterface
39
     */
40
    private $select;
41
42
    /**
43
     * @var int
44
     */
45
    private $paging;
46
47
    /**
48
     * @param ViewInterface $view
49
     * @param array         $viewOptions
50
     *
51
     * @PagerViewOption("viewOptions")
52
     */
53 21
    public function __construct(ViewInterface $view, array $viewOptions)
54
    {
55 21
        $this->view = $view;
56 21
        $this->viewOptions = $viewOptions;
57 21
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 6
    public function init(ExtendedPdoInterface $pdo, SelectInterface $select, $paging, RouteGeneratorInterface $routeGenerator)
63
    {
64 6
        $this->pdo = $pdo;
65 6
        $this->select = $select;
66 6
        $this->paging = $paging;
67 6
        $this->routeGenerator = $routeGenerator;
68 6
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73 12
    public function offsetGet($page)
74
    {
75 12
        if (! $this->routeGenerator instanceof RouteGeneratorInterface) {
76 3
            throw new NotInitialized();
77
        }
78
79 9
        $countQueryBuilderModifier = function (SelectInterface $select) {
80 9
            if (!$select instanceof Select) {
81
                throw new NotInitialized();
82
            }
83 9
            foreach (array_keys($select->getCols()) as $key) {
84 9
                $select->removeCol($key);
85 6
            }
86 9
            return $select->cols(['COUNT(*) AS total_results'])->limit(1);
87 9
        };
88 9
        $pagerfanta = new Pagerfanta(new AuraSqlQueryAdapter($this->pdo, $this->select, $countQueryBuilderModifier));
0 ignored issues
show
Compatibility introduced by
$this->pdo of type object<Aura\Sql\ExtendedPdoInterface> is not a sub-type of object<Aura\Sql\ExtendedPdo>. It seems like you assume a concrete implementation of the interface Aura\Sql\ExtendedPdoInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
89 9
        $pagerfanta->setCurrentPage($page);
90 9
        $pagerfanta->setMaxPerPage($this->paging);
91
92 9
        $pager = new Page($pagerfanta, $this->routeGenerator, $this->view, $this->viewOptions);
93 9
        $pager->maxPerPage = $pagerfanta->getMaxPerPage();
94 9
        $pager->current = $pagerfanta->getCurrentPage();
95 9
        $pager->hasNext = $pagerfanta->hasNextPage();
96 9
        $pager->hasPrevious = $pagerfanta->hasPreviousPage();
97 9
        $pager->data = $pagerfanta->getCurrentPageResults();
98 9
        $pager->total = $pagerfanta->getNbResults();
99
100 9
        return $pager;
101
    }
102
103
    /**
104
     * @inheritDoc
105
     */
106 3
    public function offsetExists($offset)
107
    {
108 3
        throw new LogicException('unsupported');
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114 3
    public function offsetSet($offset, $value)
115
    {
116 3
        throw new LogicException('read only');
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122 3
    public function offsetUnset($offset)
123
    {
124 3
        throw new LogicException('read only');
125
    }
126
}
127