Completed
Push — master ( b539f5...2b6674 )
by Philip
25:54 queued 57s
created

OrmEntityRepository::commitTransaction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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