Passed
Push — master ( b19f24...47cb12 )
by Paul
04:18
created

Pagination   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
eloc 32
dl 0
loc 71
ccs 0
cts 49
cp 0
rs 10
c 2
b 2
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 11 2
A normalize() 0 14 3
A buildFauxLinks() 0 14 3
A buildLinks() 0 5 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html\Partials;
4
5
use GeminiLabs\SiteReviews\Contracts\PartialContract;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Database\QueryBuilder;
8
use GeminiLabs\SiteReviews\Helpers\Arr;
9
use GeminiLabs\SiteReviews\Modules\Html\Template;
10
use GeminiLabs\SiteReviews\Modules\Style;
11
12
class Pagination implements PartialContract
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $args;
18
19
    /**
20
     * @return string
21
     */
22
    public function build(array $args = [])
23
    {
24
        $this->args = $this->normalize($args);
25
        if ($this->args['total'] < 2) {
26
            return '';
27
        }
28
        return glsr(Template::class)->build('templates/pagination', [
29
            'context' => [
30
                'links' => apply_filters('site-reviews/paginate_links', $this->buildLinks(), $this->args),
31
                'loader' => '<div class="glsr-loader"></div>',
32
                'screen_reader_text' => __('Site Reviews navigation', 'site-reviews'),
33
            ],
34
        ]);
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    protected function buildFauxLinks()
41
    {
42
        $links = paginate_links(wp_parse_args(['type' => 'array'], $this->args));
43
        $pattern = '/(href=["\'])([^"\']*?)(["\'])/i';
44
        foreach ($links as &$link) {
45
            if (!preg_match($pattern, $link, $matches)) {
46
                continue;
47
            }
48
            parse_str(parse_url(Arr::get($matches, 2), PHP_URL_QUERY), $urlQuery);
49
            $page = (int) Arr::get($urlQuery, glsr()->constant('PAGED_QUERY_VAR'), 1);
50
            $replacement = sprintf('data-page="%d" href="#"', $page);
51
            $link = str_replace(Arr::get($matches, 0), $replacement, $link);
52
        }
53
        return implode("\n", $links);
0 ignored issues
show
Bug introduced by
It seems like $links can also be of type string; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        return implode("\n", /** @scrutinizer ignore-type */ $links);
Loading history...
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    protected function buildLinks()
60
    {
61
        return glsr(OptionManager::class)->getBool('settings.reviews.pagination.url_parameter')
0 ignored issues
show
Bug Best Practice introduced by
The expression return glsr(GeminiLabs\S...$this->buildFauxLinks() also could return the type array|string[] which is incompatible with the documented return type string.
Loading history...
62
            ? paginate_links($this->args)
63
            : $this->buildFauxLinks();
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    protected function normalize(array $args)
70
    {
71
        if ($baseUrl = Arr::get($args, 'baseUrl')) {
72
            $args['base'] = $baseUrl.'%_%';
73
        }
74
        $args = wp_parse_args(array_filter($args), [
75
            'current' => glsr(QueryBuilder::class)->getPaged(),
76
            'total' => 1,
77
        ]);
78
        $args = glsr(Style::class)->paginationArgs($args);
79
        if ('array' == $args['type']) {
80
            $args['type'] = 'plain';
81
        }
82
        return $args;
83
    }
84
}
85