AuraSqlPager   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 37
c 4
b 0
f 0
dl 0
loc 103
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetExists() 0 4 1
A offsetGet() 0 20 2
A offsetSet() 0 3 1
A offsetUnset() 0 3 1
A init() 0 8 1
A __construct() 0 5 1
A getPdoAdapter() 0 6 3
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
    /**
39
     * @param array<array<string>> $viewOptions
40
     *
41
     * @PagerViewOption("viewOptions")
42
     */
43
    #[PagerViewOption('viewOptions')]
44
    public function __construct(ViewInterface $view, array $viewOptions)
45
    {
46
        $this->view = $view;
47
        $this->viewOptions = $viewOptions;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     *
53
     * @phpstan-param positive-int $paging
54
     */
55
    public function init(ExtendedPdoInterface $pdo, $sql, array $params, $paging, RouteGeneratorInterface $routeGenerator, ?string $entity = null): void
56
    {
57
        $this->pdo = $pdo;
58
        $this->sql = $sql;
59
        $this->params = $params;
60
        $this->paging = $paging;
61
        $this->routeGenerator = $routeGenerator;
62
        $this->entity = $entity;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    #[ReturnTypeWillChange]
69
    public function offsetExists($offset): bool
70
    {
71
        throw new LogicException('unsupported');
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     *
77
     * @phpstan-param positive-int $currentPage
78
     */
79
    public function offsetGet($currentPage): Page
80
    {
81
        if (! $this->routeGenerator instanceof RouteGeneratorInterface) {
82
            throw new NotInitialized();
83
        }
84
85
        $pagerfanta = new Pagerfanta($this->getPdoAdapter());
86
        $pagerfanta->setMaxPerPage($this->paging);
87
        $pagerfanta->setCurrentPage($currentPage);
88
        $page = new Page($pagerfanta, $this->routeGenerator, $this->view, $this->viewOptions);
89
        $page->maxPerPage = $pagerfanta->getMaxPerPage();
90
        $page->current = $pagerfanta->getCurrentPage();
91
        /** @psalm-suppress UndefinedDocblockClass */
92
        $page->hasNext = (bool) $pagerfanta->hasNextPage();
93
        /** @psalm-suppress UndefinedDocblockClass */
94
        $page->hasPrevious = $pagerfanta->hasPreviousPage();
95
        $page->data = $pagerfanta->getCurrentPageResults();
96
        $page->total = $pagerfanta->getNbResults();
97
98
        return $page;
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104
    public function offsetSet($offset, $value): void
105
    {
106
        throw new LogicException('read only');
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112
    public function offsetUnset($offset): void
113
    {
114
        throw new LogicException('read only');
115
    }
116
117
    /** @return AdapterInterface<T> */
118
    private function getPdoAdapter(): AdapterInterface
119
    {
120
        assert($this->entity === null || class_exists($this->entity));
121
        $fetcher = $this->entity ? new FetchEntity($this->pdo, $this->entity) : new FetchAssoc($this->pdo);
122
123
        return new ExtendedPdoAdapter($this->pdo, $this->sql, $this->params, $fetcher);
124
    }
125
}
126