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

ResultsHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 27
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 25 6
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