Completed
Push — master ( 03a8c3...2d8113 )
by Peter
02:49
created

View::getIterator()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

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