Completed
Push — dev ( 72cd7e...c5a23f )
by James Ekow Abaka
05:21
created

PaginationHelper::getLink()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 2
nop 1
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
    private $halfNumberOfLinks;
56
    
57
    public function __construct()
58
    {
59
        TemplateEngine::appendPath(
60
            __DIR__ . "/../../templates/pagination"
61
        );
62
    } 
63
64
    public function help($params = array())
65
    {
66
        $this->parameters = array_merge($this->parameters, $params);
67
        $this->halfNumberOfLinks = ceil($this->parameters['number_of_links'] / 2);
68
        if($this->parameters['query'] != '')
69
        {
70
            $this->parameters['page_number'] = Input::get($this->parameters['query']) == '' ? 1 : Input::get($this->parameters['query']);
71
        }
72
        return $this;
73
    }
74
    
75
    
76
    private function getLink($index)
77
    {
78
        if($this->parameters['query'] == '')
79
        {
80
            $link = $this->parameters['base_url'] . $index;
81
        }
82
        else
83
        {
84
            $link = $this->parameters['base_url'] . '?';
85
            $get = Input::get();
86
            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...
87
            {
88
                if($key == $this->parameters['query']) continue;
89
                $link .= "$key=" . urlencode($value) . '&';
90
            }
91
            $link .= "{$this->parameters['query']}=$index";
92
        }
93
        return $link;
94
    }
95
96
    public function __toString()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
97
    {
98
        $pagingLinks = array();
99 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...
100
        {
101
            $pagingLinks[] = array(
102
                "link" => $this->getLink($this->parameters['page_number']- 1),
103
                "label" => "< Prev",
104
                "selected" => false
105
            );
106
        }
107
108
        if($this->parameters['number_of_pages'] <= $this->parameters['number_of_links'] || $this->parameters['page_number']< $this->halfNumberOfLinks)
109
        {
110
            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++)
111
            {
112
                
113
                $pagingLinks[] = array(
114
                    "link" => $this->getLink($i),
115
                    "label" => "$i",
116
                    "selected" => $this->parameters['page_number']== $i
117
                );
118
            }
119
        }
120
        else
121
        {
122
            if($this->parameters['number_of_pages'] - $this->parameters['page_number']< $this->halfNumberOfLinks)
123
            {
124
                $startOffset = $this->parameters['page_number']- (($this->parameters['number_of_links'] - 1) - ($this->parameters['number_of_pages'] - $this->parameters['page_number']));
125
                $endOffset = $this->parameters['page_number']+ ($this->parameters['number_of_pages'] - $this->parameters['page_number']);
126
            }
127
            else
128
            {
129
                $startOffset = $this->parameters['page_number']- ($this->halfNumberOfLinks - 1);
130
                $endOffset = $this->parameters['page_number']+ ($this->halfNumberOfLinks - 1);
131
            }
132
            for($i = $startOffset ; $i <= $endOffset; $i++)
133
            {
134
                $pagingLinks[] = array(
135
                    "link" => $this->getLink($i),
136
                    "label" => "$i",
137
                    "selected" => $this->parameters['page_number']== $i
138
                );
139
            }
140
        }
141
142 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...
143
        {
144
            $pagingLinks[] = array(
145
                "link" => $this->getLink($this->parameters['page_number']+ 1),
146
                "label" => "Next >",
147
                "selected" => false
148
            );
149
        }
150
        
151
        return TemplateEngine::render("links.tpl.php", array('links' => $pagingLinks));
152
    }
153
}
154