Passed
Pull Request — master (#8)
by Alex
05:00
created

getSingleArrayResultOrNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\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
    public function find($id): ?EntityInterface
86
    {
87
        try {
88
            return $this->queryService->findOneById($id);
89
        } catch (\Throwable $e) {
90
            $errorMessage = sprintf('Unable to find entity of type \'%s\': %s', $this->entityName, $e->getMessage());
91
92
            $this->logger->error($errorMessage, ['exception' => $e, 'id' => $id]);
93
94
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
95
        }
96
    }
97
98
    /**
99
     * Return a single entity instance matching the provided $criteria.
100
     *
101
     * @param array $criteria The entity filter criteria.
102
     *
103
     * @return EntityInterface|null
104
     *
105
     * @throws EntityRepositoryException
106
     */
107
    public function findOneBy(array $criteria): ?EntityInterface
108
    {
109
        try {
110
            return $this->queryService->findOne($criteria);
111
        } catch (\Throwable $e) {
112
            $errorMessage = sprintf('Unable to find entity of type \'%s\': %s', $this->entityName, $e->getMessage());
113
114
            $this->logger->error($errorMessage, ['exception' => $e, 'criteria' => $criteria]);
115
116
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
117
        }
118
    }
119
120
    /**
121
     * Return all of the entities within the collection.
122
     *
123
     * @return EntityInterface[]|iterable
124
     *
125
     * @throws EntityRepositoryException
126
     */
127
    public function findAll(): iterable
128
    {
129
        return $this->findBy([]);
130
    }
131
132
    /**
133
     * Return a collection of entities that match the provided $criteria.
134
     *
135
     * @param array      $criteria
136
     * @param array|null $orderBy
137
     * @param int|null   $limit
138
     * @param int|null   $offset
139
     *
140
     * @return EntityInterface[]|iterable
141
     *
142
     * @throws EntityRepositoryException
143
     */
144
    public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): iterable
145
    {
146
        try {
147
            $options = [];
148
149
            if (null !== $orderBy) {
150
                $options[QueryServiceOption::ORDER_BY] = $orderBy;
151
            }
152
153
            if (null !== $limit) {
154
                $options[QueryServiceOption::LIMIT] = $limit;
155
            }
156
157
            if (null !== $offset) {
158
                $options[QueryServiceOption::OFFSET] = $offset;
159
            }
160
161
            return $this->queryService->findMany($criteria, $options);
162
        } catch (\Throwable $e) {
163
            $errorMessage = sprintf(
164
                'Unable to return a collection of type \'%s\': %s',
165
                $this->entityName,
166
                $e->getMessage()
167
            );
168
169
            $this->logger->error($errorMessage, ['exception' => $e, 'criteria' => $criteria, 'options' => $options]);
170
171
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
172
        }
173
    }
174
175
    /**
176
     * Save a single entity instance.
177
     *
178
     * @param EntityInterface $entity
179
     * @param array           $options
180
     *
181
     * @return EntityInterface
182
     *
183
     * @throws EntityRepositoryException
184
     */
185
    public function save(EntityInterface $entity, array $options = []): EntityInterface
186
    {
187
        try {
188
            return $this->persistService->save($entity, $options);
189
        } catch (\Throwable $e) {
190
            $errorMessage = sprintf('Unable to save entity of type \'%s\': %s', $this->entityName, $e->getMessage());
191
192
            $this->logger->error($errorMessage);
193
194
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
195
        }
196
    }
197
198
    /**
199
     * Save a collection of entities in a single transaction.
200
     *
201
     * @param iterable|EntityInterface[] $collection The collection of entities that should be saved.
202
     * @param array                      $options    the optional save options.
203
     *
204
     * @return iterable
205
     *
206
     * @throws EntityRepositoryException If the save cannot be completed
207
     */
208
    public function saveCollection(iterable $collection, array $options = []): iterable
