Passed
Push — master ( 9c6ab0...18d41c )
by Paul
08:04
created

NormalizePaginationArgs   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 81.48%

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 8
eloc 21
c 4
b 3
f 0
dl 0
loc 49
ccs 22
cts 27
cp 0.8148
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizePageUrl() 0 9 4
A normalizePageUrlParameters() 0 6 1
A normalizePage() 0 7 2
A __construct() 0 6 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Arguments;
6
use GeminiLabs\SiteReviews\Helper;
7
use GeminiLabs\SiteReviews\Helpers\Url;
8
9
/**
10
 * @property int $page;
11
 * @property string $pageUrl;
12
 * @property array $pageUrlParameters;
13
 */
14
class NormalizePaginationArgs extends Arguments
15
{
16 1
    public function __construct(array $args = [])
17
    {
18 1
        parent::__construct($args);
19 1
        $this->normalizePage();
20 1
        $this->normalizePageUrl();
21 1
        $this->normalizePageUrlParameters();
22 1
    }
23
24
    /**
25
     * Get the page number.
26
     * @return void
27
     */
28 1
    protected function normalizePage()
29
    {
30 1
        $args = glsr()->args(glsr()->retrieve(glsr()->paged_handle));
31 1
        $page = $args->get('page', 0);
32 1
        $this->page = $page
33
            ? $page
34 1
            : Helper::getPageNumber($args->url, $this->page);
35 1
    }
36
37
    /**
38
     * This should return an URL with the query string removed.
39
     * @return void
40
     */
41 1
    protected function normalizePageUrl()
42
    {
43 1
        if ($request = glsr()->retrieve(glsr()->paged_handle)) {
44
            $urlPath = Url::path($request->url);
45
            $this->pageUrl = Url::path(Url::home()) === $urlPath
46
                ? Url::home()
47
                : Url::home($urlPath);
48 1
        } elseif (empty($this->pageUrl = get_permalink())) {
49 1
            $this->pageUrl = Url::home(Url::path($_SERVER['REQUEST_URI']));
50
        }
51 1
    }
52
53
    /**
54
     * Store the query string of the URL so that we don't lose it in pagination.
55
     * @return void
56
     */
57 1
    protected function normalizePageUrlParameters()
58
    {
59 1
        $args = glsr()->args(glsr()->retrieve(glsr()->paged_handle));
60 1
        $parameters = Url::queries($args->url);
61 1
        unset($parameters[glsr()->constant('PAGED_QUERY_VAR')]);
62 1
        $this->pageUrlParameters = $parameters;
63 1
    }
64
}
65