PagerUriManager::buildSizeUri()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\OpenCubes\Component\Pager;
4
5
use BenTools\OpenCubes\OptionsTrait;
6
use Psr\Http\Message\UriInterface;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
use function BenTools\OpenCubes\cast;
9
use function BenTools\QueryString\query_string;
10
11
final class PagerUriManager implements PagerUriManagerInterface
12
{
13
    use OptionsTrait;
14
15
    public const OPT_PAGESIZE_QUERY_PARAM = 'pagesize_query_param';
16
    public const OPT_PAGE_QUERY_PARAM = 'page_query_param';
17
18
    /**
19
     * PageSizerUriParser constructor.
20
     */
21
    public function __construct(array $options = [])
22
    {
23
        $optionsResolver = new OptionsResolver();
24
        $optionsResolver->setDefaults([
25
            self::OPT_PAGESIZE_QUERY_PARAM => 'per_page',
26
            self::OPT_PAGE_QUERY_PARAM => 'page',
27
        ]);
28
29
        $this->options = $optionsResolver->resolve($options);
30
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35
    public function getCurrentPageSize(UriInterface $uri): ?int
36
    {
37
        $qs = query_string($uri);
38
39
        return cast($qs->getParam($this->getOption(self::OPT_PAGESIZE_QUERY_PARAM)))->asIntOrNull();
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function getCurrentPageNumber(UriInterface $uri): ?int
46
    {
47
        $qs = query_string($uri);
48
49
        return cast($qs->getParam($this->getOption(self::OPT_PAGE_QUERY_PARAM)))->asIntOrNull();
50
    }
51
52
    /**
53
     * @param UriInterface $uri
54
     * @param int          $pageNumber
55
     * @param bool|null    $paginationEnabled
0 ignored issues
show
Bug introduced by
There is no parameter named $paginationEnabled. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
56
     * @return UriInterface
57
     * @throws \InvalidArgumentException
58
     */
59
    public function buildPageUri(UriInterface $uri, int $pageNumber): UriInterface
60
    {
61
        $qs = query_string($uri);
62
63
        if (1 === $pageNumber) {
64
            $qs = $qs->withoutParam($this->getOption(self::OPT_PAGE_QUERY_PARAM));
65
        } else {
66
            $qs = $qs->withParam($this->getOption(self::OPT_PAGE_QUERY_PARAM), $pageNumber);
67
        }
68
69
        return $uri->withQuery((string) $qs);
70
    }
71
72
    /**
73
     * @param UriInterface $uri
74
     * @param int          $size
75
     * @return UriInterface
76
     * @throws \InvalidArgumentException
77
     * @throws \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException
78
     */
79
    public function buildSizeUri(UriInterface $uri, ?int $size): UriInterface
80
    {
81
        $uri = $this->buildPageUri($uri, 1);
82
        $qs = query_string($uri);
83
84
        if (null === $size) {
85
            $qs = $qs->withoutParam($this->getOption(self::OPT_PAGESIZE_QUERY_PARAM));
86
        } else {
87
            $qs = $qs->withParam($this->getOption(self::OPT_PAGESIZE_QUERY_PARAM), $size);
88
        }
89
90
        return $uri->withQuery((string) $qs);
91
    }
92
}
93