Completed
Push — master ( d4ecfc...438ba7 )
by Peter
05:36
created

View::getNavigateRange()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

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