AuraSqlPager::offsetUnset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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