Issues (56)

src/Pagerfanta/AuraSqlQueryPager.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule\Pagerfanta;
6
7
use ArrayAccess;
8
use Aura\Sql\ExtendedPdoInterface;
9
use Aura\SqlQuery\Common\Select;
10
use Aura\SqlQuery\Common\SelectInterface;
11
use Override;
0 ignored issues
show
The type Override 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...
12
use Pagerfanta\Exception\LogicException;
13
use Pagerfanta\Pagerfanta;
14
use Pagerfanta\View\ViewInterface;
15
use Ray\AuraSqlModule\Annotation\PagerViewOption;
16
use Ray\AuraSqlModule\Exception\NotInitialized;
17
18
use function array_keys;
19
20
/** @implements ArrayAccess<int, Page> */
21
22
final class AuraSqlQueryPager implements AuraSqlQueryPagerInterface, ArrayAccess
23
{
24
    private ExtendedPdoInterface $pdo;
25
    private ?RouteGeneratorInterface $routeGenerator = null;
26
    private SelectInterface $select;
27
28
    /** @phpstan-var positive-int */
29
    private int $paging;
30
31
    /** @param array<string, mixed> $viewOptions */
32
    public function __construct(
33
        private readonly ViewInterface $view,
34
        #[PagerViewOption]
35
        private readonly array $viewOptions
36
    ) {
37
    }
38
39
    /**
40
     * @phpstan-param positive-int $paging
41
     *
42
     * @return AuraSqlQueryPagerInterface
43
     *
44
     * {@inheritDoc}
45
     */
46
    #[Override]
47
    public function init(ExtendedPdoInterface $pdo, SelectInterface $select, int $paging, RouteGeneratorInterface $routeGenerator)
48
    {
49
        $this->pdo = $pdo;
50
        $this->select = $select;
51
        $this->paging = $paging;
52
        $this->routeGenerator = $routeGenerator;
53
54
        return $this;
55
    }
56
57
    /**
58
     * @phpstan-param positive-int $page
59
     * {@inheritDoc}
60
     */
61
    #[Override]
62
    public function offsetGet($page): Page
63
    {
64
        if (! $this->routeGenerator instanceof RouteGeneratorInterface) {
65
            throw new NotInitialized();
66
        }
67
68
        $countQueryBuilderModifier = static function (Select $select): Select {
69
            foreach (array_keys($select->getCols()) as $key) {
70
                $select->removeCol($key);
71
            }
72
73
            return $select->cols(['COUNT(*) AS total_results'])->resetOrderBy()->limit(1);
74
        };
75
        $pagerfanta = new Pagerfanta(new AuraSqlQueryAdapter($this->pdo, $this->select, $countQueryBuilderModifier));
76
        $pagerfanta->setMaxPerPage($this->paging);
77
        $pagerfanta->setCurrentPage($page);
78
        $pager = new Page($pagerfanta, $this->routeGenerator, $this->view, $this->viewOptions);
79
        $pager->maxPerPage = $pagerfanta->getMaxPerPage();
80
        $pager->current = $pagerfanta->getCurrentPage();
81
        /** @psalm-suppress UndefinedDocblockClass */
82
        $pager->hasNext = $pagerfanta->hasNextPage();
83
        /** @psalm-suppress UndefinedDocblockClass */
84
        $pager->hasPrevious = $pagerfanta->hasPreviousPage();
85
        $pager->data = $pagerfanta->getCurrentPageResults();
86
        $pager->total = $pagerfanta->getNbResults();
87
88
        return $pager;
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    #[Override]
95
    public function offsetExists($offset): bool
96
    {
97
        throw new LogicException('unsupported');
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103
    #[Override]
104
    public function offsetSet($offset, $value): void
105
    {
106
        throw new LogicException('read only');
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112
    #[Override]
113
    public function offsetUnset($offset): void
114
    {
115
        throw new LogicException('read only');
116
    }
117
}
118