CustomQueryAdapter::getNbResults()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Bugloos\ResponderBundle\PaginatorHandler\Pagerfanta;
4
5
use Pagerfanta\Doctrine\ORM\QueryAdapter;
6
7
/**
8
 * @author Mojtaba Gheytasi <[email protected]>
9
 */
10
class CustomQueryAdapter extends QueryAdapter
11
{
12
    private ?int $limit = null;
13
14
    public function getNbResults(): int
15
    {
16
        if ($this->getLimit() !== null && parent::getNbResults() > $this->getLimit()) {
17
            return $this->getLimit();
18
        }
19
20
        return parent::getNbResults();
21
    }
22
23
    public function setLimit(?int $limit)
24
    {
25
        $this->limit = $limit;
26
    }
27
28
    public function getLimit(): ?int
29
    {
30
        return $this->limit;
31
    }
32
33
    public function getSlice($offset, $length): iterable
34
    {
35
        if ($this->existsMoreItemsThanLimitation($offset, $length)) {
36
            $length = $this->getNbResults() - $offset;
37
        }
38
39
        return parent::getSlice($offset, $length);
40
    }
41
42
    private function existsMoreItemsThanLimitation(int $offset, int $length): bool
43
    {
44
        return $this->getNbResults() < $offset + $length;
45
    }
46
}
47