| Total Complexity | 61 |
| Total Lines | 481 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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( |
||
| 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 |
||
| 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( |
||
| 360 | ]; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Post-installation action to trigger the MODULE_POSTINSTALL event. |
||
| 365 | * The additional Action is required because this event must occur AFTER the rebuild of the cache which occurs on Request. |
||
| 366 | * |
||
| 367 | * @Route("/postinstall/{extensions}", methods = {"GET"}) |
||
| 368 | */ |
||
| 369 | public function postInstallAction( |
||
| 370 | ExtensionRepositoryInterface $extensionRepository, |
||
| 371 | ZikulaHttpKernelInterface $kernel, |
||
| 372 | EventDispatcherInterface $eventDispatcher, |
||
| 373 | string $extensions = null |
||
| 374 | ): RedirectResponse { |
||
| 375 | if (!empty($extensions)) { |
||
| 376 | $extensions = json_decode($extensions); |
||
| 377 | foreach ($extensions as $extensionId) { |
||
| 378 | /** @var ExtensionEntity $extensionEntity */ |
||
| 379 | $extensionEntity = $extensionRepository->find($extensionId); |
||
| 380 | if (null === $extensionRepository) { |
||
| 381 | continue; |
||
| 382 | } |
||
| 383 | /** @var AbstractExtension $extensionBundle */ |
||
| 384 | $extensionBundle = $kernel->getBundle($extensionEntity->getName()); |
||
| 385 | if (null === $extensionBundle) { |
||
| 386 | continue; |
||
| 387 | } |
||
| 388 | $eventDispatcher->dispatch(new ExtensionPostCacheRebuildEvent($extensionBundle, $extensionEntity)); |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | return $this->redirectToRoute('zikulaextensionsmodule_extension_list', ['justinstalled' => json_encode($extensions)]); |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Create array suitable for checkbox FormType [[ID => bool][ID => bool]]. |
||
| 397 | */ |
||
| 398 | private function formatDependencyCheckboxArray( |
||
| 399 | ExtensionRepositoryInterface $extensionRepository, |
||
| 400 | array $dependencies |
||
| 401 | ): array { |
||
| 402 | $return = []; |
||
| 403 | foreach ($dependencies as $dependency) { |
||
| 404 | /** @var ExtensionEntity $dependencyExtension */ |
||
| 405 | $dependencyExtension = $extensionRepository->get($dependency->getModname()); |
||
| 406 | $return[$dependency->getId()] = null !== $dependencyExtension; |
||
| 407 | } |
||
| 408 | |||
| 409 | return $return; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @Route("/upgrade/{id}/{token}", requirements={"id" = "^[1-9]\d*$"}) |
||
| 414 | * @PermissionCheck("admin") |
||
| 415 | * |
||
| 416 | * Upgrade an extension. |
||
| 417 | * |
||
| 418 | * @throws AccessDeniedException Thrown if the CSRF token is invalid |
||
| 419 | */ |
||
| 420 | public function upgradeAction( |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @Route("/uninstall/{id}/{token}", requirements={"id" = "^[1-9]\d*$"}) |
||
| 441 | * @PermissionCheck("admin") |
||
| 442 | * @Theme("admin") |
||
| 443 | * @Template("@ZikulaExtensionsModule/Extension/uninstall.html.twig") |
||
| 444 | * |
||
| 445 | * Uninstall an extension. |
||
| 446 | * |
||
| 447 | * @return array|Response|RedirectResponse |
||
| 448 | * @throws AccessDeniedException Thrown if the CSRF token is invalid |
||
| 449 | */ |
||
| 450 | public function uninstallAction( |
||
| 511 | ]; |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @Route("/post-uninstall") |
||
| 516 | * @PermissionCheck("admin") |
||
| 517 | * |
||
| 518 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 519 | */ |
||
| 520 | public function postUninstallAction(CacheClearer $cacheClearer) |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @Route("/theme-preview/{themeName}") |
||
| 529 | * @PermissionCheck("admin") |
||
| 530 | */ |
||
| 531 | public function previewAction(Engine $engine, string $themeName): Response |
||
| 537 | } |
||
| 538 | } |
||
| 539 |