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