Completed
Push — master ( 73be91...dbc7b1 )
by Mihail
03:08
created

SimplePagination   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 5 Features 0
Metric Value
wmc 17
c 6
b 5
f 0
lcom 1
cbo 3
dl 0
loc 143
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
C display() 0 70 7
B setUrlPage() 0 25 5
A generateItems() 0 14 3
1
<?php
2
3
namespace Ffcms\Core\Helper\HTML;
4
5
use Ffcms\Core\Helper\Type\Arr;
6
use Ffcms\Core\Helper\Type\Obj;
7
8
/**
9
 * Class SimplePagination
10
 * @package Ffcms\Core\Helper\HTML
11
 */
12
class SimplePagination
13
{
14
    protected $url;
15
    protected $page;
16
    protected $step;
17
    protected $total;
18
19
    public function __construct(array $elements)
20
    {
21
        $this->url = $elements['url'];
22
        $this->page = (int)$elements['page'];
23
        $this->step = (int)$elements['step'] > 0 ? (int)$elements['step'] : 5;
24
        $this->total = (int)$elements['total'];
25
    }
26
27
    /**
28
     * Display pagination html code. Must be initialized via (new SimplePagination([params]))->display(['class' => 'test'])
29
     * @param array|null $property
30
     * @return null|string
31
     */
32
    public function display(array $property = null)
33
    {
34
        // total items is less to pagination requirement
35
        if (($this->page * $this->step) + 1 > $this->total) {
36
            return null;
37
        }
38
39
        $lastPage = ceil($this->total / $this->step); // 6/5 ~ 2 = 0..2
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
40
41
        if ($lastPage <= 1) {
42
            return null;
43
        }
44
45
        // prevent hack-boy's any try
46
        if ($this->page > $lastPage) {
47
            return null;
48
        }
49
50
        $items = [];
0 ignored issues
show
Unused Code introduced by
$items is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
        // more then 10 items in pagination
52
        if ($lastPage > 10) {
53
            if ($this->page < 4 || $lastPage - $this->page <= 4) { // list start, page in [0..4]
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
54
                // from start to 4
55
                $items = $this->generateItems(0, 4);
56
57
                // add "..." button
58
                $items[] = [
59
                    'type' => 'link', 'link' => '#', 'text' => '...', 'property' => ['class' => 'disabled']
60
                ];
61
                // add middle page
62
                $middlePage = ceil($lastPage / 2);
63
                $items[] = [
64
                    'type' => 'link', 'link' => $this->setUrlPage($middlePage), 'text' => $middlePage + 1
65
                ];
66
                // add "..." button
67
                $items[] = [
68
                    'type' => 'link', 'link' => '#', 'text' => '...', 'property' => ['class' => 'disabled']
69
                ];
70
                $items = Arr::merge($items, $this->generateItems($lastPage - 4, $lastPage));
71
            } else { // meanwhile on middle
72
                // generate 1-2 pages
73
                $items = $this->generateItems(0, 2);
74
75
                // add "..." button
76
                $items[] = [
77
                    'type' => 'link', 'link' => '#', 'text' => '...', 'property' => ['class' => 'disabled']
78
                ];
79
80
                // add middle variance -3..mid..+3
81
                $items = Arr::merge($items, $this->generateItems($this->page - 3, $this->page + 3));
82
83
                // add "..." button
84
                $items[] = [
85
                    'type' => 'link', 'link' => '#', 'text' => '...', 'property' => ['class' => 'disabled']
86
                ];
87
88
                // add latest 2 items
89
                $items = Arr::merge($items, $this->generateItems($lastPage - 2, $lastPage));
90
            }
91
92
        } else { // less then 10 items in pagination
93
            $items = $this->generateItems(0, $lastPage);
94
        }
95
96
        return Listing::display([
97
            'type' => 'ul',
98
            'property' => $property,
99
            'items' => $items
100
        ]);
101
    }
102
103
    /**
104
     * Set GET param to property array
105
     * @param int|string $page_id
106
     * @return array
107
     */
108
    protected function setUrlPage($page_id)
109
    {
110
        $url = $this->url;
111
        switch (count($url)) { // check nulls if not set
112
            case 1: // only controller/action is defined
113
                $url[1] = null;
114
                $url[2] = null;
115
                break;
116
            case 2:
117
                $url[2] = null;
118
                break;
119
        }
120
121
        // add page param if > 0
122
        if ((int)$page_id > 0) {
123
            // merge with ?page if query is not empty
124
            if (Obj::isArray($url[3])) {
125
                $url[3] = Arr::merge($url[3], ['page' => $page_id]);
126
            } else { // just set ?page=$page_id
127
                $url[3] = ['page' => $page_id];
128
            }
129
        }
130
131
        return $url;
132
    }
133
134
    /**
135
     * Generate item array to build pagination listing
136
     * @param int $start
137
     * @param int $end
138
     * @return array|null
139
     */
140
    protected function generateItems($start, $end)
141
    {
142
        // prevent any shit's
143
        if ($end <= $start) {
144
            return null;
145
        }
146
        $items = [];
147
        for ($i = $start; $i < $end; $i++) {
148
            $items[] = [
149
                'type' => 'link', 'link' => $this->setUrlPage($i), 'text' => $i + 1
150
            ];
151
        }
152
        return $items;
153
    }
154
}