AuraSqlQueryPager::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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