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