Completed
Push — master ( 07c3ff...33e3c4 )
by Philip
03:17
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 Doctrine\ORM\Tools\Pagination\Paginator;
9
use Dontdrinkandroot\Entity\EntityInterface;
10
11
class OrmEntityRepository extends EntityRepository implements EntityRepositoryInterface
12
{
13
    protected $transactionManager;
14
15 10
    public function __construct($entityManager, ClassMetadata $classMetadata)
16
    {
17 10
        parent::__construct($entityManager, $classMetadata);
18 10
        $this->transactionManager = new TransactionManager($entityManager);
19 10
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 3
    public function persist($entity, $flush = true)
25
    {
26 3
        return $this->transactionManager->transactional(
27
            function () use ($entity, $flush) {
28 3
                $this->getEntityManager()->persist($entity);
29
30 3
                if ($flush) {
31 3
                    $this->getEntityManager()->flush($entity);
32
                }
33
34 3
                return $entity;
35 3
            }
36
        );
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 2
    public function merge($entity, $flush = false)
43
    {
44 2
        return $this->transactionManager->transactional(
45
            function () use ($entity, $flush) {
46 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...
47
48 2
                if ($flush) {
49
                    $this->getEntityManager()->flush($entity);
50
                }
51
52 2
                return $entity;
53 2
            }
54
        );
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function flush($entity = null)
61
    {
62 2
        $this->getEntityManager()->flush($entity);
63 2
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function detach($entity)
69
    {
70
        $this->getEntityManager()->detach($entity);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 3
    public function remove($entity, $flush = false)
77
    {
78 3
        return $this->transactionManager->transactional(
79
            function () use ($entity, $flush) {
80 3
                $this->getEntityManager()->remove($entity);
81
82 3
                if ($flush) {
83
                    $this->getEntityManager()->flush($entity);
84
                }
85 3
            }
86
        );
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1
    public function removeById($id, $flush = false)
93
    {
94 1
        return $this->transactionManager->transactional(
95
            function () use ($id, $flush) {
96
                /** @var EntityInterface $entity */
97 1
                $entity = $this->find($id);
98 1
                $this->remove($entity, $flush);
99 1
            }
100
        );
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 2
    public function removeAll($flush = false, $iterate = true)
107
    {
108 2
        return $this->transactionManager->transactional(
109
            function () use ($flush, $iterate) {
110 2
                if ($iterate) {
111 1
                    $this->removeAllByIterating();
112
                } else {
113 1
                    $this->removeAllByQuery();
114
                }
115
116 2
                if ($flush) {
117 2
                    $this->getEntityManager()->flush();
118
                }
119 2
            }
120
        );
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getTransactionManager()
127
    {
128
        return $this->transactionManager;
129
    }
130
131 1
    protected function removeAllByIterating($batchSize = 100)
132
    {
133 1
        return $this->transactionManager->transactional(
134
            function () use ($batchSize) {
135 1
                $entities = $this->findAll();
136 1
                $count = 0;
137 1
                foreach ($entities as $entity) {
138 1
                    $this->remove($entity, false);
139 1
                    $count++;
140 1
                    if ($count >= $batchSize) {
141
                        $this->getEntityManager()->flush();
142 1
                        $count = 0;
143
                    }
144
                }
145 1
            }
146
        );
147
    }
148
149 1
    protected function removeAllByQuery()
150
    {
151 1
        return $this->transactionManager->transactional(
152
            function () {
153 1
                $queryBuilder = $this->createQueryBuilder('entity');
154 1
                $queryBuilder->delete();
155 1
                $query = $queryBuilder->getQuery();
156 1
                $query->execute();
157 1
            }
158
        );
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164 1
    public function findPaginatedBy($page = 1, $perPage = 10, array $criteria = [], array $orderBy = null)
165
    {
166 1
        return $this->transactionManager->transactional(
167
            function () use ($page, $perPage, $criteria, $orderBy) {
168
169 1
                $queryBuilder = $this->createQueryBuilder('entity');
170 1
                $expr = $queryBuilder->expr();
171
172 1
                foreach ($criteria as $field => $value) {
173 1
                    $queryBuilder->andWhere($expr->eq('entity.' . $field, $value));
174
                }
175
176 1
                if (null !== $orderBy) {
177 1
                    foreach ($orderBy as $field => $order) {
178 1
                        $queryBuilder->addOrderBy('entity.' . $field, $order);
179
                    }
180
                }
181
182 1
                $queryBuilder->setFirstResult(($page - 1) * $perPage);
183 1
                $queryBuilder->setMaxResults($perPage);
184
185 1
                return new Paginator($queryBuilder);
186 1
            }
187
        );
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193 1
    public function countAll()
194
    {
195 1
        return $this->transactionManager->transactional(
196 1
            function () {
197 1
                $queryBuilder = $this->getEntityManager()->createQueryBuilder();
198
                $queryBuilder
199 1
                    ->select('count(entity)')
200 1
                    ->from($this->getClassName(), 'entity');
201
202 1
                $result = $queryBuilder->getQuery()->getSingleScalarResult();
203
204 1
                return $result;
205 1
            }
206
        );
207
    }
208
209
    /**
210
     * @return QueryBuilder
211
     */
212
    protected function createBlankQueryBuilder()
213
    {
214
        return $this->getEntityManager()->createQueryBuilder();
215
    }
216
}
217