Passed
Pull Request — master (#5)
by Alex
07:47
created

AbstractEntityRepository::deleteCollection()   B

Complexity

Conditions 8
Paths 50

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 45
rs 8.4444
c 0
b 0
f 0
cc 8
nc 50
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\DoctrineEntityRepository;
6
7
use Arp\DoctrineEntityRepository\Constant\EntityEventOption;
8
use Arp\DoctrineEntityRepository\Constant\FlushMode;
9
use Arp\DoctrineEntityRepository\Constant\QueryServiceOption;
10
use Arp\DoctrineEntityRepository\Constant\TransactionMode;
11
use Arp\DoctrineEntityRepository\Exception\EntityNotFoundException;
12
use Arp\DoctrineEntityRepository\Exception\EntityRepositoryException;
13
use Arp\DoctrineEntityRepository\Persistence\PersistServiceInterface;
14
use Arp\DoctrineEntityRepository\Query\QueryServiceInterface;
15
use Arp\Entity\EntityInterface;
16
use Psr\Log\LoggerInterface;
17
18
/**
19
 * @author  Alex Patterson <[email protected]>
20
 * @package Arp\DoctrineEntityRepository
21
 */
22
abstract class AbstractEntityRepository implements EntityRepositoryInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    protected string $entityName;
28
29
    /**
30
     * @var QueryServiceInterface
31
     */
32
    protected QueryServiceInterface $queryService;
33
34
    /**
35
     * @var PersistServiceInterface
36
     */
37
    protected PersistServiceInterface $persistService;
38
39
    /**
40
     * @var LoggerInterface
41
     */
42
    protected LoggerInterface $logger;
43
44
    /**
45
     * @param string                  $entityName
46
     * @param QueryServiceInterface   $queryService
47
     * @param PersistServiceInterface $persistService
48
     * @param LoggerInterface         $logger
49
     */
50
    public function __construct(
51
        string $entityName,
52
        QueryServiceInterface $queryService,
53
        PersistServiceInterface $persistService,
54
        LoggerInterface $logger
55
    ) {
56
        $this->entityName = $entityName;
57
        $this->queryService = $queryService;
58
        $this->persistService = $persistService;
59
        $this->logger = $logger;
60
    }
61
62
    /**
63
     * Return the fully qualified class name of the mapped entity instance.
64
     *
65
     * @return string
66
     */
67
    public function getClassName(): string
68
    {
69
        return $this->entityName;
70
    }
71
72
    /**
73
     * Return a single entity instance matching the provided $id.
74
     *
75
     * @param string $id
76
     *
77
     * @return EntityInterface|null
78
     *
79
     * @throws EntityRepositoryException
80
     */
81
    public function find($id): ?EntityInterface
82
    {
83
        try {
84
            return $this->queryService->findOneById($id);
85
        } catch (\Throwable $e) {
86
            $errorMessage = sprintf('Unable to find entity of type \'%s\': %s', $this->entityName, $e->getMessage());
87
88
            $this->logger->error($errorMessage);
89
90
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
91
        }
92
    }
93
94
    /**
95
     * Return a single entity instance matching the provided $criteria.
96
     *
97
     * @param array $criteria The entity filter criteria.
98
     *
99
     * @return EntityInterface|null
100
     *
101
     * @throws EntityRepositoryException
102
     */
103
    public function findOneBy(array $criteria): ?EntityInterface
104
    {
105
        try {
106
            return $this->queryService->findOne($criteria);
107
        } catch (\Throwable $e) {
108
            $errorMessage = sprintf('Unable to find entity of type \'%s\': %s', $this->entityName, $e->getMessage());
109
110
            $this->logger->error($errorMessage);
111
112
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
113
        }
114
    }
115
116
    /**
117
     * Return all of the entities within the collection.
118
     *
119
     * @return EntityInterface[]
120
     *
121
     * @throws EntityRepositoryException
122
     */
123
    public function findAll(): array
124
    {
125
        return $this->findBy([]);
126
    }
127
128
    /**
129
     * Return a collection of entities that match the provided $criteria.
130
     *
131
     * @param array      $criteria
132
     * @param array|null $orderBy
133
     * @param int|null   $limit
134
     * @param int|null   $offset
135
     *
136
     * @return EntityInterface[]
137
     *
138
     * @throws EntityRepositoryException
139
     */
140
    public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array
141
    {
142
        try {
143
            $options = [
144
                QueryServiceOption::LIMIT    => $limit,
145
                QueryServiceOption::OFFSET   => $offset,
146
                QueryServiceOption::ORDER_BY => $orderBy,
147
            ];
148
149
            return $this->queryService->findMany($criteria, $options);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->queryServi...ny($criteria, $options) returns the type Arp\Entity\EntityInterface[]&Traversable which is incompatible with the type-hinted return array.
Loading history...
150
        } catch (\Throwable $e) {
151
            $errorMessage = sprintf(
152
                'Unable to return a collection of type \'%s\': %s',
153
                $this->entityName,
154
                $e->getMessage()
155
            );
156
157
            $this->logger->error($errorMessage);
158
159
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
160
        }
161
    }
162
163
    /**
164
     * Save a single entity instance.
165
     *
166
     * @param EntityInterface $entity
167
     * @param array           $options
168
     *
169
     * @return EntityInterface
170
     *
171
     * @throws EntityRepositoryException
172
     */
173
    public function save(EntityInterface $entity, array $options = []): EntityInterface
174
    {
175
        try {
176
            return $this->persistService->save($entity, $options);
177
        } catch (\Throwable $e) {
178
            $errorMessage = sprintf('Unable to save entity of type \'%s\': %s', $this->entityName, $e->getMessage());
179
180
            $this->logger->error($errorMessage);
181
182
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
183
        }
184
    }
185
186
    /**
187
     * Save a collection of entities in a single transaction.
188
     *
189
     * @param iterable|EntityInterface[] $collection The collection of entities that should be saved.
190
     * @param array                      $options    the optional save options.
191
     *
192
     * @return iterable
193
     *
194
     * @throws EntityRepositoryException If the save cannot be completed
195
     */
196
    public function saveCollection(iterable $collection, array $options = []): iterable
197
    {
198
        try {
199
            $flushMode = $options[EntityEventOption::FLUSH_MODE] ?? FlushMode::ENABLED;
200
            $transactionMode = $options[EntityEventOption::TRANSACTION_MODE] ?? TransactionMode::ENABLED;
201
202
            if (TransactionMode::ENABLED === $transactionMode) {
203
                $this->persistService->beginTransaction();
204
            }
205
206
            $entities = [];
207
            $saveOptions = [
208
                EntityEventOption::FLUSH_MODE       => FlushMode::DISABLED,
209
                EntityEventOption::TRANSACTION_MODE => TransactionMode::DISABLED
210
            ];
211
212
            foreach ($collection as $entity) {
213
                $entities[] = $this->save($entity, $saveOptions);
214
            }
215
216
            if (FlushMode::ENABLED === $flushMode) {
217
                $this->persistService->flush();
218
            }
219
            if (TransactionMode::ENABLED === $transactionMode) {
220
                $this->persistService->commitTransaction();
221
            }
222
223
            return $entities;
224
        } catch (\Throwable $e) {
225
            if (TransactionMode::ENABLED === $transactionMode) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $transactionMode does not seem to be defined for all execution paths leading up to this point.
Loading history...
226
                $this->persistService->rollbackTransaction();
227
            }
228
229
            $errorMessage = sprintf(
230
                'Unable to save collection of type \'%s\' : %s',
231
                $this->entityName,
232
                $e->getMessage()
233
            );
234
235
            $this->logger->error($errorMessage);
236
237
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
238
        }
239
    }
