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 handeling 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 | */ |
||
| 60 | 90 | public function __construct(EntityRepositoryInterface $repository, $entityClassName) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Gets the repository that is used by the service. |
||
| 70 | * |
||
| 71 | * @return EntityRepositoryInterface |
||
| 72 | */ |
||
| 73 | 75 | protected function getRepository() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Get the pre initialized event object |
||
| 80 | * |
||
| 81 | * @return EntityEvent |
||
| 82 | */ |
||
| 83 | 90 | protected function getEvent() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Will create an EventManager when no EventManager was provided. |
||
| 96 | * The returned EventManager is used to handle events triggered by this service instance. |
||
| 97 | * |
||
| 98 | * @return EventManagerInterface |
||
| 99 | */ |
||
| 100 | 45 | public function getEventManager() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Set the EventManager used by this service instance to handle its events. |
||
| 111 | * It will take care of disabling the old EventManager and will subscribe the internal |
||
| 112 | * listeners to the new EventManager |
||
| 113 | * |
||
| 114 | * @param EventManagerInterface $eventManager |
||
| 115 | */ |
||
| 116 | 45 | public function setEventManager(EventManagerInterface $eventManager) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritDoc} |
||
| 139 | */ |
||
| 140 | 45 | public function attach(EventManagerInterface $events, $priority = 1) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Returns the FQCN of the entity handled by this service. |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | 81 | protected function getEntityServiceName() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Deletes the given object from the repository |
||
| 178 | * |
||
| 179 | * @param object $entity The entity to delete. |
||
| 180 | * @return mixed |
||
| 181 | */ |
||
| 182 | 6 | public function delete($entity) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Deletes all objects matching the criteria from the repository |
||
| 195 | * |
||
| 196 | * @param array|Criteria $criteria The criteria values to match on. |
||
| 197 | * @return mixed |
||
| 198 | */ |
||
| 199 | 6 | public function deleteBy($criteria) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Count the objects matching the criteria respecting the order, limit and offset. |
||
| 212 | * |
||
| 213 | * @param array|Criteria $criteria The criteria values to match on. |
||
| 214 | * @return int |
||
| 215 | */ |
||
| 216 | 6 | public function countBy($criteria) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Find one object in the repository matching the $id |
||
| 229 | * |
||
| 230 | * @param mixed $id The id of the entity. |
||
| 231 | * @return object|null |
||
| 232 | */ |
||
| 233 | 9 | public function find($id) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Finds all entities in the repository. |
||
| 246 | * |
||
| 247 | * @return array Returns the entities that exist. |
||
| 248 | */ |
||
| 249 | 6 | public function findAll() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Find one or more objects in the repository matching the criteria respecting the order, limit and offset |
||
| 260 | * |
||
| 261 | * @param array|Criteria $criteria The array with criteria to search on. |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | 6 | public function findBy($criteria) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Find one object in the repository matching the criteria |
||
| 277 | * |
||
| 278 | * @param array|Criteria $criteria The criteria values to match on. |
||
| 279 | * @return object|null |
||
| 280 | */ |
||
| 281 | 6 | public function findOneBy($criteria) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Persist the given entity |
||
| 294 | * |
||
| 295 | * @param object $entity |
||
| 296 | * @return mixed |
||
| 297 | * @throws RuntimeException |
||
| 298 | */ |
||
| 299 | 6 | public function persist($entity) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Flushes the provided entity or all persisted entities when no entity is provided. |
||
| 312 | * |
||
| 313 | * @param object $entity |
||
| 314 | * @return mixed |
||
| 315 | * @throws RuntimeException |
||
| 316 | */ |
||
| 317 | 6 | public function flush($entity = null) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * will prepare the event object and trigger the event using the internal EventManager |
||
| 330 | * |
||
| 331 | * @param string $name |
||
| 332 | * @param array $params |
||
| 333 | * @return mixed |
||
| 334 | */ |
||
| 335 | 30 | protected function trigger($name, array $params) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Starts a new transaction. |
||
| 352 | * |
||
| 353 | * @throws ServiceException |
||
| 354 | */ |
||
| 355 | 6 | public function beginTransaction() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Commits a started transaction. |
||
| 369 | * |
||
| 370 | * @throws ServiceException |
||
| 371 | */ |
||
| 372 | 6 | public function commitTransaction() |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Rolls back a started transaction. |
||
| 386 | * |
||
| 387 | * @throws ServiceException |
||
| 388 | */ |
||
| 389 | 6 | public function rollbackTransaction() |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Returns true when possible to start an transaction |
||
| 403 | */ |
||
| 404 | 18 | public function isTransactionEnabled() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Returns true when the repository for $entityName is writable |
||
| 411 | * |
||
| 412 | * @return bool |
||
| 413 | */ |
||
| 414 | 12 | protected function isRepositoryWritable() |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Returns true when the repository for $entityName is readable |
||
| 421 | * |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | 33 | protected function isRepositoryReadable() |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Returns true when the repository for $entityName has delete behavior |
||
| 431 | * |
||
| 432 | * @return bool |
||
| 433 | */ |
||
| 434 | 12 | protected function isRepositoryDeletable() |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Throws an exception for cases where it's not possible to delete from the repository. |
||
| 441 | * |
||
| 442 | * @throws RuntimeException |
||
| 443 | */ |
||
| 444 | 6 | private function createNotDeletableException() |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Throws an exception for cases where it's not possible to read from the repository. |
||
| 454 | * |
||
| 455 | * @throws RuntimeException |
||
| 456 | */ |
||
| 457 | 15 | private function createNotReadableException() |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Throws an exception for cases where it's not possible to write to the repository. |
||
| 467 | * |
||
| 468 | * @throws RuntimeException |
||
| 469 | */ |
||
| 470 | 6 | private function createNotWritableException() |
|
| 477 | } |
||
| 478 |