AuraSqlPager::offsetGet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 21
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule\Pagerfanta;
6
7
use Aura\Sql\ExtendedPdoInterface;
8
use Override;
9
use Pagerfanta\Exception\LogicException;
10
use Pagerfanta\Pagerfanta;
11
use Pagerfanta\View\ViewInterface;
12
use Ray\AuraSqlModule\Annotation\PagerViewOption;
13
use Ray\AuraSqlModule\Exception\NotInitialized;
14
// phpcs:ignore SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse
15
use ReturnTypeWillChange;
16
17
use function assert;
18
use function class_exists;
19
20
/** @template T */
21
final class AuraSqlPager implements AuraSqlPagerInterface
22
{
23
    private ?RouteGeneratorInterface $routeGenerator = null;
24
    private ExtendedPdoInterface $pdo;
25
    private string $sql;
26
    private ?string $entity = null;
27
28
    /** @var array<mixed> */
29
    private array $params;
30
31
    /** @phpstan-var positive-int */
32
    private int $paging;
33
34
    /** @param array<string, mixed> $viewOptions */
35
    public function __construct(
36
        private readonly ViewInterface $view,
37
        #[PagerViewOption]
38
        private readonly array $viewOptions
39
    ) {
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     *
45
     * @phpstan-param positive-int $paging
46
     */
47
    #[Override]
48
    public function init(ExtendedPdoInterface $pdo, $sql, array $params, $paging, RouteGeneratorInterface $routeGenerator, ?string $entity = null): void
49
    {
50
        $this->pdo = $pdo;
51
        $this->sql = $sql;
52
        $this->params = $params;
53
        $this->paging = $paging;
54
        $this->routeGenerator = $routeGenerator;
55
        $this->entity = $entity;
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    #[Override]
62
    #[ReturnTypeWillChange]
63
    public function offsetExists($offset): bool
64
    {
65
        throw new LogicException('unsupported');
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     *
71
     * @phpstan-param positive-int $currentPage
72
     */
73
    #[Override]
74
    public function offsetGet($currentPage): Page
75
    {
76
        if (! $this->routeGenerator instanceof RouteGeneratorInterface) {
77
            throw new NotInitialized();
78
        }
79
80
        $pagerfanta = new Pagerfanta($this->getPdoAdapter());
81
        $pagerfanta->setMaxPerPage($this->paging);
82
        $pagerfanta->setCurrentPage($currentPage);
83
        $page = new Page($pagerfanta, $this->routeGenerator, $this->view, $this->viewOptions);
84
        $page->maxPerPage = $pagerfanta->getMaxPerPage();
85
        $page->current = $pagerfanta->getCurrentPage();
86
        /** @psalm-suppress UndefinedDocblockClass */
87
        $page->hasNext = (bool) $pagerfanta->hasNextPage();
88
        /** @psalm-suppress UndefinedDocblockClass */
89
        $page->hasPrevious = $pagerfanta->hasPreviousPage();
90
        $page->data = $pagerfanta->getCurrentPageResults();
91
        $page->total = $pagerfanta->getNbResults();
92
93
        return $page;
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    #[Override]
100
    public function offsetSet($offset, $value): void
101
    {
102
        throw new LogicException('read only');
103
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108
    #[Override]
109
    public function offsetUnset($offset): void
110
    {
111
        throw new LogicException('read only');
112
    }
113
114
    /** @return ExtendedPdoAdapter<T> */
115
    private function getPdoAdapter(): ExtendedPdoAdapter
116
    {
117
        assert($this->entity === null || class_exists($this->entity));
118
        $fetcher = $this->entity ? new FetchEntity($this->pdo, $this->entity) : new FetchAssoc($this->pdo);
119
120
        /** @var ExtendedPdoAdapter<T> $adapter */
121
        $adapter = new ExtendedPdoAdapter($this->pdo, $this->sql, $this->params, $fetcher);
122
123
        return $adapter;
124
    }
125
}
126