| Total Complexity | 49 |
| Total Lines | 522 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AbstractEntityRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractEntityRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | abstract class AbstractEntityRepository implements EntityRepositoryInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected string $entityName; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var QueryServiceInterface |
||
| 35 | */ |
||
| 36 | protected QueryServiceInterface $queryService; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var PersistServiceInterface |
||
| 40 | */ |
||
| 41 | protected PersistServiceInterface $persistService; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var LoggerInterface |
||
| 45 | */ |
||
| 46 | protected LoggerInterface $logger; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param string $entityName |
||
| 50 | * @param QueryServiceInterface $queryService |
||
| 51 | * @param PersistServiceInterface $persistService |
||
| 52 | * @param LoggerInterface $logger |
||
| 53 | */ |
||
| 54 | public function __construct( |
||
| 55 | string $entityName, |
||
| 56 | QueryServiceInterface $queryService, |
||
| 57 | PersistServiceInterface $persistService, |
||
| 58 | LoggerInterface $logger |
||
| 59 | ) { |
||
| 60 | $this->entityName = $entityName; |
||
| 61 | $this->queryService = $queryService; |
||
| 62 | $this->persistService = $persistService; |
||
| 63 | $this->logger = $logger; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Return the fully qualified class name of the mapped entity instance. |
||
| 68 | * |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | public function getClassName(): string |
||
| 72 | { |
||
| 73 | return $this->entityName; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Return a single entity instance matching the provided $id. |
||
| 78 | * |
||
| 79 | * @param string|int $id |
||
| 80 | * |
||
| 81 | * @return EntityInterface|null |
||
| 82 | * |
||
| 83 | * @throws EntityRepositoryException |
||
| 84 | */ |
||
| 85 | public function find($id): ?EntityInterface |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Return a single entity instance matching the provided $criteria. |
||
| 100 | * |
||
| 101 | * @param array<mixed> $criteria The entity filter criteria. |
||
| 102 | * |
||
| 103 | * @return EntityInterface|null |
||
| 104 | * |
||
| 105 | * @throws EntityRepositoryException |
||
| 106 | */ |
||
| 107 | public function findOneBy(array $criteria): ?EntityInterface |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Return all of the entities within the collection. |
||
| 122 | * |
||
| 123 | * @return EntityInterface[]|iterable |
||
| 124 | * |
||
| 125 | * @throws EntityRepositoryException |
||
| 126 | */ |
||
| 127 | public function findAll(): iterable |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Return a collection of entities that match the provided $criteria. |
||
| 134 | * |
||
| 135 | * @param array<string, mixed> $criteria |
||
| 136 | * @param array<string, string>|null $orderBy |
||
| 137 | * @param int|null $limit |
||
| 138 | * @param int|null $offset |
||
| 139 | * |
||
| 140 | * @return EntityInterface[]|iterable |
||
| 141 | * |
||
| 142 | * @throws EntityRepositoryException |
||
| 143 | */ |
||
| 144 | public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): iterable |
||
| 145 | { |
||
| 146 | $options = []; |
||
| 147 | |||
| 148 | try { |
||
| 149 | if (null !== $orderBy) { |
||
| 150 | $options[QueryServiceOption::ORDER_BY] = $orderBy; |
||
| 151 | } |
||
| 152 | |||
| 153 | if (null !== $limit) { |
||
| 154 | $options[QueryServiceOption::LIMIT] = $limit; |
||
| 155 | } |
||
| 156 | |||
| 157 | if (null !== $offset) { |
||
| 158 | $options[QueryServiceOption::OFFSET] = $offset; |
||
| 159 | } |
||
| 160 | |||
| 161 | return $this->queryService->findMany($criteria, $options); |
||
| 162 | } catch (\Throwable $e) { |
||
| 163 | $errorMessage = sprintf( |
||
| 164 | 'Unable to return a collection of type \'%s\': %s', |
||
| 165 | $this->entityName, |
||
| 166 | $e->getMessage() |
||
| 167 | ); |
||
| 168 | |||
| 169 | $this->logger->error( |
||
| 170 | $errorMessage, |
||
| 171 | ['exception' => $e, 'criteria' => $criteria, 'options' => $options] |
||
| 172 | ); |
||
| 173 | |||
| 174 | throw new EntityRepositoryException($errorMessage, $e->getCode(), $e); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Save a single entity instance. |
||
| 180 | * |
||
| 181 | * @param EntityInterface $entity |
||
| 182 | * @param array<string, mixed> $options |
||
| 183 | * |
||
| 184 | * @return EntityInterface |
||
| 185 | * |
||
| 186 | * @throws EntityRepositoryException |
||
| 187 | */ |
||
| 188 | public function save(EntityInterface $entity, array $options = []): EntityInterface |
||
| 189 | { |
||
| 190 | try { |
||
| 191 | return $this->persistService->save($entity, $options); |
||
| 192 | } catch (\Throwable $e) { |
||
| 193 | $errorMessage = sprintf('Unable to save entity of type \'%s\': %s', $this->entityName, $e->getMessage()); |
||
| 194 | |||
| 195 | $this->logger->error($errorMessage); |
||
| 196 | |||
| 197 | throw new EntityRepositoryException($errorMessage, $e->getCode(), $e); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Save a collection of entities in a single transaction. |
||
| 203 | * |
||
| 204 | * @param iterable<EntityInterface> $collection The collection of entities that should be saved. |
||
| 205 | * @param array<string, mixed> $options the optional save options. |
||
| 206 | * |
||
| 207 | * @return iterable<EntityInterface> |
||
| 208 | * |
||
| 209 | * @throws EntityRepositoryException If the save cannot be completed |
||
| 210 | */ |
||
| 211 | public function saveCollection(iterable $collection, array $options = []): iterable |
||
| 212 | { |
||
| 213 | $flushMode = $options[EntityEventOption::FLUSH_MODE] ?? FlushMode::ENABLED; |
||
| 214 | $transactionMode = $options[EntityEventOption::TRANSACTION_MODE] ?? TransactionMode::ENABLED; |
||
| 215 | |||
| 216 | try { |
||
| 217 | if (TransactionMode::ENABLED === $transactionMode) { |
||
| 218 | $this->logger->info( |
||
| 219 | sprintf('Starting collection transaction for entity type \'%s\'', $this->entityName) |
||
| 220 | ); |
||
| 221 | $this->persistService->beginTransaction(); |
||
| 222 | } |
||
| 223 | |||
| 224 | $entities = []; |
||
| 225 | $saveOptions = [ |
||
| 226 | EntityEventOption::FLUSH_MODE => FlushMode::DISABLED, |
||
| 227 | EntityEventOption::TRANSACTION_MODE => TransactionMode::DISABLED, |
||
| 228 | ]; |
||
| 229 | |||
| 230 | foreach ($collection as $entity) { |
||
| 231 | $entities[] = $this->save($entity, $saveOptions); |
||
| 232 | } |
||
| 233 | |||
| 234 | $this->logger->info( |
||
| 235 | sprintf( |
||
| 236 | 'Completed collection save of \'%d\' entities of type \'%s\'', |
||
| 237 | count($entities), |
||
| 238 | $this->entityName |
||
| 239 | ) |
||
| 240 | ); |
||
| 241 | |||
| 242 | if (FlushMode::ENABLED === $flushMode) { |
||
| 243 | $this->logger->info( |
||
| 244 | sprintf('Performing collection flush operation for entity type \'%s\'', $this->entityName) |
||
| 245 | ); |
||
| 246 | $this->persistService->flush(); |
||
| 247 | } |
||
| 248 | |||
| 249 | if (TransactionMode::ENABLED === $transactionMode) { |
||
| 250 | $this->logger->info( |
||
| 251 | sprintf('Committing collection transaction for entity type \'%s\'', $this->entityName) |
||
| 252 | ); |
||
| 253 | $this->persistService->commitTransaction(); |
||
| 254 | } |
||
| 255 | |||
| 256 | return $entities; |
||
| 257 | } catch (\Throwable $e) { |
||
| 258 | if (TransactionMode::ENABLED === $transactionMode) { |
||
| 259 | $this->logger->info( |
||
| 260 | sprintf('Rolling back collection transaction for entity type \'%s\'', $this->entityName) |
||
| 261 | ); |
||
| 262 | $this->persistService->rollbackTransaction(); |
||
| 263 | } |
||
| 264 | |||
| 265 | $errorMessage = sprintf( |
||
| 266 | 'Unable to save collection of type \'%s\' : %s', |
||
| 267 | $this->entityName, |
||
| 268 | $e->getMessage() |
||
| 269 | ); |
||
| 270 | |||
| 271 | $this->logger->error($errorMessage); |
||
| 272 | |||
| 273 | throw new EntityRepositoryException($errorMessage, $e->getCode(), $e); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Delete an entity. |
||
| 279 | * |
||
| 280 | * @param EntityInterface|string|int|mixed $entity |
||
| 281 | * @param array<string, mixed> $options |
||
| 282 | * |
||
| 283 | * @return bool |
||
| 284 | * |
||
| 285 | * @throws EntityRepositoryException |
||
| 286 | */ |
||
| 287 | public function delete($entity, array $options = []): bool |
||
| 288 | { |
||
| 289 | if (is_string($entity) || is_int($entity)) { |
||
| 290 | $id = $entity; |
||
| 291 | $entity = $this->find($id); |
||
| 292 | |||
| 293 | if (null === $entity) { |
||
| 294 | $errorMessage = sprintf( |
||
| 295 | 'Unable to delete entity \'%s::%s\': The entity could not be found', |
||
| 296 | $this->entityName, |
||
| 297 | $id |
||
| 298 | ); |
||
| 299 | |||
| 300 | $this->logger->error($errorMessage); |
||
| 301 | |||
| 302 | throw new EntityNotFoundException($errorMessage); |
||
| 303 | } |
||
| 304 | } elseif (!$entity instanceof EntityInterface) { |
||
| 305 | $errorMessage = sprintf( |
||
| 306 | 'The \'entity\' argument must be a \'string\' or an object of type \'%s\'; ' |
||
| 307 | . '\'%s\' provided in \'%s::%s\'', |
||
| 308 | EntityInterface::class, |
||
| 309 | (is_object($entity) ? get_class($entity) : gettype($entity)), |
||
| 310 | static::class, |
||
| 311 | __FUNCTION__ |
||
| 312 | ); |
||
| 313 | |||
| 314 | $this->logger->error($errorMessage); |
||
| 315 | |||
| 316 | throw new EntityRepositoryException($errorMessage); |
||
| 317 | } |
||
| 318 | |||
| 319 | |||
| 320 | try { |
||
| 321 | return $this->persistService->delete($entity, $options); |
||
| 322 | } catch (\Throwable $e) { |
||
| 323 | $errorMessage = sprintf( |
||
| 324 | 'Unable to delete entity of type \'%s\': %s', |
||
| 325 | $this->entityName, |
||
| 326 | $e->getMessage() |
||
| 327 | ); |
||
| 328 | |||
| 329 | $this->logger->error($errorMessage, ['exception' => $e]); |
||
| 330 | |||
| 331 | throw new EntityRepositoryException($errorMessage, $e->getCode(), $e); |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Perform a deletion of a collection of entities. |
||
| 337 | * |
||
| 338 | * @param iterable<EntityInterface> $collection |
||
| 339 | * @param array<string, mixed> $options |
||
| 340 | * |
||
| 341 | * @return int |
||
| 342 | * |
||
| 343 | * @throws EntityRepositoryException |
||
| 344 | */ |
||
| 345 | public function deleteCollection(iterable $collection, array $options = []): int |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @throws EntityRepositoryException |
||
| 395 | */ |
||
| 396 | public function clear(): void |
||
| 397 | { |
||
| 398 | try { |
||
| 399 | $this->persistService->clear(); |
||
| 400 | } catch (\Throwable $e) { |
||
| 401 | $errorMessage = sprintf( |
||
| 402 | 'Unable to clear entity of type \'%s\': %s', |
||
| 403 | $this->entityName, |
||
| 404 | $e->getMessage() |
||
| 405 | ); |
||
| 406 | |||
| 407 | $this->logger->error($errorMessage, ['exception' => $e]); |
||
| 408 | |||
| 409 | throw new EntityRepositoryException($errorMessage, $e->getCode(), $e); |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param EntityInterface $entity |
||
| 415 | * |
||
| 416 | * @throws EntityRepositoryException |
||
| 417 | */ |
||
| 418 | public function refresh(EntityInterface $entity): void |
||
| 419 | { |
||
| 420 | try { |
||
| 421 | $this->persistService->refresh($entity); |
||
| 422 | } catch (\Throwable $e) { |
||
| 423 | $errorMessage = sprintf( |
||
| 424 | 'Unable to refresh entity of type \'%s\': %s', |
||
| 425 | $this->entityName, |
||
| 426 | $e->getMessage() |
||
| 427 | ); |
||
| 428 | |||
| 429 | $this->logger->error($errorMessage, ['exception' => $e, 'id' => $entity->getId()]); |
||
| 430 | |||
| 431 | throw new EntityRepositoryException($errorMessage, $e->getCode(), $e); |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Execute query builder or query instance and return the results. |
||
| 437 | * |
||
| 438 | * @param object $query |
||
| 439 | * @param array<string, mixed> $options |
||
| 440 | * |
||
| 441 | * @return EntityInterface[]|iterable |
||
| 442 | * |
||
| 443 | * @throws EntityRepositoryException |
||
| 444 | */ |
||
| 445 | protected function executeQuery(object $query, array $options = []) |
||
| 461 | } |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Return a single entity instance. NULL will be returned if the result set contains 0 or more than 1 result. |
||
| 466 | * |
||
| 467 | * Optionally control the object hydration with QueryServiceOption::HYDRATE_MODE. |
||
| 468 | * |
||
| 469 | * @param object $query |
||
| 470 | * @param array<string, mixed> $options |
||
| 471 | * |
||
| 472 | * @return array<mixed>|EntityInterface|null |
||
| 473 | * |
||
| 474 | * @throws EntityRepositoryException |
||
| 475 | */ |
||
| 476 | protected function getSingleResultOrNull(object $query, array $options = []) |
||
| 492 | } |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Return a result set containing a single array result. NULL will be returned if the result set |
||
| 497 | * contains 0 or more than 1 result. |
||
| 498 | * |
||
| 499 | * @param object $query |
||
| 500 | * @param array<string, mixed> $options |
||
| 501 | * |
||
| 502 | * @return array<mixed>|null |
||
| 503 | * |
||
| 504 | * @throws EntityRepositoryException |
||
| 505 | */ |
||
| 506 | protected function getSingleArrayResultOrNull(object $query, array $options = []): ?array |
||
| 507 | { |
||
| 508 | $options = array_replace_recursive( |
||
| 509 | $options, |
||
| 510 | [ |
||
| 511 | QueryServiceOption::HYDRATION_MODE => HydrateMode::ARRAY, |
||
| 512 | ] |
||
| 513 | ); |
||
| 514 | |||
| 515 | $result = $this->getSingleResultOrNull($query, $options); |
||
| 516 | |||
| 517 | return is_array($result) ? $result : null; |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Resolve the Doctrine query object from a possible QueryBuilder instance. |
||
| 522 | * |
||
| 523 | * @param object $query |
||
| 524 | * |
||
| 525 | * @return AbstractQuery |
||
| 526 | * |
||
| 527 | * @throws EntityRepositoryException |
||
| 528 | */ |
||
| 529 | private function resolveQuery(object $query): AbstractQuery |
||
| 548 | } |
||
| 549 | } |
||
| 550 |