Completed
Push — 1.x ( 75b850...fbaee3 )
by Akihito
15s queued 14s
created

AuraSqlPager::getPdoAdapter()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 12
rs 10
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
use function assert;
18
use function class_exists;
19
20
/**
21
 * @template T
22
 */
23
class AuraSqlPager implements AuraSqlPagerInterface
24
{
25
    private ViewInterface $view;
26
    private ?RouteGeneratorInterface $routeGenerator = null;
27
28
    /** @var array<array<string>> */
29
    private array $viewOptions;
30
    private ExtendedPdoInterface $pdo;
31
    private string $sql;
32
    private ?string $entity = null;
33
34
    /** @var array<mixed> */
35
    private array $params;
36
37
    /** @phpstan-var positive-int */
38
    private int $paging;
39
40
    /**
41
     * @param array<array<string>> $viewOptions
42
     *
43
     * @PagerViewOption("viewOptions")
44
     */
45
    #[PagerViewOption('viewOptions')]
46
    public function __construct(ViewInterface $view, array $viewOptions)
47
    {
48
        $this->view = $view;
49
        $this->viewOptions = $viewOptions;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     *
55
     * @phpstan-param positive-int $paging
56
     */
57
    public function init(ExtendedPdoInterface $pdo, $sql, array $params, $paging, RouteGeneratorInterface $routeGenerator, ?string $entity = null): void
58
    {
59 8
        $this->pdo = $pdo;
60
        $this->sql = $sql;
61 8
        $this->params = $params;
62 8
        $this->paging = $paging;
63 8
        $this->routeGenerator = $routeGenerator;
64
        $this->entity = $entity;
65
    }
66
67
    /**
68 3
     * {@inheritdoc}
69
     */
70 3
    #[ReturnTypeWillChange]
71 3
    public function offsetExists($offset): bool
72 3
    {
73 3
        throw new LogicException('unsupported');
74 3
    }
75 3
76
    /**
77
     * {@inheritdoc}
78
     *
79
     * @phpstan-param positive-int $currentPage
80 1
     */
81
    public function offsetGet($currentPage): Page
82 1
    {
83
        if (! $this->routeGenerator instanceof RouteGeneratorInterface) {
84
            throw new NotInitialized();
85
        }
86
87
        $pagerfanta = new Pagerfanta($this->getPdoAdapter());
88 5
        $pagerfanta->setMaxPerPage($this->paging);
89
        $pagerfanta->setCurrentPage($currentPage);
90 5
        $page = new Page($pagerfanta, $this->routeGenerator, $this->view, $this->viewOptions);
91 1
        $page->maxPerPage = $pagerfanta->getMaxPerPage();
92
        $page->current = $pagerfanta->getCurrentPage();
93 4
        /** @psalm-suppress UndefinedDocblockClass */
94 4
        $page->hasNext = (bool) $pagerfanta->hasNextPage();
95 4
        /** @psalm-suppress UndefinedDocblockClass */
96 4
        $page->hasPrevious = $pagerfanta->hasPreviousPage();
97 4
        $page->data = $pagerfanta->getCurrentPageResults();
98 4
        $page->total = $pagerfanta->getNbResults();
99 4
100 4
        return $page;
101 4
    }
102 4
103
    /**
104 4
     * {@inheritdoc}
105
     */
106
    public function offsetSet($offset, $value): void
107
    {
108
        throw new LogicException('read only');
109
    }
110 1
111
    /**
112 1
     * {@inheritdoc}
113
     */
114
    public function offsetUnset($offset): void
115
    {
116
        throw new LogicException('read only');
117
    }
118 1
119
    /**
120 1
     * @return AdapterInterface<T>
121
     */
122
    private function getPdoAdapter(): AdapterInterface
123
    {
124
        assert($this->entity === null || class_exists($this->entity));
125
        $fetcher = $this->entity ? new FetchEntity($this->pdo, $this->entity) : new FetchAssoc($this->pdo);
126
127
        return new ExtendedPdoAdapter($this->pdo, $this->sql, $this->params, $fetcher);
128
    }
129
}
130