NavigateRange::adjustmentLowerLeftOffset()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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