Complex classes like AbstractContainerMediator 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 AbstractContainerMediator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | abstract class AbstractContainerMediator extends Mediator implements ContainerMediatorInterface |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * Add a service as an event listener. |
||
| 48 | * |
||
| 49 | * @param string $eventName Name of the event the listener is being added for. |
||
| 50 | * @param array $listener Listener to be added. ['containerID', 'method'] |
||
| 51 | * @param int|string $priority Priority level for the added listener. |
||
| 52 | * |
||
| 53 | * @return ContainerMediatorInterface Fluent interface. |
||
| 54 | * @throws \DomainException |
||
| 55 | * @throws \InvalidArgumentException |
||
| 56 | */ |
||
| 57 | public function addServiceListener(string $eventName, array $listener, $priority = 0): ContainerMediatorInterface |
||
| 71 | /** |
||
| 72 | * @param array $events |
||
| 73 | * |
||
| 74 | * @return ContainerMediatorInterface |
||
| 75 | * @throws \DomainException |
||
| 76 | * @throws \InvalidArgumentException |
||
| 77 | * @throws \LengthException |
||
| 78 | */ |
||
| 79 | public function addServiceListenersByEventList(array $events): ContainerMediatorInterface |
||
| 84 | /** |
||
| 85 | * Add a service as a subscriber to event(s). |
||
| 86 | * |
||
| 87 | * @param ServiceSubscriberInterface $sub Service subscriber to be added. |
||
| 88 | * |
||
| 89 | * @return ContainerMediatorInterface Fluent interface. |
||
| 90 | * @throws \DomainException |
||
| 91 | * @throws \InvalidArgumentException |
||
| 92 | * @throws \LengthException |
||
| 93 | */ |
||
| 94 | public function addServiceSubscriber(ServiceSubscriberInterface $sub): ContainerMediatorInterface |
||
| 98 | /** |
||
| 99 | * @param string $eventName |
||
| 100 | * |
||
| 101 | * @return array |
||
| 102 | * @throws \DomainException |
||
| 103 | * @throws \InvalidArgumentException |
||
| 104 | */ |
||
| 105 | public function getListeners(string $eventName = ''): array |
||
| 110 | /** @noinspection GenericObjectTypeUsageInspection */ |
||
| 111 | /** |
||
| 112 | * This method is used any time the mediator need to get the actual instance |
||
| 113 | * of the class for an event. |
||
| 114 | * |
||
| 115 | * Normal will only be called during actual trigger of an event since lazy |
||
| 116 | * loading is used. |
||
| 117 | * |
||
| 118 | * @param string $serviceName |
||
| 119 | * |
||
| 120 | * @return object |
||
| 121 | */ |
||
| 122 | abstract public function getServiceByName(string $serviceName); |
||
| 123 | /** |
||
| 124 | * Get a list of service listeners for an event. |
||
| 125 | * |
||
| 126 | * Note that if event name is empty all listeners will be returned. Any event subscribers are also included in the |
||
| 127 | * list. |
||
| 128 | * |
||
| 129 | * @param string $eventName Name of the event the list of service listeners is needed for. |
||
| 130 | * |
||
| 131 | * @return array List of event service listeners or empty array if event is unknown or has no listeners or |
||
| 132 | * subscribers. |
||
| 133 | * @throws \InvalidArgumentException |
||
| 134 | */ |
||
| 135 | public function getServiceListeners(string $eventName = ''): array |
||
| 143 | /** |
||
| 144 | * Remove a service as an event listener. |
||
| 145 | * |
||
| 146 | * @param string $eventName Event name that listener is being removed from. |
||
| 147 | * @param array $listener Service listener to be removed. |
||
| 148 | * @param int|string $priority Priority level for the to be removed listener. |
||
| 149 | * |
||
| 150 | * @return ContainerMediatorInterface Fluent interface. |
||
| 151 | * @throws \DomainException |
||
| 152 | * @throws \InvalidArgumentException |
||
| 153 | */ |
||
| 154 | public function removeServiceListener(string $eventName, array $listener, $priority = 0): ContainerMediatorInterface |
||
| 196 | /** |
||
| 197 | * @param array $events |
||
| 198 | * |
||
| 199 | * @return ContainerMediatorInterface |
||
| 200 | * @throws \DomainException |
||
| 201 | * @throws \InvalidArgumentException |
||
| 202 | * @throws \LengthException |
||
| 203 | */ |
||
| 204 | public function removeServiceListenersByEventList(array $events): ContainerMediatorInterface |
||
| 209 | /** |
||
| 210 | * Remove a service subscriber from event(s). |
||
| 211 | * |
||
| 212 | * @param ServiceSubscriberInterface $sub Subscriber to be removed. |
||
| 213 | * |
||
| 214 | * @return ContainerMediatorInterface Fluent interface. |
||
| 215 | * @throws \DomainException |
||
| 216 | * @throws \InvalidArgumentException |
||
| 217 | * @throws \LengthException |
||
| 218 | */ |
||
| 219 | public function removeServiceSubscriber(ServiceSubscriberInterface $sub): ContainerMediatorInterface |
||
| 223 | /** |
||
| 224 | * This is used to bring in the service container that will be used. |
||
| 225 | * |
||
| 226 | * Though not required it would be considered best practice for this method |
||
| 227 | * to create a new instance of the container when given null. Another good |
||
| 228 | * practice is to call this method from the class constructor to allow |
||
| 229 | * easier testing. For examples of both have a look at |
||
| 230 | * PimpleContainerMediator. |
||
| 231 | * |
||
| 232 | * @see PimpleContainerMediator Container mediator implemented using Pimple. |
||
| 233 | * |
||
| 234 | * @param mixed $value The service container to be used. |
||
| 235 | * |
||
| 236 | * @return ContainerMediatorInterface Fluent interface. |
||
| 237 | */ |
||
| 238 | abstract public function setServiceContainer($value = null): ContainerMediatorInterface; |
||
| 239 | /** |
||
| 240 | * @param string $eventName |
||
| 241 | * @param string|int $priority |
||
| 242 | * |
||
| 243 | * @return int |
||
| 244 | */ |
||
| 245 | protected function getActualPriority(string $eventName, $priority): int |
||
| 262 | /** |
||
| 263 | * @param $listener |
||
| 264 | * |
||
| 265 | * @throws \InvalidArgumentException |
||
| 266 | */ |
||
| 267 | private function checkAllowedServiceListener($listener) |
||
| 294 | /** |
||
| 295 | * @param string $eventName |
||
| 296 | * |
||
| 297 | * @return ContainerMediatorInterface Fluent interface |
||
| 298 | * @throws \DomainException |
||
| 299 | * @throws \InvalidArgumentException |
||
| 300 | */ |
||
| 301 | private function lazyLoadServices(string $eventName = ''): ContainerMediatorInterface |
||
| 337 | /** |
||
| 338 | * @param string $eventName |
||
| 339 | * |
||
| 340 | * @return ContainerMediatorInterface Fluent Interface |
||
| 341 | * @throws \InvalidArgumentException |
||
| 342 | */ |
||
| 343 | private function sortServiceListeners(string $eventName): ContainerMediatorInterface |
||
| 362 | /** |
||
| 363 | * List of already loaded services. |
||
| 364 | * |
||
| 365 | * @var string[] $loadedServices |
||
| 366 | */ |
||
| 367 | private $loadedServices = []; |
||
| 368 | /** |
||
| 369 | * @var array $serviceListeners Holds the list of service listeners that will be lazy loaded when events are |
||
| 370 | * triggered. |
||
| 371 | */ |
||
| 372 | private $serviceListeners = []; |
||
| 373 | } |
||
| 374 |