AuraSqlQueryPager::offsetGet()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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