209
    {
210
        $flushMode = $options[EntityEventOption::FLUSH_MODE] ?? FlushMode::ENABLED;
211
        $transactionMode = $options[EntityEventOption::TRANSACTION_MODE] ?? TransactionMode::ENABLED;
212
213
        try {
214
            if (TransactionMode::ENABLED === $transactionMode) {
215
                $this->persistService->beginTransaction();
216
            }
217
218
            $entities = [];
219
            $saveOptions = [
220
                EntityEventOption::FLUSH_MODE       => FlushMode::DISABLED,
221
                EntityEventOption::TRANSACTION_MODE => TransactionMode::DISABLED,
222
            ];
223
224
            foreach ($collection as $entity) {
225
                $entities[] = $this->save($entity, $saveOptions);
226
            }
227
228
            if (FlushMode::ENABLED === $flushMode) {
229
                $this->persistService->flush();
230
            }
231
            if (TransactionMode::ENABLED === $transactionMode) {
232
                $this->persistService->commitTransaction();
233
            }
234
235
            return $entities;
236
        } catch (\Throwable $e) {
237
            if (TransactionMode::ENABLED === $transactionMode) {
238
                $this->persistService->rollbackTransaction();
239
            }
240
241
            $errorMessage = sprintf(
242
                'Unable to save collection of type \'%s\' : %s',
243
                $this->entityName,
244
                $e->getMessage()
245
            );
246
247
            $this->logger->error($errorMessage);
248
249
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
250
        }
251
    }
252
253
    /**
254
     * Delete an entity.
255
     *
256
     * @param EntityInterface|string $entity
257
     * @param array                  $options
258
     *
259
     * @return bool
260
     *
261
     * @throws EntityRepositoryException
262
     */
263
    public function delete($entity, array $options = []): bool
264
    {
265
        if (!is_string($entity) && !$entity instanceof EntityInterface) {
266
            $errorMessage = sprintf(
267
                'The \'entity\' argument must be a \'string\' or an object of type \'%s\'; '
268
                . '\'%s\' provided in \'%s::%s\'',
269
                EntityInterface::class,
270
                (is_object($entity) ? get_class($entity) : gettype($entity)),
271
                static::class,
272
                __FUNCTION__
273
            );
274
275
            $this->logger->error($errorMessage);
276
277
            throw new EntityRepositoryException($errorMessage);
278
        }
279
280
        if (is_string($entity)) {
281
            $id = $entity;
282
            $entity = $this->find($id);
283
284
            if (null === $entity) {
285
                $errorMessage = sprintf(
286
                    'Unable to delete entity \'%s::%s\': The entity could not be found',
287
                    $this->entityName,
288
                    $id
289
                );
290
291
                $this->logger->error($errorMessage);
292
293
                throw new EntityNotFoundException($errorMessage);
294
            }
295
        }
296
297
        try {
298
            return $this->persistService->delete($entity, $options);
299
        } catch (\Throwable $e) {
300
            $errorMessage = sprintf(
301
                'Unable to delete entity of type \'%s\': %s',
302
                $this->entityName,
303
                $e->getMessage()
304
            );
305
306
            $this->logger->error($errorMessage, ['exception' => $e]);
307
308
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
309
        }
310
    }
311
312
    /**
313
     * Perform a deletion of a collection of entities.
314
     *
315
     * @param iterable|EntityInterface $collection
316
     * @param array                    $options
317
     *
318
     * @return int
319
     *
320
     * @throws EntityRepositoryException
321
     */
322
    public function deleteCollection(iterable $collection, array $options = []): int
323
    {
324
        $flushMode = $options[EntityEventOption::FLUSH_MODE] ?? FlushMode::ENABLED;
325
        $transactionMode = $options[EntityEventOption::TRANSACTION_MODE] ?? TransactionMode::ENABLED;
326
327
        try {
328
            if (TransactionMode::ENABLED === $transactionMode) {
329
                $this->persistService->beginTransaction();
330
            }
331
332
            $deleteOptions = [
333
                EntityEventOption::FLUSH_MODE       => FlushMode::DISABLED,
334
                EntityEventOption::TRANSACTION_MODE => TransactionMode::DISABLED,
335
            ];
336
337
            $deleted = 0;
338
            foreach ($collection as $entity) {
339
                if (true === $this->delete($entity, $deleteOptions)) {
340
                    $deleted++;
341
                }
342
            }
343
344
            if (FlushMode::ENABLED === $flushMode) {
345
                $this->persistService->flush();
346
            }
347
348
            if (TransactionMode::ENABLED === $transactionMode) {
349
                $this->persistService->commitTransaction();
350
            }
351
352
            return $deleted;
353
        } catch (\Throwable $e) {
354
            if (TransactionMode::ENABLED === $transactionMode) {
355
                $this->persistService->rollbackTransaction();
356
            }
357
358
            $errorMessage = sprintf(
359
                'Unable to delete collection of type \'%s\' : %s',
360
                $this->entityName,
361
                $e->getMessage()
362
            );
363
364
            $this->logger->error($errorMessage, ['exception' => $e]);
365
366
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
367
        }
368
    }
369
370
    /**
371
     * @throws EntityRepositoryException
372
     */
373
    public function clear(): void
