Completed
Push — master ( d9caba...686839 )
by Philip
08:01
created

OrmEntityRepository::removeAll()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 1
nop 2
crap 3
1
<?php
2
3
namespace Dontdrinkandroot\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\Mapping\ClassMetadata;
7
use Doctrine\ORM\QueryBuilder;
8
use Dontdrinkandroot\Entity\EntityInterface;
9
use Dontdrinkandroot\Pagination\PaginatedResult;
10
use Dontdrinkandroot\Pagination\Pagination;
11
12
class OrmEntityRepository extends EntityRepository implements EntityRepositoryInterface
13
{
14
    protected $transactionManager;
15
16 10
    public function __construct($entityManager, ClassMetadata $classMetadata)
17
    {
18 10
        parent::__construct($entityManager, $classMetadata);
19 10
        $this->transactionManager = new TransactionManager($entityManager);
20 10
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 3
    public function persist($entity, $flush = true)
26
    {
27 3
        return $this->transactionManager->transactional(
28
            function () use ($entity, $flush) {
29 3
                $this->getEntityManager()->persist($entity);
30
31 3
                if ($flush) {
32 3
                    $this->getEntityManager()->flush($entity);
33
                }
34
35 3
                return $entity;
36 3
            }
37
        );
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 2
    public function merge($entity, $flush = false)
44
    {
45 2
        return $this->transactionManager->transactional(
46
            function () use ($entity, $flush) {
47 2
                $entity = $this->getEntityManager()->merge($entity);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $entity, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
48
49 2
                if ($flush) {
50
                    $this->getEntityManager()->flush($entity);
51
                }
52
53 2
                return $entity;
54 2
            }
55
        );
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 2
    public function flush($entity = null)
62
    {
63 2
        $this->getEntityManager()->flush($entity);
64 2
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function detach($entity)
70
    {
71
        $this->getEntityManager()->detach($entity);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 3
    public function remove($entity, $flush = false)
78
    {
79 3
        return $this->transactionManager->transactional(
80
            function () use ($entity, $flush) {
81 3
                $this->getEntityManager()->remove($entity);
82
83 3
                if ($flush) {
84
                    $this->getEntityManager()->flush($entity);
85
                }
86 3
            }
87
        );
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 1
    public function removeById($id, $flush = false)
94
    {
95 1
        return $this->transactionManager->transactional(
96
            function () use ($id, $flush) {
97
                /** @var EntityInterface $entity */
98 1
                $entity = $this->find($id);
99 1
                $this->remove($entity, $flush);
100 1
            }
101
        );
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 2
    public function removeAll($flush = false, $iterate = true)
108
    {
109 2
        return $this->transactionManager->transactional(
110
            function () use ($flush, $iterate) {
111 2
                if ($iterate) {
112 1
                    $this->removeAllByIterating();
113
                } else {
114 1
                    $this->removeAllByQuery();
115
                }
116
117 2
                if ($flush) {
118 2
                    $this->getEntityManager()->flush();
119
                }
120 2
            }
121
        );
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getTransactionManager()
128
    {
129
        return $this->transactionManager;
130
    }
131
132 1
    protected function removeAllByIterating($batchSize = 100)
133
    {
134 1
        return $this->transactionManager->transactional(
135
            function () use ($batchSize) {
136 1
                $entities = $this->findAll();
137 1
                $count = 0;
138 1
                foreach ($entities as $entity) {
139 1
                    $this->remove($entity, false);
140 1
                    $count++;
141 1
                    if ($count >= $batchSize) {
142
                        $this->getEntityManager()->flush();
143 1
                        $count = 0;
144
                    }
145
                }
146 1
            }
147
        );
148
    }
149
150 1
    protected function removeAllByQuery()
151
    {
152 1
        return $this->transactionManager->transactional(
153
            function () {
154 1
                $queryBuilder = $this->createQueryBuilder('entity');
155 1
                $queryBuilder->delete();
156 1
                $query = $queryBuilder->getQuery();
157 1
                $query->execute();
158 1
            }
159
        );
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165 1
    public function findPaginatedBy($page = 1, $perPage = 10, array $criteria = [], array $orderBy = null)
166
    {
167 1
        return $this->transactionManager->transactional(
168
            function () use ($page, $perPage, $criteria, $orderBy) {
169 1
                $persister = $this->getEntityManager()->getUnitOfWork()->getEntityPersister($this->_entityName);
170 1
                $total = $this->count($criteria);
171 1
                $results = $persister->loadAll($criteria, $orderBy, $perPage, ($page - 1) * $perPage);
172
173 1
                $pagination = new Pagination($page, $perPage, $total);
174 1
                $paginatedResult = new PaginatedResult($pagination, $results);
175
176 1
                return $paginatedResult;
177 1
            }
178
        );
179
    }
180
181
    /**
182
     * @param array $criteria
183
     *
184
     * @return int
185
     */
186 1
    private function count(array $criteria = [])
187
    {
188
189 1
        $persister = $this->getEntityManager()->getUnitOfWork()->getEntityPersister($this->_entityName);
190
191 1
        if (method_exists($persister, 'count')) {
192 1
            return $persister->count($criteria);
193
        }
194
195
        $queryBuilder = $this->createBlankQueryBuilder();
196
        $queryBuilder
197
            ->select('COUNT(entity)')
198
            ->from($this->getEntityName(), 'entity');
199
200
        if (count($criteria) > 0) {
201
            $expr = $queryBuilder->expr();
202
            foreach ($criteria as $field => $value) {
203
                $queryBuilder->andWhere($expr->eq('entity' . $field, $value));
204
            }
205
        }
206
207
        return (int)$queryBuilder->getQuery()->getSingleScalarResult();
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213 1
    public function countAll()
214
    {
215 1
        return $this->transactionManager->transactional(
216 1
            function () {
217 1
                $queryBuilder = $this->getEntityManager()->createQueryBuilder();
218
                $queryBuilder
219 1
                    ->select('count(entity)')
220 1
                    ->from($this->getClassName(), 'entity');
221
222 1
                $result = $queryBuilder->getQuery()->getSingleScalarResult();
223
224 1
                return $result;
225 1
            }
226
        );
227
    }
228
229
    /**
230
     * @return QueryBuilder
231
     */
232
    protected function createBlankQueryBuilder()
233
    {
234
        return $this->getEntityManager()->createQueryBuilder();
235
    }
236
}
237