View::getNext()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Bundle\PaginationBundle\Service;
12
13
use Doctrine\Common\Collections\ArrayCollection;
14
use GpsLab\Bundle\PaginationBundle\Entity\Node;
15
16
class View implements \IteratorAggregate
17
{
18
    /**
19
     * @var Configuration
20
     */
21
    private $config;
22
23
    /**
24
     * @var NavigateRange
25
     */
26
    private $range;
27
28
    /**
29
     * @var Node|null
30
     */
31
    private $first;
32
33
    /**
34
     * @var Node|null
35
     */
36
    private $prev;
37
38
    /**
39
     * @var Node|null
40
     */
41
    private $current;
42
43
    /**
44
     * @var Node|null
45
     */
46
    private $next;
47
48
    /**
49
     * @var Node|null
50
     */
51
    private $last;
52
53
    /**
54
     * @var ArrayCollection|null
55
     */
56
    private $list;
57
58
    /**
59
     * @param Configuration $config
60
     * @param NavigateRange $range
61
     */
62 28
    public function __construct(Configuration $config, NavigateRange $range)
63
    {
64 28
        $this->config = $config;
65 28
        $this->range = $range;
66 28
    }
67
68
    /**
69
     * @return int
70
     */
71 15
    public function getTotal()
72
    {
73 15
        return $this->config->getTotalPages();
74
    }
75
76
    /**
77
     * @return Node|null
78
     */
79 5
    public function getFirst()
80
    {
81 5
        if (!$this->first && $this->config->getCurrentPage() > 1) {
82 4
            $this->first = new Node(1, $this->buildLink(1));
83
        }
84
85 5
        return $this->first;
86
    }
87
88
    /**
89
     * @return Node|null
90
     */
91 3
    public function getPrev()
92
    {
93 3
        if (!$this->prev && $this->config->getCurrentPage() > 1) {
94 2
            $this->prev = new Node(
95 2
                $this->config->getCurrentPage() - 1,
96 2
                $this->buildLink($this->config->getCurrentPage() - 1)
97
            );
98
        }
99
100 3
        return $this->prev;
101
    }
102
103
    /**
104
     * @return Node
105
     */
106 4
    public function getCurrent()
107
    {
108 4
        if (!$this->current) {
109 4
            $this->current = new Node(
110 4
                $this->config->getCurrentPage(),
111 4
                $this->buildLink($this->config->getCurrentPage()),
112 4
                true
113
            );
114
        }
115
116 4
        return $this->current;
117
    }
118
119
    /**
120
     * @return Node|null
121
     */
122 3
    public function getNext()
123
    {
124 3
        if (!$this->next && $this->config->getCurrentPage() < $this->getTotal()) {
125 2
            $this->next = new Node(
126 2
                $this->config->getCurrentPage() + 1,
127 2
                $this->buildLink($this->config->getCurrentPage() + 1)
128
            );
129
        }
130
131 3
        return $this->next;
132
    }
133
134
    /**
135
     * @return Node|null
136
     */
137 3
    public function getLast()
138
    {
139 3
        if (!$this->last && $this->config->getCurrentPage() < $this->getTotal()) {
140 2
            $this->last = new Node($this->getTotal(), $this->buildLink($this->getTotal()));
141
        }
142
143 3
        return $this->last;
144
    }
145
146
    /**
147
     * @return ArrayCollection|Node[]
148
     */
149 8
    public function getIterator()
150
    {
151 8
        if (!($this->list instanceof ArrayCollection)) {
152 8
            $this->list = new ArrayCollection();
153
154 8
            if ($this->getTotal() > 1) {
155
                // determining the first and last pages in paging based on the current page and offset
156 7
                $page = $this->config->getCurrentPage() - $this->range->getLeftOffset();
157 7
                $page_to = $this->config->getCurrentPage() + $this->range->getRightOffset();
158
159 7
                while ($page <= $page_to) {
160 7
                    $this->list->add(new Node(
161 7
                        $page,
162 7
                        $this->buildLink($page),
163 7
                        $page === $this->config->getCurrentPage()
164
                    ));
165 7
                    ++$page;
166
                }
167
            }
168
        }
169
170 8
        return $this->list;
171
    }
172
173
    /**
174
     * @param int $page
175
     *
176
     * @return string
177
     */
178 21
    private function buildLink($page)
179
    {
180 21
        if ($page === 1 && $this->config->getFirstPageLink()) {
181 4
            return $this->config->getFirstPageLink();
182
        }
183
184 17
        if (is_callable($this->config->getPageLink())) {
185 6
            return call_user_func($this->config->getPageLink(), $page);
186
        }
187
188 11
        return sprintf($this->config->getPageLink(), $page);
189
    }
190
}
191