Completed
Pull Request — master (#90)
by Arnaud
04:05
created

DoctrineRepository::count()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 0
cts 10
cp 0
rs 9.4285
cc 2
eloc 12
nc 2
nop 2
crap 6
1
<?php
2
3
namespace LAG\AdminBundle\Doctrine\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\QueryBuilder;
7
use LAG\AdminBundle\Repository\RepositoryInterface;
8
9
/**
10
 * Abstract doctrine repository
11
 */
12
abstract class DoctrineRepository extends EntityRepository implements RepositoryInterface
13
{
14
    /**
15
     * Save an entity.
16
     *
17
     * @param $entity
18
     */
19
    public function save($entity)
20
    {
21
        $this
22
            ->_em
23
            ->persist($entity)
24
        ;
25
        $this
26
            ->_em
27
            ->flush()
28
        ;
29
    }
30
    /**
31
     * Delete an entity.
32
     *
33
     * @param $entity
34
     */
35
    public function delete($entity)
36
    {
37
        $this
38
            ->_em
39
            ->remove($entity)
40
        ;
41
        $this
42
            ->_em
43
            ->flush()
44
        ;
45
    }
46
    
47
    /**
48
     * @inheritdoc
49
     */
50
    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null, array $options = [])
51
    {
52
        $queryBuilder = $this->createQueryBuilder('entity');
53
        
54
        $this->addCriteria($queryBuilder, $criteria, $options);
55
    
56
        if (null !== $orderBy) {
57
            foreach ($orderBy as $sort => $order) {
58
                $queryBuilder
59
                    ->addOrderBy('entity.'.$sort, $order)
60
                ;
61
            }
62
        }
63
    
64
        if (null !== $limit) {
65
            $queryBuilder->setMaxResults($limit);
66
        }
67
    
68
        if (null !== $offset) {
69
            $queryBuilder->setFirstResult($offset);
70
        }
71
    
72
        return $queryBuilder
73
            ->getQuery()
74
            ->getResult()
75
        ;
76
    }
77
    
78
    /**
79
     * @inheritdoc
80
     */
81
    public function count(array $criteria = [], array $options = [])
82
    {
83
        $identifiers = $this
84
            ->getClassMetadata()
85
            ->getIdentifierColumnNames()
86
        ;
87
        $pieces = [];
88
    
89
        foreach ($identifiers as $identifier) {
90
            $pieces[] = 'entity.'.$identifier;
91
        }
92
        
93
        return $this
94
            ->createQueryBuilder('entity')
95
            ->select('count('.implode(',', $pieces).')')
96
            ->getQuery()
97
            ->getSingleScalarResult()
98
        ;
99
    }
100
    
101
    /**
102
     * Add criteria values from options.
103
     *
104
     * @param QueryBuilder $queryBuilder
105
     * @param array        $criteria
106
     * @param array        $options
107
     */
108
    private function addCriteria(QueryBuilder $queryBuilder, array $criteria, array $options)
109
    {
110
        foreach ($criteria as $criterion => $value) {
111
            // default values
112
            $operator = '=';
113
            
114
            if (array_key_exists($criterion, $options)) {
115
    
116
                if (array_key_exists('operator', $options[$criterion])) {
117
                    $operator = $options[$criterion]['operator'];
118
                }
119
            }
120
    
121
            if ('LIKE' === $operator) {
122
                $value = '%'.$value.'%';
123
            }
124
            $whereString = sprintf(
125
                '%s %s :%s',
126
                'entity.'.$criterion,
127
                $operator,
128
                $criterion.'_value'
129
            );
130
            
131
            $queryBuilder
132
                ->andWhere($whereString)
133
                ->setParameter($criterion.'_value', $value)
134
            ;
135
        }
136
    }
137
}
138