1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
6
|
|
|
use Doctrine\Common\Collections\Criteria; |
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
use Doctrine\ORM\EntityRepository; |
10
|
|
|
use Doctrine\ORM\LazyCriteriaCollection; |
11
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
12
|
|
|
use Doctrine\ORM\NativeQuery; |
13
|
|
|
use Doctrine\ORM\Query; |
14
|
|
|
use Doctrine\ORM\QueryBuilder; |
15
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
16
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
17
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidatorFactory; |
18
|
|
|
use Symfony\Component\Validator\Mapping\Cache\DoctrineCache; |
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 EntityValidatorFactory |
44
|
|
|
*/ |
45
|
|
|
private static $entityValidatorFactory; |
46
|
|
|
/** |
47
|
|
|
* @var EntityManager |
48
|
|
|
*/ |
49
|
|
|
protected $entityManager; |
50
|
|
|
/** |
51
|
|
|
* @var EntityRepository |
52
|
|
|
*/ |
53
|
|
|
protected $entityRepository; |
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $repositoryFactoryFqn; |
58
|
|
|
/** |
59
|
|
|
* @var ClassMetadata|null |
60
|
|
|
*/ |
61
|
|
|
protected $metaData; |
62
|
|
|
/** |
63
|
|
|
* @var NamespaceHelper |
64
|
|
|
*/ |
65
|
|
|
protected $namespaceHelper; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* AbstractEntityRepositoryFactory constructor. |
69
|
|
|
* |
70
|
|
|
* @param EntityManager $entityManager |
71
|
|
|
* @param ClassMetadata|null $metaData |
72
|
|
|
* @param NamespaceHelper|null $namespaceHelper |
73
|
43 |
|
*/ |
74
|
|
|
public function __construct( |
75
|
|
|
EntityManagerInterface $entityManager, |
76
|
|
|
?ClassMetadata $metaData = null, |
77
|
|
|
?NamespaceHelper $namespaceHelper = null |
78
|
43 |
|
) { |
79
|
43 |
|
$this->entityManager = $entityManager; |
|
|
|
|
80
|
43 |
|
$this->metaData = $metaData; |
81
|
43 |
|
$this->namespaceHelper = ($namespaceHelper ?? new NamespaceHelper()); |
82
|
43 |
|
$this->initRepository(); |
83
|
|
|
} |
84
|
43 |
|
|
85
|
|
|
protected function initRepository(): void |
86
|
43 |
|
{ |
87
|
1 |
|
if (null === $this->metaData) { |
88
|
1 |
|
$entityFqn = $this->getEntityFqn(); |
89
|
|
|
$this->metaData = $this->entityManager->getClassMetadata($entityFqn); |
90
|
|
|
} |
91
|
43 |
|
|
92
|
43 |
|
$this->entityRepository = new EntityRepository($this->entityManager, $this->metaData); |
93
|
|
|
} |
94
|
1 |
|
|
95
|
|
|
protected function getEntityFqn(): string |
96
|
1 |
|
{ |
97
|
|
|
return '\\' . \str_replace( |
98
|
1 |
|
[ |
99
|
|
|
'Entity\\Repositories', |
100
|
|
|
], |
101
|
1 |
|
[ |
102
|
|
|
'Entities', |
103
|
1 |
|
], |
104
|
|
|
$this->namespaceHelper->cropSuffix(static::class, 'Repository') |
105
|
|
|
); |
106
|
|
|
} |
107
|
1 |
|
|
108
|
|
|
public function find($id, ?int $lockMode = null, ?int $lockVersion = null): ?EntityInterface |
109
|
1 |
|
{ |
110
|
1 |
|
$entity = $this->entityRepository->find($id, $lockMode, $lockVersion); |
111
|
1 |
|
if (null === $entity || $entity instanceof EntityInterface) { |
112
|
|
|
return $this->injectValidatorIfNotNull($entity); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
throw new \TypeError('Returned result is neither null nor an instance of EntityInterface'); |
115
|
|
|
} |
116
|
36 |
|
|
117
|
|
|
private function injectValidatorIfNotNull(?EntityInterface $entity): ?EntityInterface |
118
|
36 |
|
{ |
119
|
36 |
|
if (null !== $entity) { |
120
|
|
|
$entity->injectValidator(self::getEntityValidatorFactory()->getEntityValidator()); |
121
|
|
|
} |
122
|
36 |
|
|
123
|
|
|
return $entity; |
124
|
|
|
} |
125
|
36 |
|
|
126
|
|
|
private static function getEntityValidatorFactory(): EntityValidatorFactory |
127
|
36 |
|
{ |
128
|
|
|
if (null === self::$entityValidatorFactory) { |
129
|
|
|
/** |
130
|
|
|
* Can't use DI because Doctrine uses it's own factory method for repositories |
131
|
1 |
|
*/ |
132
|
1 |
|
self::$entityValidatorFactory = new EntityValidatorFactory( |
133
|
1 |
|
new DoctrineCache( |
134
|
|
|
new ArrayCache() |
135
|
|
|
) |
136
|
|
|
); |
137
|
|
|
} |
138
|
36 |
|
|
139
|
|
|
return self::$entityValidatorFactory; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return array|EntityInterface[] |
144
|
32 |
|
*/ |
145
|
|
|
public function findAll(): array |
146
|
32 |
|
{ |
147
|
|
|
$collection = $this->entityRepository->findAll(); |
148
|
32 |
|
|
149
|
|
|
return $this->injectValidatorToCollection($collection); |
|
|
|
|
150
|
|
|
} |
151
|
34 |
|
|
152
|
|
|
private function injectValidatorToCollection(iterable $collection) |
153
|
34 |
|
{ |
154
|
34 |
|
foreach ($collection as $entity) { |
155
|
|
|
$this->injectValidatorIfNotNull($entity); |
156
|
|
|
} |
157
|
34 |
|
|
158
|
|
|
return $collection; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return array|EntityInterface[] |
163
|
1 |
|
*/ |
164
|
|
|
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array |
165
|
1 |
|
{ |
166
|
|
|
$collection = $this->entityRepository->findBy($criteria, $orderBy, $limit, $offset); |
167
|
1 |
|
|
168
|
|
|
return $this->injectValidatorToCollection($collection); |
|
|
|
|
169
|
|
|
} |
170
|
1 |
|
|
171
|
|
|
public function findOneBy(array $criteria, ?array $orderBy = null): ?EntityInterface |
172
|
1 |
|
{ |
173
|
1 |
|
$entity = $this->entityRepository->findOneBy($criteria, $orderBy); |
174
|
1 |
|
if (null === $entity || $entity instanceof EntityInterface) { |
175
|
|
|
return $this->injectValidatorIfNotNull($entity); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
throw new \TypeError('Returned result is neither null nor an instance of EntityInterface'); |
178
|
|
|
} |
179
|
2 |
|
|
180
|
|
|
public function getClassName(): string |
181
|
2 |
|
{ |
182
|
|
|
return $this->entityRepository->getClassName(); |
183
|
|
|
} |
184
|
1 |
|
|
185
|
|
|
public function matching(Criteria $criteria): LazyCriteriaCollection |
186
|
1 |
|
{ |
187
|
1 |
|
$collection = $this->entityRepository->matching($criteria); |
188
|
1 |
|
if ($collection instanceof LazyCriteriaCollection) { |
|
|
|
|
189
|
|
|
return $this->injectValidatorToCollection($collection); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
throw new \TypeError('Returned result is not an instance of LazyCriteriaCollection'); |
192
|
|
|
} |
193
|
1 |
|
|
194
|
|
|
public function createQueryBuilder(string $alias, string $indexBy = null): QueryBuilder |
195
|
1 |
|
{ |
196
|
|
|
return $this->entityRepository->createQueryBuilder($alias, $indexBy); |
197
|
|
|
} |
198
|
1 |
|
|
199
|
|
|
public function createResultSetMappingBuilder(string $alias): Query\ResultSetMappingBuilder |
200
|
1 |
|
{ |
201
|
|
|
return $this->entityRepository->createResultSetMappingBuilder($alias); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function createNamedQuery(string $queryName): Query |
205
|
|
|
{ |
206
|
|
|
return $this->entityRepository->createNamedQuery($queryName); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function createNativeNamedQuery(string $queryName): NativeQuery |
210
|
|
|
{ |
211
|
|
|
return $this->entityRepository->createNativeNamedQuery($queryName); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* |
216
|
1 |
|
*/ |
217
|
|
|
public function clear(): void |
218
|
1 |
|
{ |
219
|
1 |
|
$this->entityRepository->clear(); |
220
|
|
|
} |
221
|
1 |
|
|
222
|
|
|
public function count(array $criteria): int |
223
|
1 |
|
{ |
224
|
|
|
return $this->entityRepository->count($criteria); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.