AuraSqlQueryPager::offsetSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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