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 (#75)
by joseph
14:44
created

AbstractEntityRepository   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Test Coverage

Coverage 89.55%

Importance

Changes 0
Metric Value
wmc 27
eloc 53
dl 0
loc 188
ccs 60
cts 67
cp 0.8955
rs 10
c 0
b 0
f 0

18 Methods

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