Completed
Push — master ( 2d8113...25118b )
by Peter
02:55
created

NavigateRange   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 8
c 8
b 1
f 0
lcom 1
cbo 1
dl 0
loc 74
ccs 26
cts 26
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getLeftOffset() 0 4 1
A getRightOffset() 0 4 1
B buildOffset() 0 29 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
class NavigateRange
12
{
13
    /**
14
     * @var Configuration
15
     */
16
    protected $config;
17
18
    /**
19
     * @var int
20
     */
21
    protected $left_offset = -1;
22
23
    /**
24
     * @var int
25
     */
26
    protected $right_offset = -1;
27
28
    /**
29
     * @param Configuration $config
30
     */
31 11
    public function __construct(Configuration $config)
32
    {
33 11
        $this->config = $config;
34 11
    }
35
36
    /**
37
     * @return int
38
     */
39 10
    public function getLeftOffset()
40
    {
41 10
        return $this->buildOffset()->left_offset;
42
    }
43
44
    /**
45
     * @return int
46
     */
47 10
    public function getRightOffset()
48
    {
49 10
        return $this->buildOffset()->right_offset;
50
    }
51
52
    /**
53
     * @return NavigateRange
54
     */
55 10
    protected function buildOffset()
56
    {
57 10
        if ($this->left_offset < 0) {
58
            // definition of offset to the left and to the right of the selected page
59 10
            $this->left_offset = (int) floor(($this->config->getMaxNavigate() - 1) / 2);
60 10
            $this->right_offset = (int) ceil(($this->config->getMaxNavigate() - 1) / 2);
61
62
            // adjustment, if the offset is too large left
63 10
            if ($this->config->getCurrentPage() - $this->left_offset < 1) {
64 5
                $offset = (int) abs($this->config->getCurrentPage() - 1 - $this->left_offset);
65 5
                $this->left_offset = $this->left_offset - $offset;
66 5
                $this->right_offset = $this->right_offset + $offset;
67 5
            }
68
69
            // adjustment, if the offset is too large right
70 10
            if ($this->config->getCurrentPage() + $this->right_offset > $this->config->getTotalPages()) {
71 5
                $offset = (int) abs($this->config->getTotalPages() - $this->config->getCurrentPage() - $this->right_offset);
72 5
                $this->left_offset = $this->left_offset + $offset;
73 5
                $this->right_offset = $this->right_offset - $offset;
74 5
            }
75
76
            // left offset should point not lower of the first page
77 10
            if ($this->left_offset >= $this->config->getCurrentPage()) {
78 3
                $this->left_offset = $this->config->getCurrentPage() - 1;
79 3
            }
80 10
        }
81
82 10
        return $this;
83
    }
84
}
85