Paginator::hasPreviousPage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sylius\Api;
4
5
class Paginator implements PaginatorInterface
6
{
7
    /**
8
     * @var AdapterInterface $adapter
9
     */
10
    private $adapter;
11
    /**
12
     * @var int
13
     */
14
    private $currentPage = 1;
15
    /**
16
     * @var int
17
     */
18
    private $lastPage;
19
    /**
20
     * @var array
21
     */
22
    private $currentResults;
23
    /**
24
     * @var int
25
     */
26
    private $numberOfResults = -1;
27
    /**
28
     * @var array $queryParameters
29
     */
30
    private $queryParameters;
31
    /**
32
     * @var array $uriParameters
33
     */
34
    private $uriParameters;
35
36
    /**
37
     * @param AdapterInterface $adapter
38
     * @param array            $queryParameters
39
     * @param array            $uriParameters
40
     */
41
    public function __construct(AdapterInterface $adapter, array $queryParameters = [], array $uriParameters = [])
42
    {
43
        $queryParameters['limit'] = isset($queryParameters['limit']) ? $queryParameters['limit'] : 10;
44
        if (!is_int($queryParameters['limit'])) {
45
            throw new \InvalidArgumentException('Page limit must an integer!');
46
        }
47
        $this->adapter = $adapter;
48
        $this->queryParameters = $queryParameters;
49
        $this->queryParameters['page'] = $this->currentPage;
50
        $this->uriParameters = $uriParameters;
51
        $this->lastPage = (int) ceil($this->getNumberOfResults() / $queryParameters['limit']);
52
    }
53
54
    public function getCurrentPageResults()
55
    {
56
        if (!$this->isResultCached()) {
57
            $this->queryParameters['page'] = $this->currentPage;
58
            $this->currentResults = $this->adapter->getResults($this->queryParameters, $this->uriParameters);
59
        }
60
61
        return $this->currentResults;
62
    }
63
64
    private function isResultCached()
65
    {
66
        return (null !== $this->currentResults);
67
    }
68
69
    public function previousPage()
70
    {
71
        if (!$this->hasPreviousPage()) {
72
            throw new \LogicException('There is no previous page.');
73
        }
74
        $this->currentPage--;
75
        $this->currentResults = null;
76
    }
77
78
    public function nextPage()
79
    {
80
        if (!$this->hasNextPage()) {
81
            throw new \LogicException('There is no next page.');
82
        }
83
        $this->currentPage++;
84
        $this->currentResults = null;
85
    }
86
87
    public function hasPreviousPage()
88
    {
89
        return (1 < $this->currentPage);
90
    }
91
92
    public function hasNextPage()
93
    {
94
        return ($this->currentPage < $this->lastPage);
95
    }
96
97
    public function getNumberOfResults()
98
    {
99
        if (-1 === $this->numberOfResults) {
100
            $this->numberOfResults = $this->adapter->getNumberOfResults($this->queryParameters, $this->uriParameters);
101
        }
102
103
        return $this->numberOfResults;
104
    }
105
106
    /**
107
     * @return int
108
     */
109
    public function getCurrentPage()
110
    {
111
        return $this->currentPage;
112
    }
113
}
114