Completed
Pull Request — master (#4)
by James Ekow Abaka
03:06 queued 36s
created

PaginationHelper::__toString()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 56
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 30
nc 16
nop 0
dl 0
loc 56
ccs 27
cts 27
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Common utilities file for the Ntentan framework. This file contains a 
4
 * collection of utility static methods which are used accross the framework.
5
 * 
6
 * Ntentan Framework
7
 * Copyright (c) 2008-2014 James Ekow Abaka Ainooson
8
 * 
9
 * Permission is hereby granted, free of charge, to any person obtaining
10
 * a copy of this software and associated documentation files (the
11
 * "Software"), to deal in the Software without restriction, including
12
 * without limitation the rights to use, copy, modify, merge, publish,
13
 * distribute, sublicense, and/or sell copies of the Software, and to
14
 * permit persons to whom the Software is furnished to do so, subject to
15
 * the following conditions:
16
 * 
17
 * The above copyright notice and this permission notice shall be
18
 * included in all copies or substantial portions of the Software.
19
 * 
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
27
 * 
28
 * @author James Ainooson <[email protected]>
29
 * @copyright Copyright 2014 James Ekow Abaka Ainooson
30
 * @license MIT
31
 */
32
33
namespace ntentan\honam\engines\php\helpers;
34
35
use ntentan\honam\engines\php\Helper;
36
use ntentan\utils\Input;
37
38
class PaginationHelper extends Helper
39
{
40
    private $parameters = [
41
        'query' => null,
42
        'number_of_links' => 21,
43
        'number_of_pages' => null,
44
        'base_url' => null,
45
        'page_number' => 1
46
    ];
47
48
    private $halfNumberOfLinks;
49
50 5
    public function help($params = [])
51
    {
52 5
        $this->parameters = array_merge($this->parameters, $params);
53 5
        $this->halfNumberOfLinks = ceil($this->parameters['number_of_links'] / 2);
54 5
        if($this->parameters['query'] != '')
55
        {
56 1
            $this->parameters['page_number'] = Input::get($this->parameters['query']) == '' ? 1 : Input::get($this->parameters['query']);
57
        }
58 5
        return $this;
59
    }
60
    
61
    
62 4
    private function getLink($index)
63
    {
64 4
        if($this->parameters['query'] == '')
65
        {
66 3
            $link = $this->parameters['base_url'] . $index;
67
        }
68
        else
69
        {
70 1
            $link = $this->parameters['base_url'] . '?';
71 1
            $get = Input::get();
72 1
            foreach($get as $key => $value)
73
            {
74
                if($key == $this->parameters['query']) continue;
75
                $link .= "$key=" . urlencode($value) . '&';
76
            }
77 1
            $link .= "{$this->parameters['query']}=$index";
78
        }
79 4
        return $link;
80
    }
81
82 5
    public function __toString()
83
    {
84 5
        $pagingLinks = array();
85 5
        if($this->parameters['page_number']> 1)
86
        {
87 2
            $pagingLinks[] = array(
88 2
                "link" => $this->getLink($this->parameters['page_number']- 1),
89 2
                "label" => "< Prev",
90
                "selected" => false
91
            );
92
        }
93
94 5
        if($this->parameters['number_of_pages'] <= $this->parameters['number_of_links'] || $this->parameters['page_number']< $this->halfNumberOfLinks)
95
        {
96 3
            for($i = 1; $i <= ($this->parameters['number_of_pages'] > $this->parameters['number_of_links'] ? $this->parameters['number_of_links'] : $this->parameters['number_of_pages']) ; $i++)
97
            {
98
                
99 2
                $pagingLinks[] = array(
100 2
                    "link" => $this->getLink($i),
101 2
                    "label" => "$i",
102 2
                    "selected" => $this->parameters['page_number']== $i
103
                );
104
            }
105
        }
106
        else
107
        {
108 2
            if($this->parameters['number_of_pages'] - $this->parameters['page_number']< $this->halfNumberOfLinks)
109
            {
110 1
                $startOffset = $this->parameters['page_number']- (($this->parameters['number_of_links'] - 1) - ($this->parameters['number_of_pages'] - $this->parameters['page_number']));
111 1
                $endOffset = $this->parameters['page_number']+ ($this->parameters['number_of_pages'] - $this->parameters['page_number']);
112
            }
113
            else
114
            {
115 1
                $startOffset = $this->parameters['page_number']- ($this->halfNumberOfLinks - 1);
116 1
                $endOffset = $this->parameters['page_number']+ ($this->halfNumberOfLinks - 1);
117
            }
118 2
            for($i = $startOffset ; $i <= $endOffset; $i++)
119
            {
120 2
                $pagingLinks[] = array(
121 2
                    "link" => $this->getLink($i),
122 2
                    "label" => "$i",
123 2
                    "selected" => $this->parameters['page_number']== $i
124
                );
125
            }
126
        }
127
128 5
        if($this->parameters['page_number']< $this->parameters['number_of_pages'])
129
        {
130 4
            $pagingLinks[] = array(
131 4
                "link" => $this->getLink($this->parameters['page_number']+ 1),
132 4
                "label" => "Next >",
133
                "selected" => false
134
            );
135
        }
136
        
137 5
        return $this->templateRenderer->render("links.tpl.php", array('links' => $pagingLinks));
138
    }
139
}
140