Completed
Push — master ( d24529...76d477 )
by BENOIT
05:43
created

OffsetParameterUrlBuilder::buildUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
3
namespace BenTools\Pager\Model\Factory;
4
5
use BenTools\Pager\Contract\PageInterface;
6
use BenTools\Pager\Contract\PagerFactoryInterface;
7
use BenTools\Pager\Contract\PagerInterface;
8
use BenTools\Pager\Contract\PageUrlBuilderInterface;
9
use BenTools\Pager\Model\Pager;
10
use function GuzzleHttp\Psr7\parse_query;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Psr7\parse_query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use GuzzleHttp\Psr7\Uri;
12
13
class OffsetParameterUrlBuilder implements PageUrlBuilderInterface, PagerFactoryInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $baseUrl;
19
20
    /**
21
     * @var int
22
     */
23
    private $perPage;
24
25
    /**
26
     * @var string
27
     */
28
    private $offsetQueryParam;
29
30
    /**
31
     * PageUrlBuilder constructor.
32
     * @param string $baseUrl
33
     * @param int    $perPage
34
     * @param string $offsetQueryParam
35
     */
36
    public function __construct(string $baseUrl, int $perPage, string $offsetQueryParam)
37
    {
38
        $this->baseUrl = $baseUrl;
39
        $this->perPage = $perPage;
40
        $this->offsetQueryParam = $offsetQueryParam;
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    private function getCurrentPageNumber(): int
47
    {
48
        $parsedQuery = parse_query((new Uri($this->baseUrl))->getQuery());
49
        $currentOffset = $parsedQuery[$this->offsetQueryParam] ?? 0;
50
        return (int) floor($currentOffset / $this->perPage) + 1;
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function buildUrl(PagerInterface $pager, PageInterface $page): string
57
    {
58
        return (string) Uri::withQueryValue(new Uri($this->baseUrl), $this->offsetQueryParam, $pager->getPageOffset($page));
59
    }
60
61
    /**
62
     * @param int    $perPage
63
     * @param string $offsetParam
64
     * @return OffsetParameterUrlBuilder
65
     * @throws \RuntimeException
66
     */
67
    public static function fromRequestUri(int $perPage, string $offsetParam): self
68
    {
69
        if (!isset($_SERVER['REQUEST_URI'])) {
70
            throw new \RuntimeException('$_SERVER[\'REQUEST_URI\'] is not set.');
71
        }
72
        return new static($_SERVER['REQUEST_URI'], $perPage, $offsetParam);
73
    }
74
75
    /**
76
     * @param int|null $numFound
77
     * @return PagerInterface
78
     */
79
    public function createPager(int $numFound = null): PagerInterface
80
    {
81
        $pager = new Pager($this->perPage, $this->getCurrentPageNumber(), $numFound);
82
        return $pager;
83
    }
84
}
85