| Total Complexity | 57 |
| Total Lines | 446 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Complex classes like ExtensionController 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 ExtensionController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 56 | class ExtensionController extends AbstractController |
||
| 57 | { |
||
| 58 | private const NEW_ROUTES_AVAIL = 'new.routes.avail'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @Route("/list/{pos}") |
||
| 62 | * @PermissionCheck("admin") |
||
| 63 | * @Theme("admin") |
||
| 64 | * @Template("@ZikulaExtensionsModule/Extension/list.html.twig") |
||
| 65 | */ |
||
| 66 | public function listAction( |
||
| 67 | Request $request, |
||
| 68 | EventDispatcherInterface $eventDispatcher, |
||
| 69 | ExtensionRepositoryInterface $extensionRepository, |
||
| 70 | BundleSyncHelper $bundleSyncHelper, |
||
| 71 | RouterInterface $router, |
||
| 72 | int $pos = 1 |
||
| 73 | ): array { |
||
| 74 | $modulesJustInstalled = $request->query->get('justinstalled'); |
||
| 75 | if (!empty($modulesJustInstalled)) { |
||
| 76 | // notify the event dispatcher that new routes are available (ids of modules just installed avail as args) |
||
| 77 | $event = new GenericEvent(null, json_decode($modulesJustInstalled)); |
||
| 78 | $eventDispatcher->dispatch($event, self::NEW_ROUTES_AVAIL); |
||
| 79 | } |
||
| 80 | |||
| 81 | $sortableColumns = new SortableColumns($router, 'zikulaextensionsmodule_extension_list'); |
||
| 82 | $sortableColumns->addColumns([new Column('displayname'), new Column('state')]); |
||
| 83 | $sortableColumns->setOrderByFromRequest($request); |
||
| 84 | |||
| 85 | $upgradedExtensions = []; |
||
| 86 | $vetoEvent = new GenericEvent(); |
||
| 87 | $eventDispatcher->dispatch($vetoEvent, ExtensionEvents::REGENERATE_VETO); |
||
| 88 | if (1 === $pos && !$vetoEvent->isPropagationStopped()) { |
||
| 89 | // regenerate the extension list only when viewing the first page |
||
| 90 | $extensionsInFileSystem = $bundleSyncHelper->scanForBundles(); |
||
| 91 | $upgradedExtensions = $bundleSyncHelper->syncExtensions($extensionsInFileSystem); |
||
| 92 | } |
||
| 93 | |||
| 94 | $pagedResult = $extensionRepository->getPagedCollectionBy([], [ |
||
| 95 | $sortableColumns->getSortColumn()->getName() => $sortableColumns->getSortDirection() |
||
| 96 | ], $this->getVar('itemsperpage'), $pos); |
||
|
|
|||
| 97 | |||
| 98 | return [ |
||
| 99 | 'sort' => $sortableColumns->generateSortableColumns(), |
||
| 100 | 'pager' => [ |
||
| 101 | 'limit' => $this->getVar('itemsperpage'), |
||
| 102 | 'count' => count($pagedResult) |
||
| 103 | ], |
||
| 104 | 'extensions' => $pagedResult, |
||
| 105 | 'upgradedExtensions' => $upgradedExtensions |
||
| 106 | ]; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @Route("/activate/{id}/{token}", methods = {"GET"}, requirements={"id" = "^[1-9]\d*$"}) |
||
| 111 | * @PermissionCheck("admin") |
||
| 112 | * |
||
| 113 | * Activate an extension. |
||
| 114 | * |
||
| 115 | * @throws AccessDeniedException Thrown if the CSRF token is invalid |
||
| 116 | */ |
||
| 117 | public function activateAction( |
||
| 118 | int $id, |
||
| 119 | string $token, |
||
| 120 | ExtensionRepositoryInterface $extensionRepository, |
||
| 121 | ExtensionStateHelper $extensionStateHelper, |
||
| 122 | CacheClearer $cacheClearer |
||
| 123 | ): RedirectResponse { |
||
| 124 | if (!$this->isCsrfTokenValid('activate-extension', $token)) { |
||
| 125 | throw new AccessDeniedException(); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** @var ExtensionEntity $extension */ |
||
| 129 | $extension = $extensionRepository->find($id); |
||
| 130 | if (Constant::STATE_NOTALLOWED === $extension->getState()) { |
||
| 131 | $this->addFlash('error', $this->trans('Error! Activation of %name% not allowed.', ['%name%' => $extension->getName()])); |
||
| 132 | } else { |
||
| 133 | // Update state |
||
| 134 | $extensionStateHelper->updateState($id, Constant::STATE_ACTIVE); |
||
| 135 | $cacheClearer->clear('symfony'); |
||
| 136 | $this->addFlash('status', $this->trans('Done! Activated %name%.', ['%name%' => $extension->getName()])); |
||
| 137 | } |
||
| 138 | |||
| 139 | return $this->redirectToRoute('zikulaextensionsmodule_extension_list'); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @Route("/deactivate/{id}/{token}", methods = {"GET"}, requirements={"id" = "^[1-9]\d*$"}) |
||
| 144 | * @PermissionCheck("admin") |
||
| 145 | * |
||
| 146 | * Deactivate an extension |
||
| 147 | * |
||
| 148 | * @throws AccessDeniedException Thrown if the CSRF token is invalid |
||
| 149 | */ |
||
| 150 | public function deactivateAction( |
||
| 151 | int $id, |
||
| 152 | string $token, |
||
| 153 | ExtensionRepositoryInterface $extensionRepository, |
||
| 154 | ExtensionStateHelper $extensionStateHelper, |
||
| 155 | CacheClearer $cacheClearer |
||
| 156 | ): RedirectResponse { |
||
| 157 | if (!$this->isCsrfTokenValid('deactivate-extension', $token)) { |
||
| 158 | throw new AccessDeniedException(); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** @var ExtensionEntity $extension */ |
||
| 162 | $extension = $extensionRepository->find($id); |
||
| 163 | if (null !== $extension) { |
||
| 164 | if (ZikulaKernel::isCoreExtension($extension->getName())) { |
||
| 165 | $this->addFlash('error', $this->trans('Error! You cannot deactivate the %name%. It is required by the system.', ['%name%' => $extension->getName()])); |
||
| 166 | } else { |
||
| 167 | // Update state |
||
| 168 | $extensionStateHelper->updateState($id, Constant::STATE_INACTIVE); |
||
| 169 | $cacheClearer->clear('symfony'); |
||
| 170 | $this->addFlash('status', $this->trans('Done! Deactivated %name%.', ['%name%' => $extension->getName()])); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | return $this->redirectToRoute('zikulaextensionsmodule_extension_list'); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @Route("/modify/{id}/{forceDefaults}", requirements={"id" = "^[1-9]\d*$", "forceDefaults" = "0|1"}) |
||
| 179 | * @Theme("admin") |
||
| 180 | * @Template("@ZikulaExtensionsModule/Extension/modify.html.twig") |
||
| 181 | * |
||
| 182 | * Modify a module. |
||
| 183 | * |
||
| 184 | * @return array|RedirectResponse |
||
| 185 | * @throws AccessDeniedException Thrown if the user doesn't have admin permissions for modifying the extension |
||
| 186 | */ |
||
| 187 | public function modifyAction( |
||
| 188 | Request $request, |
||
| 189 | ZikulaHttpKernelInterface $kernel, |
||
| 190 | ExtensionEntity $extension, |
||
| 191 | CacheClearer $cacheClearer, |
||
| 192 | bool $forceDefaults = false |
||
| 193 | ) { |
||
| 194 | if (!$this->hasPermission('ZikulaExtensionsModule::modify', $extension->getName() . '::' . $extension->getId(), ACCESS_ADMIN)) { |
||
| 195 | throw new AccessDeniedException(); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** @var AbstractExtension $extensionBundle */ |
||
| 199 | $extensionBundle = $kernel->getBundle($extension->getName()); |
||
| 200 | $metaData = $extensionBundle->getMetaData()->getFilteredVersionInfoArray(); |
||
| 201 | |||
| 202 | if ($forceDefaults) { |
||
| 203 | $extension->setName($metaData['name']); |
||
| 204 | $extension->setUrl($metaData['url']); |
||
| 205 | $extension->setDescription($metaData['description']); |
||
| 206 | } |
||
| 207 | |||
| 208 | $form = $this->createForm(ExtensionModifyType::class, $extension); |
||
| 209 | $form->handleRequest($request); |
||
| 210 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 211 | if ($form->get('defaults')->isClicked()) { |
||
| 212 | $this->addFlash('info', 'Default values reloaded. Save to confirm.'); |
||
| 213 | |||
| 214 | return $this->redirectToRoute('zikulaextensionsmodule_extension_modify', ['id' => $extension->getId(), 'forceDefaults' => 1]); |
||
| 215 | } |
||
| 216 | if ($form->get('save')->isClicked()) { |
||
| 217 | $em = $this->getDoctrine()->getManager(); |
||
| 218 | $em->persist($extension); |
||
| 219 | $em->flush(); |
||
| 220 | |||
| 221 | $cacheClearer->clear('symfony'); |
||
| 222 | $this->addFlash('status', 'Done! Extension updated.'); |
||
| 223 | } elseif ($form->get('cancel')->isClicked()) { |
||
| 224 | $this->addFlash('status', 'Operation cancelled.'); |
||
| 225 | } |
||
| 226 | |||
| 227 | return $this->redirectToRoute('zikulaextensionsmodule_extension_list'); |
||
| 228 | } |
||
| 229 | |||
| 230 | return [ |
||
| 231 | 'form' => $form->createView() |
||
| 232 | ]; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @Route("/compatibility/{id}", methods = {"GET"}, requirements={"id" = "^[1-9]\d*$"}) |
||
| 237 | * @Theme("admin") |
||
| 238 | * @Template("@ZikulaExtensionsModule/Extension/compatibility.html.twig") |
||
| 239 | * |
||
| 240 | * Display information of a module compatibility with the version of the core |
||
| 241 | * |
||
| 242 | * @throws AccessDeniedException Thrown if the user doesn't have admin permission to the requested module |
||
| 243 | */ |
||
| 244 | public function compatibilityAction(ExtensionEntity $extension): array |
||
| 245 | { |
||
| 246 | if (!$this->hasPermission('ZikulaExtensionsModule::', $extension->getName() . '::' . $extension->getId(), ACCESS_ADMIN)) { |
||
| 247 | throw new AccessDeniedException(); |
||
| 248 | } |
||
| 249 | |||
| 250 | return [ |
||
| 251 | 'extension' => $extension |
||
| 252 | ]; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @Route("/install/{id}/{token}", requirements={"id" = "^[1-9]\d*$"}) |
||
| 257 | * @PermissionCheck("admin") |
||
| 258 | * @Theme("admin") |
||
| 259 | * @Template("@ZikulaExtensionsModule/Extension/install.html.twig") |
||
| 260 | * |
||
| 261 | * Install and initialise an extension. |
||
| 262 | * |
||
| 263 | * @return array|RedirectResponse |
||
| 264 | * @throws AccessDeniedException Thrown if the CSRF token is invalid |
||
| 265 | */ |
||
| 266 | public function installAction( |
||
| 337 | ]; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Post-installation action to trigger the MODULE_POSTINSTALL event. |
||
| 342 | * The additional Action is required because this event must occur AFTER the rebuild of the cache which occurs on Request. |
||
| 343 | * |
||
| 344 | * @Route("/postinstall/{extensions}", methods = {"GET"}) |
||
| 345 | */ |
||
| 346 | public function postInstallAction( |
||
| 347 | ExtensionRepositoryInterface $extensionRepository, |
||
| 348 | ZikulaHttpKernelInterface $kernel, |
||
| 349 | EventDispatcherInterface $eventDispatcher, |
||
| 350 | string $extensions = null |
||
| 351 | ): RedirectResponse { |
||
| 352 | if (!empty($extensions)) { |
||
| 353 | $extensions = json_decode($extensions); |
||
| 354 | foreach ($extensions as $extensionId) { |
||
| 355 | /** @var ExtensionEntity $extensionEntity */ |
||
| 356 | $extensionEntity = $extensionRepository->find($extensionId); |
||
| 357 | if (null === $extensionRepository) { |
||
| 358 | continue; |
||
| 359 | } |
||
| 360 | /** @var AbstractExtension $extensionBundle */ |
||
| 361 | $extensionBundle = $kernel->getBundle($extensionEntity->getName()); |
||
| 362 | if (null === $extensionBundle) { |
||
| 363 | continue; |
||
| 364 | } |
||
| 365 | $event = new ExtensionStateEvent($extensionBundle, $extensionEntity->toArray()); |
||
| 366 | $eventDispatcher->dispatch($event, ExtensionEvents::EXTENSION_POSTINSTALL); |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | return $this->redirectToRoute('zikulaextensionsmodule_extension_list', ['justinstalled' => json_encode($extensions)]); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Create array suitable for checkbox FormType [[ID => bool][ID => bool]]. |
||
| 375 | */ |
||
| 376 | private function formatDependencyCheckboxArray( |
||
| 377 | ExtensionRepositoryInterface $extensionRepository, |
||
| 378 | array $dependencies |
||
| 379 | ): array { |
||
| 380 | $return = []; |
||
| 381 | foreach ($dependencies as $dependency) { |
||
| 382 | /** @var ExtensionEntity $dependencyExtension */ |
||
| 383 | $dependencyExtension = $extensionRepository->get($dependency->getModname()); |
||
| 384 | $return[$dependency->getId()] = null !== $dependencyExtension; |
||
| 385 | } |
||
| 386 | |||
| 387 | return $return; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @Route("/upgrade/{id}/{token}", requirements={"id" = "^[1-9]\d*$"}) |
||
| 392 | * @PermissionCheck("admin") |
||
| 393 | * |
||
| 394 | * Upgrade an extension. |
||
| 395 | * |
||
| 396 | * @throws AccessDeniedException Thrown if the CSRF token is invalid |
||
| 397 | */ |
||
| 398 | public function upgradeAction( |
||
| 399 | ExtensionEntity $extension, |
||
| 400 | $token, |
||
| 401 | ExtensionHelper $extensionHelper |
||
| 402 | ): RedirectResponse { |
||
| 403 | if (!$this->isCsrfTokenValid('upgrade-extension', $token)) { |
||
| 404 | throw new AccessDeniedException(); |
||
| 405 | } |
||
| 406 | |||
| 407 | $result = $extensionHelper->upgrade($extension); |
||
| 408 | if ($result) { |
||
| 409 | $this->addFlash('status', $this->trans('%name% upgraded to new version and activated.', ['%name%' => $extension->getDisplayname()])); |
||
| 410 | } else { |
||
| 411 | $this->addFlash('error', 'Extension upgrade failed!'); |
||
| 412 | } |
||
| 413 | |||
| 414 | return $this->redirectToRoute('zikulaextensionsmodule_extension_list'); |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @Route("/uninstall/{id}/{token}", requirements={"id" = "^[1-9]\d*$"}) |
||
| 419 | * @PermissionCheck("admin") |
||
| 420 | * @Theme("admin") |
||
| 421 | * @Template("@ZikulaExtensionsModule/Extension/uninstall.html.twig") |
||
| 422 | * |
||
| 423 | * Uninstall an extension. |
||
| 424 | * |
||
| 425 | * @return array|Response|RedirectResponse |
||
| 426 | * @throws AccessDeniedException Thrown if the CSRF token is invalid |
||
| 427 | */ |
||
| 428 | public function uninstallAction( |
||
| 489 | ]; |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @Route("/theme-preview/{themeName}") |
||
| 494 | * @PermissionCheck("admin") |
||
| 495 | */ |
||
| 496 | public function previewAction(Engine $engine, string $themeName): Response |
||
| 502 | } |
||
| 503 | } |
||
| 504 |