Completed
Push — master ( 6292d2...03a8c3 )
by Peter
7s
created

NavigateRange::getRightOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
/**
12
 * NavigateRange.
13
 */
14
class NavigateRange
15
{
16
    /**
17
     * @var Configuration
18
     */
19
    protected $config;
20
21
    /**
22
     * @var int
23
     */
24
    protected $left_offset = -1;
25
26
    /**
27
     * @var int
28
     */
29
    protected $right_offset = -1;
30
31
    /**
32
     * @param Configuration $config
33
     */
34 12
    public function __construct(Configuration $config)
35
    {
36 12
        $this->config = $config;
37
    }
38
39
    /**
40
     * @return int
41
     */
42 1
    public function getLeftOffset()
43
    {
44 1
        return $this->buildOffset()->left_offset;
45
    }
46
47
    /**
48
     * @return int
49
     */
50 1
    public function getRightOffset()
51
    {
52 1
        return $this->buildOffset()->right_offset;
53
    }
54
55
    /**
56
     * @return NavigateRange
57
     */
58 4
    protected function buildOffset()
59
    {
60
        if ($this->config->getTotalPages() == 1) {
61
            throw new \RangeException('Impossible calculate offsets from empty pages list');
62
        }
63
64
        if ($this->left_offset < 0 && $this->right_offset < 0) {
65
            // definition of offset to the left and to the right of the selected page
66
            $this->left_offset = (int) floor(($this->config->getMaxNavigate() - 1) / 2);
67
            $this->right_offset = (int) ceil(($this->config->getMaxNavigate() - 1) / 2);
68
69
            // adjustment, if the offset is too large left
70
            if ($this->config->getCurrentPage() - $this->left_offset < 1) {
71
                $offset = (int) abs($this->config->getCurrentPage() - 1 - $this->left_offset);
72 4
                $this->left_offset = $this->left_offset - $offset;
73 4
                $this->right_offset = $this->right_offset + $offset;
74
            }
75
76
            // adjustment, if the offset is too large right
77
            if ($this->config->getCurrentPage() + $this->right_offset > $this->config->getTotalPages()) {
78
                $offset = (int) abs($this->config->getTotalPages() - $this->config->getCurrentPage() - $this->right_offset);
79 4
                $this->left_offset = $this->left_offset + $offset;
80 4
                $this->right_offset = $this->right_offset - $offset;
81
            }
82
83
            // left offset should point not lower of the first page
84
            if ($this->left_offset >= $this->config->getCurrentPage()) {
85
                $this->left_offset = $this->config->getCurrentPage() - 1;
86
            }
87
        }
88
89
        return $this;
90
    }
91
}
92