AuraSqlPager   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 39
c 4
b 0
f 0
dl 0
loc 105
ccs 31
cts 31
cp 1
rs 10
wmc 10

7 Methods

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