NavigateRange::getLeftOffset()   A
last analyzed

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 0
Metric Value
c 0
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
10
namespace AnimeDb\Bundle\PaginationBundle\Service;
11
12
class NavigateRange
13
{
14
    /**
15
     * @var Configuration
16
     */
17
    protected $config;
18
19
    /**
20
     * @var int
21
     */
22
    protected $left_offset = -1;
23
24
    /**
25
     * @var int
26
     */
27
    protected $right_offset = -1;
28
29
    /**
30
     * @param Configuration $config
31
     */
32 11
    public function __construct(Configuration $config)
33
    {
34 11
        $this->config = $config;
35 11
    }
36
37
    /**
38
     * @return int
39
     */
40 10
    public function getLeftOffset()
41
    {
42 10
        return $this->buildOffset()->left_offset;
43
    }
44
45
    /**
46
     * @return int
47
     */
48 10
    public function getRightOffset()
49
    {
50 10
        return $this->buildOffset()->right_offset;
51
    }
52
53
    /**
54
     * @return NavigateRange
55
     */
56 10
    protected function buildOffset()
57
    {
58 10
        if ($this->left_offset < 0) {
59 10
            $this
60 10
                ->definitionOffset()
61 10
                ->adjustmentLargeLeftOffset()
62 10
                ->adjustmentLargeRightOffset()
63 10
                ->adjustmentLowerLeftOffset();
64 10
        }
65
66 10
        return $this;
67
    }
68
69
    /**
70
     * Definition of offset to the left and to the right of the selected page.
71
     *
72
     * @return NavigateRange
73
     */
74 10
    protected function definitionOffset()
75
    {
76 10
        $this->left_offset = (int) floor(($this->config->getMaxNavigate() - 1) / 2);
77 10
        $this->right_offset = (int) ceil(($this->config->getMaxNavigate() - 1) / 2);
78
79 10
        return $this;
80
    }
81
82
    /**
83
     * Adjustment, if the offset is too large left.
84
     *
85
     * @return NavigateRange
86
     */
87 10
    protected function adjustmentLargeLeftOffset()
88
    {
89 10
        if ($this->config->getCurrentPage() - $this->left_offset < 1) {
90 5
            $offset = (int) abs($this->config->getCurrentPage() - 1 - $this->left_offset);
91 5
            $this->left_offset = $this->left_offset - $offset;
92 5
            $this->right_offset = $this->right_offset + $offset;
93 5
        }
94
95 10
        return $this;
96
    }
97
98
    /**
99
     * Adjustment, if the offset is too large right.
100
     *
101
     * @return NavigateRange
102
     */
103 10
    protected function adjustmentLargeRightOffset()
104
    {
105 10
        if ($this->config->getCurrentPage() + $this->right_offset > $this->config->getTotalPages()) {
106 5
            $offset = (int) abs(
107 5
                $this->config->getTotalPages() -
108 5
                $this->config->getCurrentPage() -
109 5
                $this->right_offset
110 5
            );
111 5
            $this->left_offset = $this->left_offset + $offset;
112 5
            $this->right_offset = $this->right_offset - $offset;
113 5
        }
114
115 10
        return $this;
116
    }
117
118
    /**
119
     * Left offset should point not lower of the first page.
120
     *
121
     * @return NavigateRange
122
     */
123 10
    protected function adjustmentLowerLeftOffset()
124
    {
125 10
        if ($this->left_offset >= $this->config->getCurrentPage()) {
126 3
            $this->left_offset = $this->config->getCurrentPage() - 1;
127 3
        }
128
129 10
        return $this;
130
    }
131
}
132