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;
0 ignored issues
show
Bug introduced by
The type ReturnTypeWillChange was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
    #[PagerViewOption('viewOptions')]
36
    public function __construct(private readonly ViewInterface $view, private readonly array $viewOptions)
37
    {
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     *
43
     * @phpstan-param positive-int $paging
44
     */
45
    #[Override]
46
    public function init(ExtendedPdoInterface $pdo, $sql, array $params, $paging, RouteGeneratorInterface $routeGenerator, ?string $entity = null): void
47
    {
48
        $this->pdo = $pdo;
49
        $this->sql = $sql;
50
        $this->params = $params;
51
        $this->paging = $paging;
52
        $this->routeGenerator = $routeGenerator;
53
        $this->entity = $entity;
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    #[Override]
60
    #[ReturnTypeWillChange]
61
    public function offsetExists($offset): bool
62
    {
63
        throw new LogicException('unsupported');
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     *
69
     * @phpstan-param positive-int $currentPage
70
     */
71
    #[Override]
72
    public function offsetGet($currentPage): Page
73
    {
74
        if (! $this->routeGenerator instanceof RouteGeneratorInterface) {
75
            throw new NotInitialized();
76
        }
77
78
        $pagerfanta = new Pagerfanta($this->getPdoAdapter());
79
        $pagerfanta->setMaxPerPage($this->paging);
80
        $pagerfanta->setCurrentPage($currentPage);
81
        $page = new Page($pagerfanta, $this->routeGenerator, $this->view, $this->viewOptions);
82
        $page->maxPerPage = $pagerfanta->getMaxPerPage();
83
        $page->current = $pagerfanta->getCurrentPage();
84
        /** @psalm-suppress UndefinedDocblockClass */
85
        $page->hasNext = (bool) $pagerfanta->hasNextPage();
86
        /** @psalm-suppress UndefinedDocblockClass */
87
        $page->hasPrevious = $pagerfanta->hasPreviousPage();
88
        $page->data = $pagerfanta->getCurrentPageResults();
89
        $page->total = $pagerfanta->getNbResults();
90
91
        return $page;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    #[Override]
98
    public function offsetSet($offset, $value): void
99
    {
100
        throw new LogicException('read only');
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106
    #[Override]
107
    public function offsetUnset($offset): void
108
    {
109
        throw new LogicException('read only');
110
    }
111
112
    /** @return ExtendedPdoAdapter<T> */
113
    private function getPdoAdapter(): ExtendedPdoAdapter
114
    {
115
        assert($this->entity === null || class_exists($this->entity));
116
        $fetcher = $this->entity ? new FetchEntity($this->pdo, $this->entity) : new FetchAssoc($this->pdo);
0 ignored issues
show
Bug introduced by
The type Ray\AuraSqlModule\Pagerfanta\FetchAssoc was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type Ray\AuraSqlModule\Pagerfanta\FetchEntity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
117
118
        /** @var ExtendedPdoAdapter<T> $adapter */
119
        $adapter = new ExtendedPdoAdapter($this->pdo, $this->sql, $this->params, $fetcher);
120
121
        return $adapter;
122
    }
123
}
124