AuraSqlQueryPager   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 90
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 90
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
    /** @param array<array<string>> $viewOptions */
35
    #[PagerViewOption('viewOptions')]
36
    public function __construct(ViewInterface $view, array $viewOptions)
37
    {
38
        $this->view = $view;
39
        $this->viewOptions = $viewOptions;
40
    }
41
42
    /**
43
     * @phpstan-param positive-int $paging
44
     * {@inheritDoc}
45
     */
46
    public function init(ExtendedPdoInterface $pdo, SelectInterface $select, int $paging, RouteGeneratorInterface $routeGenerator)
47
    {
48
        $this->pdo = $pdo;
49
        $this->select = $select;
50
        $this->paging = $paging;
51
        $this->routeGenerator = $routeGenerator;
52
53
        return $this;
54
    }
55
56
    /**
57
     * @phpstan-param positive-int $page
58
     * {@inheritDoc}
59
     */
60
    public function offsetGet($page): Page
61
    {
62
        if (! $this->routeGenerator instanceof RouteGeneratorInterface) {
63
            throw new NotInitialized();
64
        }
65
66
        $countQueryBuilderModifier = static function (Select $select): Select {
67
            foreach (array_keys($select->getCols()) as $key) {
68
                $select->removeCol($key);
69
            }
70
71
            return $select->cols(['COUNT(*) AS total_results'])->resetOrderBy()->limit(1);
72
        };
73
        $pagerfanta = new Pagerfanta(new AuraSqlQueryAdapter($this->pdo, $this->select, $countQueryBuilderModifier));
74
        $pagerfanta->setMaxPerPage($this->paging);
75
        $pagerfanta->setCurrentPage($page);
76
        $pager = new Page($pagerfanta, $this->routeGenerator, $this->view, $this->viewOptions);
77
        $pager->maxPerPage = $pagerfanta->getMaxPerPage();
78
        $pager->current = $pagerfanta->getCurrentPage();
79
        /** @psalm-suppress UndefinedDocblockClass */
80
        $pager->hasNext = $pagerfanta->hasNextPage();
81
        /** @psalm-suppress UndefinedDocblockClass */
82
        $pager->hasPrevious = $pagerfanta->hasPreviousPage();
83
        $pager->data = $pagerfanta->getCurrentPageResults();
84
        $pager->total = $pagerfanta->getNbResults();
85
86
        return $pager;
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function offsetExists($offset): bool
93
    {
94
        throw new LogicException('unsupported');
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