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 $entityName;
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* @var QueryServiceInterface
|
31
|
|
|
*/
|
32
|
|
|
protected $queryService;
|
33
|
|
|
|
34
|
|
|
/**
|
35
|
|
|
* @var PersistServiceInterface
|
36
|
|
|
*/
|
37
|
|
|
protected $persistService;
|
38
|
|
|
|
39
|
|
|
/**
|
40
|
|
|
* @var LoggerInterface
|
41
|
|
|
*/
|
42
|
|
|
protected $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);
|
|
|
|
|
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
|
|
|
$saveOptions = array_replace_recursive(
|
200
|
|
|
[
|
201
|
|
|
EntityEventOption::FLUSH_MODE => FlushMode::DISABLED,
|
202
|
|
|
EntityEventOption::TRANSACTION_MODE => TransactionMode::DISABLED,
|
203
|
|
|
],
|
204
|
|
|
$options
|
205
|
|
|
);
|
206
|
|
|
|
207
|
|
|
$this->persistService->beginTransaction();
|
208
|
|
|
|
209
|
|
|
$entities = [];
|
210
|
|
|
foreach ($collection as $entity) {
|
211
|
|
|
$entities[] = $this->save($entity, $saveOptions);
|
212
|
|
|
}
|
213
|
|
|
|
214
|
|
|
$this->persistService->flush();
|
215
|
|
|
$this->persistService->commitTransaction();
|
216
|
|
|
|
217
|
|
|
return $entities;
|
218
|
|
|
} catch (\Throwable $e) {
|
219
|
|
|
$errorMessage = sprintf(
|
220
|
|
|
'Unable to save collection of type \'%s\' : %s',
|
221
|
|
|
$this->entityName,
|
222
|
|
|
$e->getMessage()
|
223
|
|
|
);
|
224
|
|
|
|
225
|
|
|
$this->logger->error($errorMessage);
|
226
|
|
|
|
227
|
|
|
throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
|
228
|
|
|
}
|
229
|
|
|
}
|
230
|
|
|
|
231
|
|
|
/**
|
232
|
|
|
* Delete an entity.
|
233
|
|
|
*
|
234
|
|
|
* @param EntityInterface|int|string $entity
|
235
|
|
|
* @param array $options
|
236
|
|
|
*
|
237
|
|
|
* @return bool
|
238
|
|
|
*
|
239
|
|
|
* @throws EntityRepositoryException
|
240
|
|
|
*/
|
241
|
|
|
public function delete($entity, array $options = []): bool
|
242
|
|
|
{
|
243
|
|
|
if (!is_int($entity) && !is_string($entity) && !$entity instanceof EntityInterface) {
|
|
|
|
|
244
|
|
|
throw new EntityRepositoryException(
|
245
|
|
|
sprintf(
|
246
|
|
|
'The \'entity\' argument must be \'int\' or an object of type \'%s\'; \'%s\' provided in \'%s\'',
|
247
|
|
|
EntityInterface::class,
|
248
|
|
|
(is_object($entity) ? get_class($entity) : gettype($entity)),
|
249
|
|
|
__METHOD__
|
250
|
|
|
)
|
251
|
|
|
);
|
252
|
|
|
}
|
253
|
|
|
|
254
|
|
|
if (! is_object($entity)) {
|
255
|
|
|
$id = (int) $entity;
|
256
|
|
|
$entity = $this->find($id);
|
257
|
|
|
|
258
|
|
|
if (null === $entity) {
|
259
|
|
|
$errorMessage = sprintf(
|
260
|
|
|
'Unable to delete entity \'%s::%s\': The entity could not be found',
|
261
|
|
|
$this->entityName,
|
262
|
|
|
$id
|
263
|
|
|
);
|
264
|
|
|
|
265
|
|
|
$this->logger->error($errorMessage);
|
266
|
|
|
|
267
|
|
|
throw new EntityNotFoundException($errorMessage);
|
268
|
|
|
}
|
269
|
|
|
}
|
270
|
|
|
|
271
|
|
|
try {
|
272
|
|
|
return $this->persistService->delete($entity, $options);
|
273
|
|
|
} catch (\Throwable $e) {
|
274
|
|
|
$errorMessage = sprintf('Unable to delete entity of type \'%s\': %s', $this->entityName, $e->getMessage());
|
275
|
|
|
|
276
|
|
|
$this->logger->error($errorMessage);
|
277
|
|
|
|
278
|
|
|
throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
|
279
|
|
|
}
|
280
|
|
|
}
|
281
|
|
|
|
282
|
|
|
/**
|
283
|
|
|
* @throws EntityRepositoryException
|
284
|
|
|
*/
|
285
|
|
|
public function clear(): void
|
286
|
|
|
{
|
287
|
|
|
try {
|
288
|
|
|
$this->persistService->clear();
|
289
|
|
|
} catch (\Throwable $e) {
|
290
|
|
|
$errorMessage = sprintf(
|
291
|
|
|
'Unable to clear entity of type \'%s\': %s',
|
292
|
|
|
$this->entityName,
|
293
|
|
|
$e->getMessage()
|
294
|
|
|
);
|
295
|
|
|
|
296
|
|
|
$this->logger->error($errorMessage);
|
297
|
|
|
|
298
|
|
|
throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
|
299
|
|
|
}
|
300
|
|
|
}
|
301
|
|
|
|
302
|
|
|
/**
|
303
|
|
|
* @param EntityInterface $entity
|
304
|
|
|
*
|
305
|
|
|
* @throws EntityRepositoryException
|
306
|
|
|
*/
|
307
|
|
|
public function refresh(EntityInterface $entity): void
|
308
|
|
|
{
|
309
|
|
|
try {
|
310
|
|
|
$this->persistService->refresh($entity);
|
311
|
|
|
} catch (\Throwable $e) {
|
312
|
|
|
$errorMessage = sprintf(
|
313
|
|
|
'Unable to refresh entity of type \'%s\': %s',
|
314
|
|
|
$this->entityName,
|
315
|
|
|
$e->getMessage()
|
316
|
|
|
);
|
317
|
|
|
|
318
|
|
|
$this->logger->error($errorMessage);
|
319
|
|
|
|
320
|
|
|
throw new EntityRepositoryException($errorMessage, $e->getCode(), $e);
|
321
|
|
|
}
|
322
|
|
|
}
|
323
|
|
|
}
|
324
|
|
|
|