Passed
Pull Request — 1.x (#65)
by Akihito
09:19 queued 07:17
created

AuraSqlPager::getPdoAdapter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 6
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
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 8
    }
60
61 8
    /**
62 8
     * {@inheritdoc}
63 8
     */
64
    #[ReturnTypeWillChange]
65
    public function offsetExists($offset): bool
66
    {
67
        throw new LogicException('unsupported');
68 3
    }
69
70 3
    /**
71 3
     * {@inheritdoc}
72 3
     *
73 3
     * @phpstan-param positive-int $currentPage
74 3
     */
75 3
    public function offsetGet($currentPage): Page
76
    {
77
        if (! $this->routeGenerator instanceof RouteGeneratorInterface) {
78
            throw new NotInitialized();
79
        }
80 1
81
        $pagerfanta = new Pagerfanta($this->getPdoAdapter());
82 1
        $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 5
        $page->hasNext = (bool) $pagerfanta->hasNextPage();
89
        /** @psalm-suppress UndefinedDocblockClass */
90 5
        $page->hasPrevious = $pagerfanta->hasPreviousPage();
91 1
        $page->data = $pagerfanta->getCurrentPageResults();
92
        $page->total = $pagerfanta->getNbResults();
93 4
94 4
        return $page;
95 4
    }
96 4
97 4
    /**
98 4
     * {@inheritdoc}
99 4
     */
100 4
    public function offsetSet($offset, $value): void
101 4
    {
102 4
        throw new LogicException('read only');
103
    }
104 4
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function offsetUnset($offset): void
109
    {
110 1
        throw new LogicException('read only');
111
    }
112 1
113
    private function getPdoAdapter(): AdapterInterface
114
    {
115
        $fetcher = $this->entity ? new FetchEntity($this->pdo, $this->entity) : new FetchAssoc($this->pdo);
116
117
        return new ExtendedPdoAdapter($this->pdo, $this->sql, $this->params, $fetcher);
118 1
    }
119
}
120