| Total Complexity | 58 |
| Total Lines | 428 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like RegistrationContext 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 RegistrationContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class RegistrationContext { |
||
| 54 | |||
| 55 | /** @var ServiceRegistration<ICapability>[] */ |
||
| 56 | private $capabilities = []; |
||
| 57 | |||
| 58 | /** @var ServiceRegistration<IReporter>[] */ |
||
| 59 | private $crashReporters = []; |
||
| 60 | |||
| 61 | /** @var ServiceRegistration<IWidget>[] */ |
||
| 62 | private $dashboardPanels = []; |
||
| 63 | |||
| 64 | /** @var ServiceFactoryRegistration[] */ |
||
| 65 | private $services = []; |
||
| 66 | |||
| 67 | /** @var ServiceAliasRegistration[] */ |
||
| 68 | private $aliases = []; |
||
| 69 | |||
| 70 | /** @var ParameterRegistration[] */ |
||
| 71 | private $parameters = []; |
||
| 72 | |||
| 73 | /** @var EventListenerRegistration[] */ |
||
| 74 | private $eventListeners = []; |
||
| 75 | |||
| 76 | /** @var ServiceRegistration<Middleware>[] */ |
||
| 77 | private $middlewares = []; |
||
| 78 | |||
| 79 | /** @var ServiceRegistration<IProvider>[] */ |
||
| 80 | private $searchProviders = []; |
||
| 81 | |||
| 82 | /** @var ServiceRegistration<IAlternativeLogin>[] */ |
||
| 83 | private $alternativeLogins = []; |
||
| 84 | |||
| 85 | /** @var ServiceRegistration<InitialStateProvider>[] */ |
||
| 86 | private $initialStates = []; |
||
| 87 | |||
| 88 | /** @var ServiceRegistration<IHandler>[] */ |
||
| 89 | private $wellKnownHandlers = []; |
||
| 90 | |||
| 91 | /** @var ServiceRegistration<ICustomTemplateProvider>[] */ |
||
| 92 | private $templateProviders = []; |
||
| 93 | |||
| 94 | /** @var ServiceRegistration<INotifier>[] */ |
||
| 95 | private $notifierServices; |
||
| 96 | |||
| 97 | /** @var ILogger */ |
||
| 98 | private $logger; |
||
| 99 | |||
| 100 | public function __construct(ILogger $logger) { |
||
| 101 | $this->logger = $logger; |
||
| 102 | } |
||
| 103 | |||
| 104 | public function for(string $appId): IRegistrationContext { |
||
| 105 | return new class($appId, $this) implements IRegistrationContext { |
||
| 106 | /** @var string */ |
||
| 107 | private $appId; |
||
| 108 | |||
| 109 | /** @var RegistrationContext */ |
||
| 110 | private $context; |
||
| 111 | |||
| 112 | public function __construct(string $appId, RegistrationContext $context) { |
||
| 113 | $this->appId = $appId; |
||
| 114 | $this->context = $context; |
||
| 115 | } |
||
| 116 | |||
| 117 | public function registerCapability(string $capability): void { |
||
| 118 | $this->context->registerCapability( |
||
| 119 | $this->appId, |
||
| 120 | $capability |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function registerCrashReporter(string $reporterClass): void { |
||
| 125 | $this->context->registerCrashReporter( |
||
| 126 | $this->appId, |
||
| 127 | $reporterClass |
||
| 128 | ); |
||
| 129 | } |
||
| 130 | |||
| 131 | public function registerDashboardWidget(string $widgetClass): void { |
||
| 132 | $this->context->registerDashboardPanel( |
||
| 133 | $this->appId, |
||
| 134 | $widgetClass |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | |||
| 138 | public function registerService(string $name, callable $factory, bool $shared = true): void { |
||
| 139 | $this->context->registerService( |
||
| 140 | $this->appId, |
||
| 141 | $name, |
||
| 142 | $factory, |
||
| 143 | $shared |
||
| 144 | ); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function registerServiceAlias(string $alias, string $target): void { |
||
| 148 | $this->context->registerServiceAlias( |
||
| 149 | $this->appId, |
||
| 150 | $alias, |
||
| 151 | $target |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function registerParameter(string $name, $value): void { |
||
| 156 | $this->context->registerParameter( |
||
| 157 | $this->appId, |
||
| 158 | $name, |
||
| 159 | $value |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function registerEventListener(string $event, string $listener, int $priority = 0): void { |
||
| 164 | $this->context->registerEventListener( |
||
| 165 | $this->appId, |
||
| 166 | $event, |
||
| 167 | $listener, |
||
| 168 | $priority |
||
| 169 | ); |
||
| 170 | } |
||
| 171 | |||
| 172 | public function registerMiddleware(string $class): void { |
||
| 173 | $this->context->registerMiddleware( |
||
| 174 | $this->appId, |
||
| 175 | $class |
||
| 176 | ); |
||
| 177 | } |
||
| 178 | |||
| 179 | public function registerSearchProvider(string $class): void { |
||
| 180 | $this->context->registerSearchProvider( |
||
| 181 | $this->appId, |
||
| 182 | $class |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 186 | public function registerAlternativeLogin(string $class): void { |
||
| 187 | $this->context->registerAlternativeLogin( |
||
| 188 | $this->appId, |
||
| 189 | $class |
||
| 190 | ); |
||
| 191 | } |
||
| 192 | |||
| 193 | public function registerInitialStateProvider(string $class): void { |
||
| 194 | $this->context->registerInitialState( |
||
| 195 | $this->appId, |
||
| 196 | $class |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | |||
| 200 | public function registerWellKnownHandler(string $class): void { |
||
| 201 | $this->context->registerWellKnown( |
||
| 202 | $this->appId, |
||
| 203 | $class |
||
| 204 | ); |
||
| 205 | } |
||
| 206 | |||
| 207 | public function registerTemplateProvider(string $providerClass): void { |
||
| 208 | $this->context->registerTemplateProvider( |
||
| 209 | $this->appId, |
||
| 210 | $providerClass |
||
| 211 | ); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function registerNotifierService(string $notifierClass): void { |
||
| 215 | $this->context->registerNotifierService( |
||
| 216 | $this->appId, |
||
| 217 | $notifierClass |
||
| 218 | ); |
||
| 219 | } |
||
| 220 | }; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @psalm-param class-string<ICapability> $capability |
||
| 225 | */ |
||
| 226 | public function registerCapability(string $appId, string $capability): void { |
||
| 227 | $this->capabilities[] = new ServiceRegistration($appId, $capability); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @psalm-param class-string<IReporter> $capability |
||
| 232 | */ |
||
| 233 | public function registerCrashReporter(string $appId, string $reporterClass): void { |
||
| 234 | $this->crashReporters[] = new ServiceRegistration($appId, $reporterClass); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @psalm-param class-string<IWidget> $capability |
||
| 239 | */ |
||
| 240 | public function registerDashboardPanel(string $appId, string $panelClass): void { |
||
| 241 | $this->dashboardPanels[] = new ServiceRegistration($appId, $panelClass); |
||
| 242 | } |
||
| 243 | |||
| 244 | public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void { |
||
| 245 | $this->services[] = new ServiceFactoryRegistration($appId, $name, $factory, $shared); |
||
| 246 | } |
||
| 247 | |||
| 248 | public function registerServiceAlias(string $appId, string $alias, string $target): void { |
||
| 249 | $this->aliases[] = new ServiceAliasRegistration($appId, $alias, $target); |
||
| 250 | } |
||
| 251 | |||
| 252 | public function registerParameter(string $appId, string $name, $value): void { |
||
| 253 | $this->parameters[] = new ParameterRegistration($appId, $name, $value); |
||
| 254 | } |
||
| 255 | |||
| 256 | public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void { |
||
| 257 | $this->eventListeners[] = new EventListenerRegistration($appId, $event, $listener, $priority); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @psalm-param class-string<Middleware> $class |
||
| 262 | */ |
||
| 263 | public function registerMiddleware(string $appId, string $class): void { |
||
| 264 | $this->middlewares[] = new ServiceRegistration($appId, $class); |
||
| 265 | } |
||
| 266 | |||
| 267 | public function registerSearchProvider(string $appId, string $class) { |
||
| 268 | $this->searchProviders[] = new ServiceRegistration($appId, $class); |
||
| 269 | } |
||
| 270 | |||
| 271 | public function registerAlternativeLogin(string $appId, string $class): void { |
||
| 272 | $this->alternativeLogins[] = new ServiceRegistration($appId, $class); |
||
| 273 | } |
||
| 274 | |||
| 275 | public function registerInitialState(string $appId, string $class): void { |
||
| 276 | $this->initialStates[] = new ServiceRegistration($appId, $class); |
||
| 277 | } |
||
| 278 | |||
| 279 | public function registerWellKnown(string $appId, string $class): void { |
||
| 281 | } |
||
| 282 | |||
| 283 | public function registerTemplateProvider(string $appId, string $class): void { |
||
| 284 | $this->templateProviders[] = new ServiceRegistration($appId, $class); |
||
| 285 | } |
||
| 286 | |||
| 287 | public function registerNotifierService(string $appId, string $class): void { |
||
| 288 | $this->notifierServices[] = new ServiceRegistration($appId, $class); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param App[] $apps |
||
| 293 | */ |
||
| 294 | public function delegateCapabilityRegistrations(array $apps): void { |
||
| 295 | while (($registration = array_shift($this->capabilities)) !== null) { |
||
|
|
|||
| 296 | try { |
||
| 297 | $apps[$registration->getAppId()] |
||
| 298 | ->getContainer() |
||
| 299 | ->registerCapability($registration->getService()); |
||
| 300 | } catch (Throwable $e) { |
||
| 301 | $appId = $registration->getAppId(); |
||
| 302 | $this->logger->logException($e, [ |
||
| 303 | 'message' => "Error during capability registration of $appId: " . $e->getMessage(), |
||
| 304 | 'level' => ILogger::ERROR, |
||
| 305 | ]); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param App[] $apps |
||
| 312 | */ |
||
| 313 | public function delegateCrashReporterRegistrations(array $apps, Registry $registry): void { |
||
| 314 | while (($registration = array_shift($this->crashReporters)) !== null) { |
||
| 315 | try { |
||
| 316 | $registry->registerLazy($registration->getService()); |
||
| 317 | } catch (Throwable $e) { |
||
| 318 | $appId = $registration->getAppId(); |
||
| 319 | $this->logger->logException($e, [ |
||
| 320 | 'message' => "Error during crash reporter registration of $appId: " . $e->getMessage(), |
||
| 321 | 'level' => ILogger::ERROR, |
||
| 322 | ]); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param App[] $apps |
||
| 329 | */ |
||
| 330 | public function delegateDashboardPanelRegistrations(array $apps, IManager $dashboardManager): void { |
||
| 331 | while (($panel = array_shift($this->dashboardPanels)) !== null) { |
||
| 332 | try { |
||
| 333 | $dashboardManager->lazyRegisterWidget($panel->getService()); |
||
| 334 | } catch (Throwable $e) { |
||
| 335 | $appId = $panel->getAppId(); |
||
| 336 | $this->logger->logException($e, [ |
||
| 337 | 'message' => "Error during dashboard registration of $appId: " . $e->getMessage(), |
||
| 338 | 'level' => ILogger::ERROR, |
||
| 339 | ]); |
||
| 340 | } |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void { |
||
| 345 | while (($registration = array_shift($this->eventListeners)) !== null) { |
||
| 346 | try { |
||
| 347 | $eventDispatcher->addServiceListener( |
||
| 348 | $registration->getEvent(), |
||
| 349 | $registration->getService(), |
||
| 350 | $registration->getPriority() |
||
| 351 | ); |
||
| 352 | } catch (Throwable $e) { |
||
| 353 | $appId = $registration->getAppId(); |
||
| 354 | $this->logger->logException($e, [ |
||
| 355 | 'message' => "Error during event listener registration of $appId: " . $e->getMessage(), |
||
| 356 | 'level' => ILogger::ERROR, |
||
| 357 | ]); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param App[] $apps |
||
| 364 | */ |
||
| 365 | public function delegateContainerRegistrations(array $apps): void { |
||
| 366 | while (($registration = array_shift($this->services)) !== null) { |
||
| 367 | try { |
||
| 368 | /** |
||
| 369 | * Register the service and convert the callable into a \Closure if necessary |
||
| 370 | */ |
||
| 371 | $apps[$registration->getAppId()] |
||
| 372 | ->getContainer() |
||
| 373 | ->registerService( |
||
| 374 | $registration->getName(), |
||
| 375 | Closure::fromCallable($registration->getFactory()), |
||
| 376 | $registration->isShared() |
||
| 377 | ); |
||
| 378 | } catch (Throwable $e) { |
||
| 379 | $appId = $registration->getAppId(); |
||
| 380 | $this->logger->logException($e, [ |
||
| 381 | 'message' => "Error during service registration of $appId: " . $e->getMessage(), |
||
| 382 | 'level' => ILogger::ERROR, |
||
| 383 | ]); |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | while (($registration = array_shift($this->aliases)) !== null) { |
||
| 388 | try { |
||
| 389 | $apps[$registration->getAppId()] |
||
| 390 | ->getContainer() |
||
| 391 | ->registerAlias( |
||
| 392 | $registration->getAlias(), |
||
| 393 | $registration->getTarget() |
||
| 394 | ); |
||
| 395 | } catch (Throwable $e) { |
||
| 396 | $appId = $registration->getAppId(); |
||
| 397 | $this->logger->logException($e, [ |
||
| 398 | 'message' => "Error during service alias registration of $appId: " . $e->getMessage(), |
||
| 399 | 'level' => ILogger::ERROR, |
||
| 400 | ]); |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | while (($registration = array_shift($this->parameters)) !== null) { |
||
| 405 | try { |
||
| 406 | $apps[$registration->getAppId()] |
||
| 407 | ->getContainer() |
||
| 408 | ->registerParameter( |
||
| 409 | $registration->getName(), |
||
| 410 | $registration->getValue() |
||
| 411 | ); |
||
| 412 | } catch (Throwable $e) { |
||
| 413 | $appId = $registration->getAppId(); |
||
| 414 | $this->logger->logException($e, [ |
||
| 415 | 'message' => "Error during service alias registration of $appId: " . $e->getMessage(), |
||
| 416 | 'level' => ILogger::ERROR, |
||
| 417 | ]); |
||
| 418 | } |
||
| 419 | } |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @param App[] $apps |
||
| 424 | */ |
||
| 425 | public function delegateMiddlewareRegistrations(array $apps): void { |
||
| 426 | while (($middleware = array_shift($this->middlewares)) !== null) { |
||
| 427 | try { |
||
| 428 | $apps[$middleware->getAppId()] |
||
| 429 | ->getContainer() |
||
| 430 | ->registerMiddleWare($middleware->getService()); |
||
| 431 | } catch (Throwable $e) { |
||
| 432 | $appId = $middleware->getAppId(); |
||
| 433 | $this->logger->logException($e, [ |
||
| 434 | 'message' => "Error during capability registration of $appId: " . $e->getMessage(), |
||
| 435 | 'level' => ILogger::ERROR, |
||
| 436 | ]); |
||
| 437 | } |
||
| 438 | } |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @return ServiceRegistration<IProvider>[] |
||
| 443 | */ |
||
| 444 | public function getSearchProviders(): array { |
||
| 445 | return $this->searchProviders; |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @return ServiceRegistration<IAlternativeLogin>[] |
||
| 450 | */ |
||
| 451 | public function getAlternativeLogins(): array { |
||
| 452 | return $this->alternativeLogins; |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @return ServiceRegistration<InitialStateProvider>[] |
||
| 457 | */ |
||
| 458 | public function getInitialStates(): array { |
||
| 459 | return $this->initialStates; |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @return ServiceRegistration<IHandler>[] |
||
| 464 | */ |
||
| 465 | public function getWellKnownHandlers(): array { |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @return ServiceRegistration<ICustomTemplateProvider>[] |
||
| 471 | */ |
||
| 472 | public function getTemplateProviders(): array { |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @return ServiceRegistration<INotifier>[] |
||
| 478 | */ |
||
| 479 | public function getNotifierServices(): array { |
||
| 480 | return $this->notifierServices; |
||
| 481 | } |
||
| 482 | } |
||
| 483 |