Completed
Push — master ( 022d9c...3eb66a )
by Philip
05:41
created

OrmEntityRepository::remove()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

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