240
241
    /**
242
     * Delete an entity.
243
     *
244
     * @param EntityInterface|int|string $entity
245
     * @param array                      $options
246
     *
247
     * @return bool
248
     *
249
     * @throws EntityRepositoryException
250
     */
251
    public function delete($entity, array $options = []): bool
252
    {
253
        if (!is_int($entity) && !is_string($entity) && !$entity instanceof EntityInterface) {
0 ignored issues
show
introduced by
$entity is always a sub-type of Arp\Entity\EntityInterface.
Loading history...
254
            throw new EntityRepositoryException(
255
                sprintf(
256
                    'The \'entity\' argument must be \'int\' or an object of type \'%s\'; \'%s\' provided in \'%s\'',
257
                    EntityInterface::class,
258
                    (is_object($entity) ? get_class($entity) : gettype($entity)),
259
                    __METHOD__
260
                )
261
            );
262
        }
263
264
        if (! is_object($entity)) {
265
            $id = (int) $entity;
266
            $entity = $this->find($id);
267
268
            if (null === $entity) {
269
                $errorMessage = sprintf(
270
                    'Unable to delete entity \'%s::%s\': The entity could not be found',
271
                    $this->entityName,
272
                    $id
273
                );
274
275
                $this->logger->error($errorMessage);
276
277
                throw new EntityNotFoundException($errorMessage);
278
            }
279
        }
280
281
        try {
282
            return $this->persistService->delete($entity, $options);
283
        } catch (\Throwable $e) {
284
            $errorMessage = sprintf('Unable to delete entity of type \'%s\': %s', $this->entityName, $e->getMessage());
285
286
            $this->logger->error($errorMessage);
287
288
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
289
        }
290
    }