374
    {
375
        try {
376
            $this->persistService->clear();
377
        } catch (\Throwable $e) {
378
            $errorMessage = sprintf(
379
                'Unable to clear entity of type \'%s\': %s',
380
                $this->entityName,
381
                $e->getMessage()
382
            );
383
384
            $this->logger->error($errorMessage, ['exception' => $e]);
385
386
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
387
        }
388
    }
389
390
    /**
391
     * @param EntityInterface $entity
392
     *
393
     * @throws EntityRepositoryException
394
     */
395
    public function refresh(EntityInterface $entity): void
396
    {
397
        try {
398
            $this->persistService->refresh($entity);
399
        } catch (\Throwable $e) {
400
            $errorMessage = sprintf(
401
                'Unable to refresh entity of type \'%s\': %s',
402
                $this->entityName,
403
                $e->getMessage()
404
            );
405
406
            $this->logger->error($errorMessage, ['exception' => $e, 'id' => $entity->getId()]);
407
408
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
409
        }
410
    }
411
412
    /**
413
     * Execute query builder or query instance and return the results.
414
     *
415
     * @param object $query
416
     * @param array  $options
417
     *
418
     * @return EntityInterface[]|iterable
419
     *
420
     * @throws EntityRepositoryException
421
     */
422
    protected function executeQuery(object $query, array $options = [])
423
    {
424
        $query = $this->resolveQuery($query);
425
426
        try {
427
            return $this->queryService->execute($query, $options);
428
        } catch (QueryServiceException $e) {
429
            $errorMessage = sprintf(
430
                'Failed to perform query for entity type \'%s\': %s',
431
                $this->entityName,
432
                $e->getMessage()
433
            );
434
435
            $this->logger->error($errorMessage, ['exception' => $e, 'sql' => $query->getSQL()]);
436
437
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
438
        }
439
    }
440
441
    /**
442
     * Return a single entity instance. NULL will be returned if the result set contains 0 or more than 1 result.
443
     *
444
     * Optionally control the object hydration with QueryServiceOption::HYDRATE_MODE.
445
     *
446
     * @param object $query
447
     * @param array  $options
448
     *
449
     * @return EntityInterface|array|null
450
     *
451
     * @throws EntityRepositoryException
452
     */
453
    protected function getSingleResultOrNull(object $query, array $options = [])
454
    {
455
        $query = $this->resolveQuery($query);
456
457
        try {
458
            return $this->queryService->getSingleResultOrNull($query, $options);
459
        } catch (QueryServiceException $e) {
460
            $errorMessage = sprintf(
461
                'Failed to perform query for entity type \'%s\': %s',
462
                $this->entityName,
463
                $e->getMessage()
464
            );
465
466
            $this->logger->error($errorMessage, ['exception' => $e, 'sql' => $query->getSQL()]);
467
468
            throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
469
        }
470
    }
471
472
    /**
473
     * Return a result set containing a single array result. NULL will be returned if the result set
474
     * contains 0 or more than 1 result.
475
     *
476
     * @param object $query
477
     * @param array  $options
478
     *
479
     * @return array|null
480
     *
481
     * @throws EntityRepositoryException
482
     */
483
    protected function getSingleArrayResultOrNull(object $query, array $options = []): ?array
484
    {
485
        $options = array_replace_recursive(
486
            $options,
487
            [
488
                QueryServiceOption::HYDRATION_MODE => HydrateMode::ARRAY,
489
            ]
490
        );
491
492
        return $this->getSingleResultOrNull($query, $options);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getSingleR...rNull($query, $options) could return the type Arp\Entity\EntityInterface which is incompatible with the type-hinted return array|null. Consider adding an additional type-check to rule them out.
Loading history...
493
    }
494
495
    /**
496
     * Resolve the Doctrine query object from a possible QueryBuilder instance.
497
     *
498
     * @param object $query
499
     *
500
     * @return AbstractQuery
501
     *
502
     * @throws EntityRepositoryException
503
     */
504
    private function resolveQuery(object $query): AbstractQuery
505
    {
506
        if ($query instanceof QueryBuilder) {
507
            $query = $query->getQuery();
508
        }
509
510
        if (!$query instanceof AbstractQuery) {
511
            throw new EntityRepositoryException(
512
                sprintf(
513
                    'The \'query\' argument must be an object of type \'%s\' or \'%s\'; \'%s\' provided in \'%s\'',
514
                    QueryBuilder::class,
515
                    AbstractQuery::class,
516
                    get_class($query),
517
                    __METHOD__
518
                )
519
            );
520
        }
521
522
        return $query;
523
    }
524
}
525