Completed
Pull Request — master (#90)
by Arnaud
03:09 queued 01:17
created

DoctrineRepository::validateEntity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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