edmondscommerce /
doctrine-static-meta
| 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
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
|
|||
| 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
|
|||
| 191 | 1 | return $this->injectValidatorToCollection($collection); |
|
|
0 ignored issues
–
show
|
|||
| 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 |