AuraSqlQueryPager::offsetGet()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 28
ccs 17
cts 17
cp 1
crap 3
rs 9.6666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule\Pagerfanta;
6
7
use ArrayAccess;
8
use Aura\Sql\ExtendedPdoInterface;
9
use Aura\SqlQuery\Common\Select;
10
use Aura\SqlQuery\Common\SelectInterface;
11
use Override;
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
use function array_keys;
19
20
/** @implements ArrayAccess<int, Page> */
21
22
final class AuraSqlQueryPager implements AuraSqlQueryPagerInterface, ArrayAccess
23
{
24
    private ExtendedPdoInterface $pdo;
25
    private ?RouteGeneratorInterface $routeGenerator = null;
26
    private SelectInterface $select;
27
28
    /** @phpstan-var positive-int */
29
    private int $paging;
30
31
    /**
32
     * @param array<string, mixed> $viewOptions
33
     *
34
     * @PagerViewOption("viewOptions")
35
     */
36
    #[PagerViewOption('viewOptions')]
37
    public function __construct(private readonly ViewInterface $view, private readonly array $viewOptions)
38
    {
39
    }
40
41
    /**
42
     * @phpstan-param positive-int $paging
43
     *
44
     * @return AuraSqlQueryPagerInterface
45
     *
46
     * {@inheritDoc}
47
     */
48
    #[Override]
49
    public function init(ExtendedPdoInterface $pdo, SelectInterface $select, int $paging, RouteGeneratorInterface $routeGenerator)
50
    {
51
        $this->pdo = $pdo;
52
        $this->select = $select;
53 8
        $this->paging = $paging;
54
        $this->routeGenerator = $routeGenerator;
55 8
56 8
        return $this;
57 8
    }
58
59
    /**
60
     * @phpstan-param positive-int $page
61
     * {@inheritDoc}
62 3
     */
63
    #[Override]
64 3
    public function offsetGet($page): Page
65 3
    {
66 3
        if (! $this->routeGenerator instanceof RouteGeneratorInterface) {
67 3
            throw new NotInitialized();
68 3
        }
69
70
        $countQueryBuilderModifier = static function (Select $select): Select {
71
            foreach (array_keys($select->getCols()) as $key) {
72
                $select->removeCol($key);
73 6
            }
74
75 6
            return $select->cols(['COUNT(*) AS total_results'])->resetOrderBy()->limit(1);
76 1
        };
77
        $pagerfanta = new Pagerfanta(new AuraSqlQueryAdapter($this->pdo, $this->select, $countQueryBuilderModifier));
78
        $pagerfanta->setMaxPerPage($this->paging);
79 5
        $pagerfanta->setCurrentPage($page);
80 5
        $pager = new Page($pagerfanta, $this->routeGenerator, $this->view, $this->viewOptions);
81 5
        $pager->maxPerPage = $pagerfanta->getMaxPerPage();
82
        $pager->current = $pagerfanta->getCurrentPage();
83
        /** @psalm-suppress UndefinedDocblockClass */
84 5
        $pager->hasNext = $pagerfanta->hasNextPage();
85 5
        /** @psalm-suppress UndefinedDocblockClass */
86 5
        $pager->hasPrevious = $pagerfanta->hasPreviousPage();
87 5
        $pager->data = $pagerfanta->getCurrentPageResults();
88 5
        $pager->total = $pagerfanta->getNbResults();
89
90 5
        return $pager;
91 5
    }
92 5
93 5
    /**
94 5
     * {@inheritDoc}
95 5
     */
96 5
    #[Override]
97
    public function offsetExists($offset): bool
98 5
    {
99
        throw new LogicException('unsupported');
100
    }
101
102
    /**
103
     * {@inheritDoc}
104 1
     */
105
    #[Override]
106 1
    public function offsetSet($offset, $value): void
107
    {
108
        throw new LogicException('read only');
109
    }
110
111
    /**
112 1
     * {@inheritDoc}
113
     */
114 1
    #[Override]
115
    public function offsetUnset($offset): void
116
    {
117
        throw new LogicException('read only');
118
    }
119
}
120