291
292
    /**
293
     * Perform a deletion of a collection of entities.
294
     *
295
     * @param iterable|EntityInterface $collection
296
     * @param array                    $options
297
     *
298
     * @return int
299
     *
300
     * @throws EntityRepositoryException
301
     */
302
    public function deleteCollection(iterable $collection, array $options = []): int
303
    {
304
        try {
305
            $flushMode = $options[EntityEventOption::FLUSH_MODE] ?? FlushMode::ENABLED;
306
            $transactionMode = $options[EntityEventOption::TRANSACTION_MODE] ?? TransactionMode::ENABLED;
307
308
            if (TransactionMode::ENABLED === $transactionMode) {
309
                $this->persistService->beginTransaction();
310
            }
311
312
            $deleteOptions = [
313
                EntityEventOption::FLUSH_MODE       => FlushMode::DISABLED,
314
                EntityEventOption::TRANSACTION_MODE => TransactionMode::DISABLED,
315
            ];
316
317
            $deleted = 0;
318
            foreach ($collection as $entity) {
319
                if (true === $this->delete($entity, $deleteOptions)) {
320
                    $deleted++;
321
                }
322
            }
323
324
            if (FlushMode::ENABLED === $flushMode) {
325
                $this->persistService->flush();
326
            }
327
328
            if (TransactionMode::ENABLED === $transactionMode) {
329
                $this->persistService->commitTransaction();
330
            }
331
332
            return $deleted;
333
        } catch (\Throwable $e) {
334
            if (TransactionMode::ENABLED === $transactionMode) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $transactionMode does not seem to be defined for all execution paths leading up to this point.
Loading history...
335
                $this->persistService->rollbackTransaction();
336
            }
337
338
            $errorMessage = sprintf(
339
                'Unable to delete collection of type \'%s\' : %s',
340
                $this->entityName,
341
                $e->getMessage()
342
            );
343
344
            $this->logger->error($errorMessage);
345
346
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
347
        }
348
    }
349
350
    /**
351
     * @throws EntityRepositoryException
352
     */
353
    public function clear(): void
354
    {
355
        try {
356
            $this->persistService->clear();
357
        } catch (\Throwable $e) {
358
            $errorMessage = sprintf(
359
                'Unable to clear entity of type \'%s\': %s',
360
                $this->entityName,
361
                $e->getMessage()
362
            );
363
364
            $this->logger->error($errorMessage);
365
366
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
367
        }
368
    }
369
370
    /**
371
     * @param EntityInterface $entity
372
     *
373
     * @throws EntityRepositoryException
374
     */
375
    public function refresh(EntityInterface $entity): void
376
    {
377
        try {
378
            $this->persistService->refresh($entity);
379
        } catch (\Throwable $e) {
380
            $errorMessage = sprintf(
381
                'Unable to refresh entity of type \'%s\': %s',
382
                $this->entityName,
383
                $e->getMessage()
384
            );
385
386
            $this->logger->error($errorMessage);
387
388
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
389
        }
390
    }
391
}
392