Passed
Pull Request — master (#150)
by Arnaud
07:48
created

ResultsHandler::handle()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
c 1
b 0
f 0
nc 5
nop 4
dl 0
loc 25
rs 9.2222
1
<?php
2
3
namespace LAG\AdminBundle\Bridge\Doctrine\ORM\Results;
4
5
use Doctrine\ORM\Query;
6
use Doctrine\ORM\QueryBuilder;
7
use Pagerfanta\Adapter\ArrayAdapter;
8
use Pagerfanta\Adapter\DoctrineORMAdapter;
9
use Pagerfanta\Pagerfanta;
10
11
class ResultsHandler implements ResultsHandlerInterface
12
{
13
    public function handle($data, bool $pagination, int $page = 1, int $maxPerPage = 25)
14
    {
15
        if ('pagerfanta' === $pagination) {
0 ignored issues
show
introduced by
The condition 'pagerfanta' === $pagination is always false.
Loading history...
16
17
            if ($data instanceof QueryBuilder || $data instanceof Query) {
18
                $adapter = new DoctrineORMAdapter($data, true, true);
19
            } else {
20
                $adapter = new ArrayAdapter($data);
21
            }
22
            $pager = new Pagerfanta($adapter);
23
            $pager->setCurrentPage($page);
24
            $pager->setMaxPerPage($maxPerPage);
25
26
            return $pager;
27
        }
28
29
        if ($data instanceof Query) {
30
            return $data->getResult();
31
        }
32
33
        if ($data instanceof QueryBuilder) {
34
            return $data->getQuery()->getResult();
35
        }
36
37
        return $data;
38
    }
39
}
40