AuraSqlQueryPager::offsetGet()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

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