Passed
Push — fetch_entity_in_pager ( 75b850...64a10b )
by Akihito
11:34 queued 10:04
created

AuraSqlPager::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
class AuraSqlPager implements AuraSqlPagerInterface
18
{
19
    private ViewInterface $view;
20
    private ?RouteGeneratorInterface $routeGenerator = null;
21
22
    /** @var array<array<string>> */
23
    private array $viewOptions;
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<array<string>> $viewOptions
36
     *
37
     * @PagerViewOption("viewOptions")
38
     */
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
    private function getPdoAdapter(): AdapterInterface
114
    {
115
        $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...
116
117
        return new ExtendedPdoAdapter($this->pdo, $this->sql, $this->params, $fetcher);
118
    }
119
}
120