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