Completed
Push — dev ( a486ee...7113e4 )
by James Ekow Abaka
03:55
created

PaginationHelper::help()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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
37
use ntentan\honam\TemplateEngine;
38
use ntentan\utils\Input;
39
40
class PaginationHelper extends Helper
41
{
42
    /*private $pageNumber;
43
    private $numberOfPages;
44
    private $baseRoute;
45
    private $numberOfLinks;
46
    private $query;*/
47
    
48
    private $parameters = array(
49
        'query' => null,
50
        'number_of_links' => 21,
51
        'number_of_pages' => null,
52
        'base_url' => null,
53
        'page_number' => 1
54
    );
55
56
    private $halfNumberOfLinks;
57
58 5
    public function help($params = array())
59
    {
60 5
        $this->parameters = array_merge($this->parameters, $params);
61 5
        $this->halfNumberOfLinks = ceil($this->parameters['number_of_links'] / 2);
62 5
        if($this->parameters['query'] != '')
63
        {
64 1
            $this->parameters['page_number'] = Input::get($this->parameters['query']) == '' ? 1 : Input::get($this->parameters['query']);
65
        }
66 5
        return $this;
67
    }
68
    
69
    
70 4
    private function getLink($index)
71
    {
72 4
        if($this->parameters['query'] == '')
73
        {
74 3
            $link = $this->parameters['base_url'] . $index;
75
        }
76
        else
77
        {
78 1
            $link = $this->parameters['base_url'] . '?';
79 1
            $get = Input::get();
80 1
            foreach($get as $key => $value)
0 ignored issues
show
Bug introduced by
The expression $get of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
81
            {
82
                if($key == $this->parameters['query']) continue;
83
                $link .= "$key=" . urlencode($value) . '&';
84
            }
85 1
            $link .= "{$this->parameters['query']}=$index";
86
        }
87 4
        return $link;
88
    }
89
90 5
    public function __toString()
91
    {
92 5
        $pagingLinks = array();
93 5 View Code Duplication
        if($this->parameters['page_number']> 1)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
        {
95 2
            $pagingLinks[] = array(
96 2
                "link" => $this->getLink($this->parameters['page_number']- 1),
97 2
                "label" => "< Prev",
98
                "selected" => false
99
            );
100
        }
101
102 5
        if($this->parameters['number_of_pages'] <= $this->parameters['number_of_links'] || $this->parameters['page_number']< $this->halfNumberOfLinks)
103
        {
104 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++)
105
            {
106
                
107 2
                $pagingLinks[] = array(
108 2
                    "link" => $this->getLink($i),
109 2
                    "label" => "$i",
110 2
                    "selected" => $this->parameters['page_number']== $i
111
                );
112
            }
113
        }
114
        else
115
        {
116 2
            if($this->parameters['number_of_pages'] - $this->parameters['page_number']< $this->halfNumberOfLinks)
117
            {
118 1
                $startOffset = $this->parameters['page_number']- (($this->parameters['number_of_links'] - 1) - ($this->parameters['number_of_pages'] - $this->parameters['page_number']));
119 1
                $endOffset = $this->parameters['page_number']+ ($this->parameters['number_of_pages'] - $this->parameters['page_number']);
120
            }
121
            else
122
            {
123 1
                $startOffset = $this->parameters['page_number']- ($this->halfNumberOfLinks - 1);
124 1
                $endOffset = $this->parameters['page_number']+ ($this->halfNumberOfLinks - 1);
125
            }
126 2
            for($i = $startOffset ; $i <= $endOffset; $i++)
127
            {
128 2
                $pagingLinks[] = array(
129 2
                    "link" => $this->getLink($i),
130 2
                    "label" => "$i",
131 2
                    "selected" => $this->parameters['page_number']== $i
132
                );
133
            }
134
        }
135
136 5 View Code Duplication
        if($this->parameters['page_number']< $this->parameters['number_of_pages'])
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
        {
138 4
            $pagingLinks[] = array(
139 4
                "link" => $this->getLink($this->parameters['page_number']+ 1),
140 4
                "label" => "Next >",
141
                "selected" => false
142
            );
143
        }
144
        
145 5
        return $this->templateRenderer->render("links.tpl.php", array('links' => $pagingLinks));
146
    }
147
}
148