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

PageParameterUrlBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 71
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCurrentPageNumber() 0 4 1
A createPager() 0 4 1
A fromRequestUri() 0 6 2
A __construct() 0 5 1
A buildUrl() 0 3 1
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 PageParameterUrlBuilder 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 $pageNumberQueryParam;
29
30
    /**
31
     * PageUrlBuilder constructor.
32
     * @param string $baseUrl
33
     * @param int    $perPage
34
     * @param string $pageNumberQueryParam
35
     */
36
    public function __construct(string $baseUrl, int $perPage, string $pageNumberQueryParam = 'page')
37
    {
38
        $this->baseUrl = $baseUrl;
39
        $this->perPage = $perPage;
40
        $this->pageNumberQueryParam = $pageNumberQueryParam;
41
    }
42
43
    /**
44
     * Return the current page.
45
     *
46
     * @return int
47
     */
48
    private function getCurrentPageNumber(): int
49
    {
50
        $parsedQuery = parse_query((new Uri($this->baseUrl))->getQuery());
51
        return $parsedQuery[$this->pageNumberQueryParam] ?? 1;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function buildUrl(PagerInterface $pager, PageInterface $page): string
58
    {
59
        return (string) Uri::withQueryValue(new Uri($this->baseUrl), $this->pageNumberQueryParam, $page->getPageNumber());
60
    }
61
62
    /**
63
     * @param int    $perPage
64
     * @param string $pageNumberQueryParam
65
     * @return PageParameterUrlBuilder
66
     * @throws \RuntimeException
67
     */
68
    public static function fromRequestUri(int $perPage, string $pageNumberQueryParam = 'page'): self
69
    {
70
        if (!isset($_SERVER['REQUEST_URI'])) {
71
            throw new \RuntimeException('$_SERVER[\'REQUEST_URI\'] is not set.');
72
        }
73
        return new static($_SERVER['REQUEST_URI'], $perPage, $pageNumberQueryParam);
74
    }
75
76
    /**
77
     * @param int|null $numFound
78
     * @return PagerInterface
79
     */
80
    public function createPager(int $numFound = null): PagerInterface
81
    {
82
        $pager = new Pager($this->perPage, $this->getCurrentPageNumber(), $numFound);
83
        return $pager;
84
    }
85
}
86