Complex classes like AbstractEntityService 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AbstractEntityService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | abstract class AbstractEntityService extends AbstractListenerAggregate implements |
||
| 30 | EntityServiceInterface, |
||
| 31 | TransactionAwareInterface |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * The repository that is used for this entity service. |
||
| 35 | * |
||
| 36 | * @var EntityRepositoryInterface |
||
| 37 | */ |
||
| 38 | private $repository; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * EventManager handling all events triggered by this service |
||
| 42 | * |
||
| 43 | * @var EventManagerInterface |
||
| 44 | */ |
||
| 45 | private $eventManager; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Initialized Event |
||
| 49 | * |
||
| 50 | * @var EntityEvent |
||
| 51 | */ |
||
| 52 | private $event; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Initializes a new instance of this class. |
||
| 56 | * |
||
| 57 | * @param EntityRepositoryInterface $repository The repository that is used to communicate with. |
||
| 58 | * @param string $entityClassName The FQCN of the entity. |
||
| 59 | * @throws \PolderKnowledge\EntityService\Exception\RuntimeException |
||
| 60 | */ |
||
| 61 | 90 | public function __construct(EntityRepositoryInterface $repository, $entityClassName) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Gets the repository that is used by the service. |
||
| 71 | * |
||
| 72 | * @return EntityRepositoryInterface |
||
| 73 | */ |
||
| 74 | 75 | protected function getRepository() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Get the pre initialized event object |
||
| 81 | * |
||
| 82 | * @return EntityEvent |
||
| 83 | */ |
||
| 84 | 90 | protected function getEvent() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Will create an EventManager when no EventManager was provided. |
||
| 97 | * The returned EventManager is used to handle events triggered by this service instance. |
||
| 98 | * |
||
| 99 | * @return EventManagerInterface |
||
| 100 | */ |
||
| 101 | 45 | public function getEventManager() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Set the EventManager used by this service instance to handle its events. |
||
| 112 | * It will take care of disabling the old EventManager and will subscribe the internal |
||
| 113 | * listeners to the new EventManager |
||
| 114 | * |
||
| 115 | * @param EventManagerInterface $eventManager |
||
| 116 | */ |
||
| 117 | 45 | public function setEventManager(EventManagerInterface $eventManager) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritDoc} |
||
| 140 | */ |
||
| 141 | 45 | public function attach(EventManagerInterface $events, $priority = 1) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Returns the FQCN of the entity handled by this service. |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | 81 | protected function getEntityServiceName() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Deletes the given object from the repository |
||
| 179 | * |
||
| 180 | * @param object $entity The entity to delete. |
||
| 181 | * @return mixed |
||
| 182 | * @throws \Zend\EventManager\Exception\InvalidArgumentException |
||
| 183 | * @throws \PolderKnowledge\EntityService\Exception\RuntimeException |
||
| 184 | */ |
||
| 185 | 6 | public function delete($entity) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Deletes all objects matching the criteria from the repository |
||
| 198 | * |
||
| 199 | * @param array|Criteria $criteria The criteria values to match on. |
||
| 200 | * @return mixed |
||
| 201 | * @throws \Zend\EventManager\Exception\InvalidArgumentException |
||
| 202 | * @throws \PolderKnowledge\EntityService\Exception\RuntimeException |
||
| 203 | */ |
||
| 204 | 6 | public function deleteBy($criteria) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Count the objects matching the criteria respecting the order, limit and offset. |
||
| 217 | * |
||
| 218 | * @param array|Criteria $criteria The criteria values to match on. |
||
| 219 | * @return int |
||
| 220 | * @throws \Zend\EventManager\Exception\InvalidArgumentException |
||
| 221 | * @throws \PolderKnowledge\EntityService\Exception\RuntimeException |
||
| 222 | */ |
||
| 223 | 6 | public function countBy($criteria) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Find one object in the repository matching the $id |
||
| 236 | * |
||
| 237 | * @param mixed $id The id of the entity. |
||
| 238 | * @return object|null |
||
| 239 | * @throws \Zend\EventManager\Exception\InvalidArgumentException |
||
| 240 | * @throws \PolderKnowledge\EntityService\Exception\RuntimeException |
||
| 241 | */ |
||
| 242 | 9 | public function find($id) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Finds all entities in the repository. |
||
| 255 | * |
||
| 256 | * @return array Returns the entities that exist. |
||
| 257 | * @throws \Zend\EventManager\Exception\InvalidArgumentException |
||
| 258 | * @throws \PolderKnowledge\EntityService\Exception\RuntimeException |
||
| 259 | */ |
||
| 260 | 6 | public function findAll() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Find one or more objects in the repository matching the criteria respecting the order, limit and offset |
||
| 271 | * |
||
| 272 | * @param array|Criteria $criteria The array with criteria to search on. |
||
| 273 | * @return array |
||
| 274 | * @throws \Zend\EventManager\Exception\InvalidArgumentException |
||
| 275 | * @throws \PolderKnowledge\EntityService\Exception\RuntimeException |
||
| 276 | */ |
||
| 277 | 6 | public function findBy($criteria) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Find one object in the repository matching the criteria |
||
| 290 | * |
||
| 291 | * @param array|Criteria $criteria The criteria values to match on. |
||
| 292 | * @return object|null |
||
| 293 | * @throws \Zend\EventManager\Exception\InvalidArgumentException |
||
| 294 | * @throws \PolderKnowledge\EntityService\Exception\RuntimeException |
||
| 295 | */ |
||
| 296 | 6 | public function findOneBy($criteria) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Persist the given entity |
||
| 309 | * |
||
| 310 | * @param object $entity |
||
| 311 | * @return mixed |
||
| 312 | * @throws \Zend\EventManager\Exception\InvalidArgumentException |
||
| 313 | * @throws RuntimeException |
||
| 314 | */ |
||
| 315 | 6 | public function persist($entity) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Flushes the provided entity or all persisted entities when no entity is provided. |
||
| 328 | * |
||
| 329 | * @param object $entity |
||
| 330 | * @return mixed |
||
| 331 | * @throws \Zend\EventManager\Exception\InvalidArgumentException |
||
| 332 | * @throws RuntimeException |
||
| 333 | */ |
||
| 334 | 6 | public function flush($entity = null) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * will prepare the event object and trigger the event using the internal EventManager |
||
| 347 | * |
||
| 348 | * @param string $name |
||
| 349 | * @param array $params |
||
| 350 | * @return mixed |
||
| 351 | * @throws \Zend\EventManager\Exception\InvalidArgumentException |
||
| 352 | * @throws \PolderKnowledge\EntityService\Exception\RuntimeException |
||
| 353 | */ |
||
| 354 | 30 | protected function trigger($name, array $params) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Starts a new transaction. |
||
| 371 | * |
||
| 372 | * @throws ServiceException |
||
| 373 | */ |
||
| 374 | 6 | public function beginTransaction() |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Commits a started transaction. |
||
| 388 | * |
||
| 389 | * @throws ServiceException |
||
| 390 | */ |
||
| 391 | 6 | public function commitTransaction() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Rolls back a started transaction. |
||
| 405 | * |
||
| 406 | * @throws ServiceException |
||
| 407 | */ |
||
| 408 | 6 | public function rollbackTransaction() |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Returns true when possible to start an transaction |
||
| 422 | */ |
||
| 423 | 18 | public function isTransactionEnabled() |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Returns true when the repository for $entityName is writable |
||
| 430 | * |
||
| 431 | * @return bool |
||
| 432 | */ |
||
| 433 | 12 | protected function isRepositoryWritable() |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Returns true when the repository for $entityName is readable |
||
| 440 | * |
||
| 441 | * @return bool |
||
| 442 | */ |
||
| 443 | 33 | protected function isRepositoryReadable() |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Returns true when the repository for $entityName has delete behavior |
||
| 450 | * |
||
| 451 | * @return bool |
||
| 452 | */ |
||
| 453 | 12 | protected function isRepositoryDeletable() |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Throws an exception for cases where it's not possible to delete from the repository. |
||
| 460 | * |
||
| 461 | * @throws RuntimeException |
||
| 462 | */ |
||
| 463 | 6 | private function createNotDeletableException() |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Throws an exception for cases where it's not possible to read from the repository. |
||
| 473 | * |
||
| 474 | * @throws RuntimeException |
||
| 475 | */ |
||
| 476 | 15 | private function createNotReadableException() |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Throws an exception for cases where it's not possible to write to the repository. |
||
| 486 | * |
||
| 487 | * @throws RuntimeException |
||
| 488 | */ |
||
| 489 | 6 | private function createNotWritableException() |
|
| 496 | } |
||
| 497 |