Passed
Pull Request — master (#3)
by Flavien
03:30
created

PaginationAndSortingFeaturesTrait::addOrderBy()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 3
crap 4.074
1
<?php
2
3
namespace QualityCode\ApiFeaturesBundle\Repository;
4
5
use Pagerfanta\Adapter\DoctrineORMAdapter;
6
use Pagerfanta\Pagerfanta;
7
use Doctrine\ORM\QueryBuilder;
8
9
trait PaginationAndSortingFeaturesTrait
10
{
11
    /**
12
     * Creates a new QueryBuilder instance that is prepopulated for this entity name.
13
     *
14
     * @param string $alias
15
     * @param string $indexBy The index for the from
16
     *
17
     * @return QueryBuilder
18
     */
19
    abstract public function createQueryBuilder($alias, $indexBy = null);
20
21
    /**
22
     * @return \Doctrine\ORM\Mapping\ClassMetadata
23
     */
24
    abstract protected function getClassMetadata();
25
26
    /**
27
     * @param int   $limit
28
     * @param int   $page
29
     * @param array $sorting
30
     * @param array $searching
31
     *
32
     * @return Pagerfanta
33
     */
34 1
    public function findAllPaginated($limit, $page, array $sorting = array(), array $searching = array())
35
    {
36 1
        $fields = array_keys($this->getClassMetadata()->fieldMappings);
37 1
        $queryBuilder = $this->createQueryBuilder('r');
38
39 1
        $this->addOrderBy($queryBuilder, $fields, $sorting)->addSearchBy($queryBuilder, $fields, $searching);
40
41 1
        $pagerAdapter = new DoctrineORMAdapter($queryBuilder);
42 1
        $pager = new Pagerfanta($pagerAdapter);
43 1
        $pager->setCurrentPage($page);
44 1
        $pager->setMaxPerPage($limit);
45
46 1
        return $pager;
47
    }
48
49
    /**
50
     * @param QueryBuilder $queryBuilder
51
     * @param array        $fields
52
     * @param array        $sorting
53
     *
54
     * @return QueryBuilder
55
     */
56 1
    protected function addOrderBy(QueryBuilder &$queryBuilder, array $fields, array $sorting)
57
    {
58 1
        foreach ($fields as $field) {
59 1
            if (isset($sorting[$field])) {
60
                $direction = ($sorting[$field] === 'asc') ? 'asc' : 'desc';
61 1
                $queryBuilder->addOrderBy('r.'.$field, $direction);
62
            }
63
        }
64
65 1
        return $this;
66
    }
67
68
    /**
69
     * @param QueryBuilder $queryBuilder
70
     * @param array        $fields
71
     * @param array        $searching
72
     *
73
     * @return QueryBuilder
74
     */
75 1
    protected function addSearchBy(QueryBuilder &$queryBuilder, array $fields, array $searching)
76
    {
77 1
        foreach ($fields as $field) {
78 1
            if (isset($searching[$field])) {
79
                $queryBuilder->orWhere($queryBuilder->expr()->like('r.'.$field, ':'.$field));
80 1
                $queryBuilder->setParameter($field, '%'.$searching[$field].'%');
81
            }
82
        }
83
84 1
        return $this;
85
    }
86
}
87