Test Failed
Pull Request — master (#8)
by Alex
02:54
created

AbstractEntityRepository::resolveQuery()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 19
rs 9.9
c 0
b 0
f 0
cc 3
nc 4
nop 1
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\HydrateMode;
10
use Arp\DoctrineEntityRepository\Constant\QueryServiceOption;
11
use Arp\DoctrineEntityRepository\Constant\TransactionMode;
12
use Arp\DoctrineEntityRepository\Exception\EntityNotFoundException;
13
use Arp\DoctrineEntityRepository\Exception\EntityRepositoryException;
14
use Arp\DoctrineEntityRepository\Persistence\PersistServiceInterface;
15
use Arp\DoctrineEntityRepository\Query\Exception\QueryServiceException;
16
use Arp\DoctrineEntityRepository\Query\QueryServiceInterface;
17
use Arp\Entity\EntityInterface;
18
use Doctrine\ORM\AbstractQuery;
19
use Doctrine\ORM\QueryBuilder;
20
use Psr\Log\LoggerInterface;
21
22
/**
23
 * @author  Alex Patterson <[email protected]>
24
 * @package Arp\DoctrineEntityRepository
25
 */
26
abstract class AbstractEntityRepository implements EntityRepositoryInterface
27
{
28
    /**
29
     * @var string
30
     */
31
    protected string $entityName;
32
33
    /**
34
     * @var QueryServiceInterface
35
     */
36
    protected QueryServiceInterface $queryService;
37
38
    /**
39
     * @var PersistServiceInterface
40
     */
41
    protected PersistServiceInterface $persistService;
42
43
    /**
44
     * @var LoggerInterface
45
     */
46
    protected LoggerInterface $logger;
47
48
    /**
49
     * @param string                  $entityName
50
     * @param QueryServiceInterface   $queryService
51
     * @param PersistServiceInterface $persistService
52
     * @param LoggerInterface         $logger
53
     */
54
    public function __construct(
55
        string $entityName,
56
        QueryServiceInterface $queryService,
57
        PersistServiceInterface $persistService,
58
        LoggerInterface $logger
59
    ) {
60
        $this->entityName = $entityName;
61
        $this->queryService = $queryService;
62
        $this->persistService = $persistService;
63
        $this->logger = $logger;
64
    }
65
66
    /**
67
     * Return the fully qualified class name of the mapped entity instance.
68
     *
69
     * @return string
70
     */
71
    public function getClassName(): string
72
    {
73
        return $this->entityName;
74
    }
75
76
    /**
77
     * Return a single entity instance matching the provided $id.
78
     *
79
     * @param string $id
80
     *
81
     * @return EntityInterface|null
82
     *
83
     * @throws EntityRepositoryException
84
     *
85
     * @noinspection PhpMissingParamTypeInspection
86
     */
87
    public function find($id): ?EntityInterface
88
    {
89
        try {
90
            return $this->queryService->findOneById($id);
91
        } catch (\Throwable $e) {
92
            $errorMessage = sprintf('Unable to find entity of type \'%s\': %s', $this->entityName, $e->getMessage());
93
94
            $this->logger->error($errorMessage, ['exception' => $e, 'id' => $id]);
95
96
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
97
        }
98
    }
99
100
    /**
101
     * Return a single entity instance matching the provided $criteria.
102
     *
103
     * @param array $criteria The entity filter criteria.
104
     *
105
     * @return EntityInterface|null
106
     *
107
     * @throws EntityRepositoryException
108
     */
109
    public function findOneBy(array $criteria): ?EntityInterface
110
    {
111
        try {
112
            return $this->queryService->findOne($criteria);
113
        } catch (\Throwable $e) {
114
            $errorMessage = sprintf('Unable to find entity of type \'%s\': %s', $this->entityName, $e->getMessage());
115
116
            $this->logger->error($errorMessage, ['exception' => $e, 'criteria' => $criteria]);
117
118
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
119
        }
120
    }
121
122
    /**
123
     * Return all of the entities within the collection.
124
     *
125
     * @return EntityInterface[]|iterable
126
     *
127
     * @throws EntityRepositoryException
128
     */
129
    public function findAll(): iterable
130
    {
131
        return $this->findBy([]);
132
    }
133
134
    /**
135
     * Return a collection of entities that match the provided $criteria.
136
     *
137
     * @param array      $criteria
138
     * @param array|null $orderBy
139
     * @param int|null   $limit
140
     * @param int|null   $offset
141
     *
142
     * @return EntityInterface[]|iterable
143
     *
144
     * @throws EntityRepositoryException
145
     */
146
    public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): iterable
147
    {
148
        try {
149
            $options = [];
150
151
            if (null !== $orderBy) {
152
                $options[QueryServiceOption::ORDER_BY] = $orderBy;
153
            }
154
155
            if (null !== $limit) {
156
                $options[QueryServiceOption::LIMIT] = $limit;
157
            }
158
159
            if (null !== $offset) {
160
                $options[QueryServiceOption::OFFSET] = $offset;
161
            }
162
163
            return $this->queryService->findMany($criteria, $options);
164
        } catch (\Throwable $e) {
165
            $errorMessage = sprintf(
166
                'Unable to return a collection of type \'%s\': %s',
167
                $this->entityName,
168
                $e->getMessage()
169
            );
170
171
            $this->logger->error($errorMessage, ['exception' => $e, 'criteria' => $criteria, 'options' => $options]);
172
173
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
174
        }
175
    }
176
177
    /**
178
     * Save a single entity instance.
179
     *
180
     * @param EntityInterface $entity
181
     * @param array           $options
182
     *
183
     * @return EntityInterface
184
     *
185
     * @throws EntityRepositoryException
186
     */
187
    public function save(EntityInterface $entity, array $options = []): EntityInterface
188
    {
189
        try {
190
            return $this->persistService->save($entity, $options);
191
        } catch (\Throwable $e) {
192
            $errorMessage = sprintf('Unable to save entity of type \'%s\': %s', $this->entityName, $e->getMessage());
193
194
            $this->logger->error($errorMessage);
195
196
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
197
        }
198
    }
199
200
    /**
201
     * Save a collection of entities in a single transaction.
202
     *
203
     * @param iterable|EntityInterface[] $collection The collection of entities that should be saved.
204
     * @param array                      $options    the optional save options.
205
     *
206
     * @return iterable
207
     *
208
     * @throws EntityRepositoryException If the save cannot be completed
209
     */
210
    public function saveCollection(iterable $collection, array $options = []): iterable
211
    {
212
        $flushMode = $options[EntityEventOption::FLUSH_MODE] ?? FlushMode::ENABLED;
213
        $transactionMode = $options[EntityEventOption::TRANSACTION_MODE] ?? TransactionMode::ENABLED;
214
215
        try {
216
            if (TransactionMode::ENABLED === $transactionMode) {
217
                $this->logger->info(
218
                    sprintf('Starting collection transaction for entity type \'%s\'', $this->entityName)
219
                );
220
                $this->persistService->beginTransaction();
221
            }
222
223
            $entities = [];
224
            $saveOptions = [
225
                EntityEventOption::FLUSH_MODE       => FlushMode::DISABLED,
226
                EntityEventOption::TRANSACTION_MODE => TransactionMode::DISABLED,
227
            ];
228
229
            foreach ($collection as $entity) {
230
                $entities[] = $this->save($entity, $saveOptions);
231
            }
232
233
            $this->logger->info(
234
                sprintf(
235
                    'Completed collection save of \'%d\' entities of type \'%s\'',
236
                    count($entities),
237
                    $this->entityName
238
                )
239
            );
240
241
            if (FlushMode::ENABLED === $flushMode) {
242
                $this->logger->info(
243
                    sprintf('Performing collection flush operation for entity type \'%s\'', $this->entityName)
244
                );
245
                $this->persistService->flush();
246
            }
247
248
            if (TransactionMode::ENABLED === $transactionMode) {
249
                $this->logger->info(
250
                    sprintf('Committing collection transaction for entity type \'%s\'', $this->entityName)
251
                );
252
                $this->persistService->commitTransaction();
253
            }
254
255
            return $entities;
256
        } catch (\Throwable $e) {
257
            if (TransactionMode::ENABLED === $transactionMode) {
258
                $this->logger->info(
259
                    sprintf('Rolling back collection transaction for entity type \'%s\'', $this->entityName)
260
                );
261
                $this->persistService->rollbackTransaction();
262
            }
263
264
            $errorMessage = sprintf(
265
                'Unable to save collection of type \'%s\' : %s',
266
                $this->entityName,
267
                $e->getMessage()
268
            );
269
270
            $this->logger->error($errorMessage);
271
272
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
273
        }
274
    }
275
276
    /**
277
     * Delete an entity.
278
     *
279
     * @param EntityInterface|string $entity
280
     * @param array                  $options
281
     *
282
     * @return bool
283
     *
284
     * @throws EntityRepositoryException
285
     */
286
    public function delete($entity, array $options = []): bool
287
    {
288
        if (!is_string($entity) && !$entity instanceof EntityInterface) {
289
            $errorMessage = sprintf(
290
                'The \'entity\' argument must be a \'string\' or an object of type \'%s\'; '
291
                . '\'%s\' provided in \'%s::%s\'',
292
                EntityInterface::class,
293
                (is_object($entity) ? get_class($entity) : gettype($entity)),
294
                static::class,
295
                __FUNCTION__
296
            );
297
298
            $this->logger->error($errorMessage);
299
300
            throw new EntityRepositoryException($errorMessage);
301
        }
302
303
        if (is_string($entity)) {
304
            $id = $entity;
305
            $entity = $this->find($id);
306
307
            if (null === $entity) {
308
                $errorMessage = sprintf(
309
                    'Unable to delete entity \'%s::%s\': The entity could not be found',
310
                    $this->entityName,
311
                    $id
312
                );
313
314
                $this->logger->error($errorMessage);
315
316
                throw new EntityNotFoundException($errorMessage);
317
            }
318
        }
319
320
        try {
321
            return $this->persistService->delete($entity, $options);
322
        } catch (\Throwable $e) {
323
            $errorMessage = sprintf(
324
                'Unable to delete entity of type \'%s\': %s',
325
                $this->entityName,
326
                $e->getMessage()
327
            );
328
329
            $this->logger->error($errorMessage, ['exception' => $e]);
330
331
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
332
        }
333
    }
334
335
    /**
336
     * Perform a deletion of a collection of entities.
337
     *
338
     * @param iterable|EntityInterface $collection
339
     * @param array                    $options
340
     *
341
     * @return int
342
     *
343
     * @throws EntityRepositoryException
344
     */
345
    public function deleteCollection(iterable $collection, array $options = []): int
346
    {
347
        $flushMode = $options[EntityEventOption::FLUSH_MODE] ?? FlushMode::ENABLED;
348
        $transactionMode = $options[EntityEventOption::TRANSACTION_MODE] ?? TransactionMode::ENABLED;
349
350
        try {
351
            if (TransactionMode::ENABLED === $transactionMode) {
352
                $this->persistService->beginTransaction();
353
            }
354
355
            $deleteOptions = [
356
                EntityEventOption::FLUSH_MODE       => FlushMode::DISABLED,
357
                EntityEventOption::TRANSACTION_MODE => TransactionMode::DISABLED,
358
            ];
359
360
            $deleted = 0;
361
            foreach ($collection as $entity) {
362
                if (true === $this->delete($entity, $deleteOptions)) {
363
                    $deleted++;
364
                }
365
            }
366
367
            if (FlushMode::ENABLED === $flushMode) {
368
                $this->persistService->flush();
369
            }
370
371
            if (TransactionMode::ENABLED === $transactionMode) {
372
                $this->persistService->commitTransaction();
373
            }
374
375
            return $deleted;
376
        } catch (\Throwable $e) {
377
            if (TransactionMode::ENABLED === $transactionMode) {
378
                $this->persistService->rollbackTransaction();
379
            }
380
381
            $errorMessage = sprintf(
382
                'Unable to delete collection of type \'%s\' : %s',
383
                $this->entityName,
384
                $e->getMessage()
385
            );
386
387
            $this->logger->error($errorMessage, ['exception' => $e]);
388
389
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
390
        }
391
    }
392
393
    /**
394
     * @throws EntityRepositoryException
395
     */
396
    public function clear(): void
397
    {
398
        try {
399
            $this->persistService->clear();
400
        } catch (\Throwable $e) {
401
            $errorMessage = sprintf(
402
                'Unable to clear entity of type \'%s\': %s',
403
                $this->entityName,
404
                $e->getMessage()
405
            );
406
407
            $this->logger->error($errorMessage, ['exception' => $e]);
408
409
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
410
        }
411
    }
412
413
    /**
414
     * @param EntityInterface $entity
415
     *
416
     * @throws EntityRepositoryException
417
     */
418
    public function refresh(EntityInterface $entity): void
419
    {
420
        try {
421
            $this->persistService->refresh($entity);
422
        } catch (\Throwable $e) {
423
            $errorMessage = sprintf(
424
                'Unable to refresh entity of type \'%s\': %s',
425
                $this->entityName,
426
                $e->getMessage()
427
            );
428
429
            $this->logger->error($errorMessage, ['exception' => $e, 'id' => $entity->getId()]);
430
431
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
432
        }
433
    }
434
435
    /**
436
     * Execute query builder or query instance and return the results.
437
     *
438
     * @param object $query
439
     * @param array  $options
440
     *
441
     * @return EntityInterface[]|iterable
442
     *
443
     * @throws EntityRepositoryException
444
     */
445
    protected function executeQuery(object $query, array $options = [])
446
    {
447
        $query = $this->resolveQuery($query);
448
449
        try {
450
            return $this->queryService->execute($query, $options);
451
        } catch (QueryServiceException $e) {
452
            $errorMessage = sprintf(
453
                'Failed to perform query for entity type \'%s\': %s',
454
                $this->entityName,
455
                $e->getMessage()
456
            );
457
458
            $this->logger->error($errorMessage, ['exception' => $e, 'sql' => $query->getSQL()]);
459
460
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
461
        }
462
    }
463
464
    /**
465
     * Return a single entity instance. NULL will be returned if the result set contains 0 or more than 1 result.
466
     *
467
     * Optionally control the object hydration with QueryServiceOption::HYDRATE_MODE.
468
     *
469
     * @param object $query
470
     * @param array  $options
471
     *
472
     * @return EntityInterface|array|null
473
     *
474
     * @throws EntityRepositoryException
475
     */
476
    protected function getSingleResultOrNull(object $query, array $options = [])
477
    {
478
        $query = $this->resolveQuery($query);
479
480
        try {
481
            return $this->queryService->getSingleResultOrNull($query, $options);
482
        } catch (QueryServiceException $e) {
483
            $errorMessage = sprintf(
484
                'Failed to perform query for entity type \'%s\': %s',
485
                $this->entityName,
486
                $e->getMessage()
487
            );
488
489
            $this->logger->error($errorMessage, ['exception' => $e, 'sql' => $query->getSQL()]);
490
491
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
492
        }
493
    }
494
495
    /**
496
     * Return a result set containing a single array result. NULL will be returned if the result set
497
     * contains 0 or more than 1 result.
498
     *
499
     * @param object $query
500
     * @param array  $options
501
     *
502
     * @return array|EntityInterface|null
503
     *
504
     * @throws EntityRepositoryException
505
     */
506
    protected function getSingleArrayResultOrNull(object $query, array $options = [])
507
    {
508
        $options = array_replace_recursive(
509
            $options,
510
            [
511
                QueryServiceOption::HYDRATION_MODE => HydrateMode::ARRAY,
512
            ]
513
        );
514
515
        return $this->getSingleResultOrNull($query, $options);
516
    }
517
518
    /**
519
     * Resolve the Doctrine query object from a possible QueryBuilder instance.
520
     *
521
     * @param object $query
522
     *
523
     * @return AbstractQuery
524
     *
525
     * @throws EntityRepositoryException
526
     */
527
    private function resolveQuery(object $query): AbstractQuery
528
    {
529
        if ($query instanceof QueryBuilder) {
530
            $query = $query->getQuery();
531
        }
532
533
        if (!$query instanceof AbstractQuery) {
534
            throw new EntityRepositoryException(
535
                sprintf(
536
                    'The \'query\' argument must be an object of type \'%s\' or \'%s\'; \'%s\' provided in \'%s\'',
537
                    QueryBuilder::class,
538
                    AbstractQuery::class,
539
                    get_class($query),
540
                    __METHOD__
541
                )
542
            );
543
        }
544
545
        return $query;
546
    }
547
}
548