AuraSqlQueryPager   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 8
eloc 34
c 4
b 0
f 1
dl 0
loc 94
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetExists() 0 3 1
A __construct() 0 5 1
A offsetGet() 0 27 3
A init() 0 8 1
A offsetUnset() 0 3 1
A offsetSet() 0 3 1
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 Pagerfanta\Exception\LogicException;
12
use Pagerfanta\Pagerfanta;
13
use Pagerfanta\View\ViewInterface;
14
use Ray\AuraSqlModule\Annotation\PagerViewOption;
15
use Ray\AuraSqlModule\Exception\NotInitialized;
16
17
use function array_keys;
18
19
/** @implements ArrayAccess<int, Page> */
20
21
class AuraSqlQueryPager implements AuraSqlQueryPagerInterface, ArrayAccess
22
{
23
    private ExtendedPdoInterface $pdo;
24
    private ViewInterface $view;
25
    private ?RouteGeneratorInterface $routeGenerator = null;
26
27
    /** @var array<array<string>> */
28
    private array $viewOptions;
29
    private SelectInterface $select;
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
     * @phpstan-param positive-int $paging
48
     * {@inheritDoc}
49
     */
50
    public function init(ExtendedPdoInterface $pdo, SelectInterface $select, int $paging, RouteGeneratorInterface $routeGenerator)
51
    {
52
        $this->pdo = $pdo;
53
        $this->select = $select;
54
        $this->paging = $paging;
55
        $this->routeGenerator = $routeGenerator;
56
57
        return $this;
58
    }
59
60
    /**
61
     * @phpstan-param positive-int $page
62
     * {@inheritDoc}
63
     */
64
    public function offsetGet($page): Page
65
    {
66
        if (! $this->routeGenerator instanceof RouteGeneratorInterface) {
67
            throw new NotInitialized();
68
        }
69
70
        $countQueryBuilderModifier = static function (Select $select): Select {
71
            foreach (array_keys($select->getCols()) as $key) {
72
                $select->removeCol($key);
73
            }
74
75
            return $select->cols(['COUNT(*) AS total_results'])->resetOrderBy()->limit(1);
76
        };
77
        $pagerfanta = new Pagerfanta(new AuraSqlQueryAdapter($this->pdo, $this->select, $countQueryBuilderModifier));
78
        $pagerfanta->setMaxPerPage($this->paging);
79
        $pagerfanta->setCurrentPage($page);
80
        $pager = new Page($pagerfanta, $this->routeGenerator, $this->view, $this->viewOptions);
81
        $pager->maxPerPage = $pagerfanta->getMaxPerPage();
82
        $pager->current = $pagerfanta->getCurrentPage();
83
        /** @psalm-suppress UndefinedDocblockClass */
84
        $pager->hasNext = $pagerfanta->hasNextPage();
85
        /** @psalm-suppress UndefinedDocblockClass */
86
        $pager->hasPrevious = $pagerfanta->hasPreviousPage();
87
        $pager->data = $pagerfanta->getCurrentPageResults();
88
        $pager->total = $pagerfanta->getNbResults();
89
90
        return $pager;
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96
    public function offsetExists($offset): bool
97
    {
98
        throw new LogicException('unsupported');
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