CustomQueryAdapter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 35
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setLimit() 0 3 1
A getLimit() 0 3 1
A getNbResults() 0 7 3
A getSlice() 0 7 2
A existsMoreItemsThanLimitation() 0 3 1
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