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