1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Criteria; |
6
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Doctrine\ORM\EntityRepository; |
9
|
|
|
use Doctrine\ORM\LazyCriteriaCollection; |
10
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
11
|
|
|
use Doctrine\ORM\NativeQuery; |
12
|
|
|
use Doctrine\ORM\Query; |
13
|
|
|
use Doctrine\ORM\QueryBuilder; |
14
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
15
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactory; |
16
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactoryInterface; |
17
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
18
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class AbstractEntityRepository |
22
|
|
|
* |
23
|
|
|
* This provides a base class that handles instantiating the correctly configured EntityRepository and provides an |
24
|
|
|
* extensible baseline for further customisation |
25
|
|
|
* |
26
|
|
|
* We have extracted an interface from the standard Doctrine EntityRepository and implemented that |
27
|
|
|
* However, so we can add type safety, we can't "actually" implement it |
28
|
|
|
* |
29
|
|
|
* We have also deliberately left out the magic calls. Please make real methods in your concrete repository class |
30
|
|
|
* |
31
|
|
|
* Note, there are quite a few PHPMD warnings, however it needs to respect the legacy interface so they are being |
32
|
|
|
* suppressed |
33
|
|
|
* |
34
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories |
35
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
36
|
|
|
* @SuppressWarnings(PHPMD.ExcessivePublicCount) |
37
|
|
|
* @SuppressWarnings(PHPMD.NumberOfChildren) |
38
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
39
|
|
|
*/ |
40
|
|
|
abstract class AbstractEntityRepository implements EntityRepositoryInterface |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var EntityManagerInterface |
44
|
|
|
*/ |
45
|
|
|
protected $entityManager; |
46
|
|
|
/** |
47
|
|
|
* @var EntityRepository |
48
|
|
|
*/ |
49
|
|
|
protected $entityRepository; |
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $repositoryFactoryFqn; |
54
|
|
|
/** |
55
|
|
|
* @var ClassMetadata|null |
56
|
|
|
*/ |
57
|
|
|
protected $metaData; |
58
|
|
|
/** |
59
|
|
|
* @var NamespaceHelper |
60
|
|
|
*/ |
61
|
|
|
protected $namespaceHelper; |
62
|
|
|
/** |
63
|
|
|
* @var EntityFactoryInterface |
64
|
|
|
*/ |
65
|
|
|
private $entityFactory; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* AbstractEntityRepositoryFactory constructor. |
69
|
|
|
* |
70
|
|
|
* @param EntityManagerInterface $entityManager |
71
|
|
|
* @param EntityFactoryInterface $entityFactory |
72
|
|
|
* @param NamespaceHelper|null $namespaceHelper |
73
|
|
|
*/ |
74
|
14 |
|
public function __construct( |
75
|
|
|
EntityManagerInterface $entityManager, |
76
|
|
|
EntityFactoryInterface $entityFactory, |
77
|
|
|
NamespaceHelper $namespaceHelper |
78
|
|
|
) { |
79
|
14 |
|
$this->entityManager = $entityManager; |
80
|
14 |
|
$this->namespaceHelper = $namespaceHelper; |
81
|
14 |
|
$this->entityFactory = $entityFactory; |
82
|
14 |
|
$this->initRepository(); |
83
|
14 |
|
} |
84
|
|
|
|
85
|
14 |
|
protected function initRepository(): void |
86
|
|
|
{ |
87
|
14 |
|
if (null === $this->metaData) { |
88
|
14 |
|
$entityFqn = $this->getEntityFqn(); |
89
|
14 |
|
$this->metaData = $this->entityManager->getClassMetadata($entityFqn); |
90
|
|
|
} |
91
|
|
|
|
92
|
14 |
|
$this->entityRepository = new EntityRepository($this->entityManager, $this->metaData); |
93
|
14 |
|
} |
94
|
|
|
|
95
|
14 |
|
protected function getEntityFqn(): string |
96
|
|
|
{ |
97
|
14 |
|
return '\\' . \str_replace( |
98
|
|
|
[ |
99
|
14 |
|
'Entity\\Repositories', |
100
|
|
|
], |
101
|
|
|
[ |
102
|
14 |
|
'Entities', |
103
|
|
|
], |
104
|
14 |
|
$this->namespaceHelper->cropSuffix(static::class, 'Repository') |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return array|EntityInterface[] |
110
|
|
|
*/ |
111
|
1 |
|
public function findAll(): array |
112
|
|
|
{ |
113
|
1 |
|
return $this->initialiseEntities($this->entityRepository->findAll()); |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
private function initialiseEntities($entities) |
117
|
|
|
{ |
118
|
3 |
|
foreach ($entities as $entity) { |
119
|
3 |
|
$this->initialiseEntity($entity); |
120
|
|
|
} |
121
|
|
|
|
122
|
3 |
|
return $entities; |
123
|
|
|
} |
124
|
|
|
|
125
|
7 |
|
private function initialiseEntity(EntityInterface $entity) |
126
|
|
|
{ |
127
|
7 |
|
$this->entityFactory->initialiseEntity($entity); |
128
|
|
|
|
129
|
7 |
|
return $entity; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array|EntityInterface[] |
134
|
|
|
*/ |
135
|
1 |
|
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array |
136
|
|
|
{ |
137
|
1 |
|
return $this->initialiseEntities($this->entityRepository->findBy($criteria, $orderBy, $limit, $offset)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param mixed $id |
142
|
|
|
* @param int|null $lockMode |
143
|
|
|
* @param int|null $lockVersion |
144
|
|
|
* |
145
|
|
|
* @return EntityInterface |
146
|
|
|
* @throws DoctrineStaticMetaException |
147
|
|
|
*/ |
148
|
2 |
|
public function get($id, ?int $lockMode = null, ?int $lockVersion = null) |
149
|
|
|
{ |
150
|
|
|
try { |
151
|
2 |
|
$entity = $this->find($id, $lockMode, $lockVersion); |
152
|
1 |
|
} catch (ConversionException $e) { |
153
|
1 |
|
$error = 'Failed getting by id ' . $id |
154
|
1 |
|
. ', unless configured as an int ID entity, this should be a valid UUID'; |
155
|
1 |
|
throw new DoctrineStaticMetaException($error, $e->getCode(), $e); |
156
|
|
|
} |
157
|
1 |
|
if ($entity === null) { |
158
|
|
|
throw new DoctrineStaticMetaException('Could not find the entity with id ' . $id); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
return $this->initialiseEntity($entity); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param mixed $id |
166
|
|
|
* @param int|null $lockMode |
167
|
|
|
* @param int|null $lockVersion |
168
|
|
|
* |
169
|
|
|
* @return EntityInterface|null |
170
|
|
|
*/ |
171
|
3 |
|
public function find($id, ?int $lockMode = null, ?int $lockVersion = null) |
172
|
|
|
{ |
173
|
3 |
|
$entity = $this->entityRepository->find($id, $lockMode, $lockVersion); |
174
|
2 |
|
if (null === $entity) { |
175
|
|
|
return null; |
176
|
|
|
} |
177
|
2 |
|
if ($entity instanceof EntityInterface) { |
178
|
2 |
|
$this->initialiseEntity($entity); |
179
|
|
|
|
180
|
2 |
|
return $entity; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param array $criteria |
186
|
|
|
* @param array|null $orderBy |
187
|
|
|
* |
188
|
|
|
* @return EntityInterface |
189
|
|
|
*/ |
190
|
|
|
public function getOneBy(array $criteria, ?array $orderBy = null) |
191
|
|
|
{ |
192
|
|
|
$result = $this->findOneBy($criteria, $orderBy); |
193
|
|
|
if ($result === null) { |
194
|
|
|
throw new \RuntimeException('Could not find the entity'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $this->initialiseEntity($result); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param array $criteria |
202
|
|
|
* @param array|null $orderBy |
203
|
|
|
* |
204
|
|
|
* @return EntityInterface|null |
205
|
|
|
*/ |
206
|
3 |
|
public function findOneBy(array $criteria, ?array $orderBy = null) |
207
|
|
|
{ |
208
|
3 |
|
$entity = $this->entityRepository->findOneBy($criteria, $orderBy); |
209
|
3 |
|
if (null === $entity) { |
210
|
1 |
|
return null; |
211
|
|
|
} |
212
|
2 |
|
if ($entity instanceof EntityInterface) { |
213
|
2 |
|
$this->initialiseEntity($entity); |
214
|
|
|
|
215
|
2 |
|
return $entity; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
1 |
|
public function getClassName(): string |
220
|
|
|
{ |
221
|
1 |
|
return $this->entityRepository->getClassName(); |
222
|
|
|
} |
223
|
|
|
|
224
|
1 |
|
public function matching(Criteria $criteria): LazyCriteriaCollection |
225
|
|
|
{ |
226
|
1 |
|
$collection = $this->entityRepository->matching($criteria); |
227
|
1 |
|
if ($collection instanceof LazyCriteriaCollection) { |
|
|
|
|
228
|
1 |
|
return $this->initialiseEntities($collection); |
229
|
|
|
} |
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
1 |
|
public function createQueryBuilder(string $alias, string $indexBy = null): QueryBuilder |
233
|
|
|
{ |
234
|
1 |
|
return $this->entityRepository->createQueryBuilder($alias, $indexBy); |
235
|
|
|
} |
236
|
|
|
|
237
|
1 |
|
public function createResultSetMappingBuilder(string $alias): Query\ResultSetMappingBuilder |
238
|
|
|
{ |
239
|
1 |
|
return $this->entityRepository->createResultSetMappingBuilder($alias); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function createNamedQuery(string $queryName): Query |
243
|
|
|
{ |
244
|
|
|
return $this->entityRepository->createNamedQuery($queryName); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function createNativeNamedQuery(string $queryName): NativeQuery |
248
|
|
|
{ |
249
|
|
|
return $this->entityRepository->createNativeNamedQuery($queryName); |
250
|
|
|
} |
251
|
|
|
|
252
|
1 |
|
public function clear(): void |
253
|
|
|
{ |
254
|
1 |
|
$this->entityRepository->clear(); |
255
|
1 |
|
} |
256
|
|
|
|
257
|
1 |
|
public function count(array $criteria = []): int |
258
|
|
|
{ |
259
|
1 |
|
return $this->entityRepository->count($criteria); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|