GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#154)
by joseph
32:32
created

AbstractEntityRepository::createQueryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
270
    }
271
272
    public function createQueryBuilder(string $alias, string $indexBy = null): QueryBuilder
273
    {
274
        return $this->entityRepository->createQueryBuilder($alias, $indexBy);
275
    }
276
277
    public function createResultSetMappingBuilder(string $alias): Query\ResultSetMappingBuilder
278
    {
279
        return $this->entityRepository->createResultSetMappingBuilder($alias);
280
    }
281
282
    public function createNamedQuery(string $queryName): Query
283
    {
284
        return $this->entityRepository->createNamedQuery($queryName);
285
    }
286
287
    public function createNativeNamedQuery(string $queryName): NativeQuery
288
    {
289
        return $this->entityRepository->createNativeNamedQuery($queryName);
290
    }
291
292
    public function clear(): void
293
    {
294
        $this->entityRepository->clear();
295
    }
296
}
297