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