| Total Complexity | 70 |
| Total Lines | 471 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ModuleController 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 ModuleController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | class ModuleController extends AbstractController |
||
| 56 | { |
||
| 57 | private const NEW_ROUTES_AVAIL = 'new.routes.avail'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @Route("/list/{pos}") |
||
| 61 | * @Theme("admin") |
||
| 62 | * @Template("@ZikulaExtensionsModule/Module/viewModuleList.html.twig") |
||
| 63 | * |
||
| 64 | * @throws AccessDeniedException Thrown if the user doesn't have admin permissions for the module |
||
| 65 | */ |
||
| 66 | public function viewModuleListAction( |
||
| 67 | Request $request, |
||
| 68 | ExtensionRepositoryInterface $extensionRepository, |
||
| 69 | BundleSyncHelper $bundleSyncHelper, |
||
| 70 | RouterInterface $router, |
||
| 71 | int $pos = 1 |
||
| 72 | ): array { |
||
| 73 | if (!$this->hasPermission('ZikulaExtensionsModule::', '::', ACCESS_ADMIN)) { |
||
| 74 | throw new AccessDeniedException(); |
||
| 75 | } |
||
| 76 | $modulesJustInstalled = $request->query->get('justinstalled'); |
||
| 77 | if (!empty($modulesJustInstalled)) { |
||
| 78 | // notify the event dispatcher that new routes are available (ids of modules just installed avail as args) |
||
| 79 | $event = new GenericEvent(null, json_decode($modulesJustInstalled)); |
||
| 80 | $this->get('event_dispatcher')->dispatch($event, self::NEW_ROUTES_AVAIL); |
||
| 81 | } |
||
| 82 | |||
| 83 | $sortableColumns = new SortableColumns($router, 'zikulaextensionsmodule_module_viewmodulelist'); |
||
| 84 | $sortableColumns->addColumns([new Column('displayname'), new Column('state')]); |
||
| 85 | $sortableColumns->setOrderByFromRequest($request); |
||
| 86 | |||
| 87 | $upgradedExtensions = []; |
||
| 88 | $vetoEvent = new GenericEvent(); |
||
| 89 | $this->get('event_dispatcher')->dispatch($vetoEvent, ExtensionEvents::REGENERATE_VETO); |
||
| 90 | if (1 === $pos && !$vetoEvent->isPropagationStopped()) { |
||
|
|
|||
| 91 | // regenerate the extension list only when viewing the first page |
||
| 92 | $extensionsInFileSystem = $bundleSyncHelper->scanForBundles(); |
||
| 93 | $upgradedExtensions = $bundleSyncHelper->syncExtensions($extensionsInFileSystem); |
||
| 94 | } |
||
| 95 | |||
| 96 | $pagedResult = $extensionRepository->getPagedCollectionBy([], [ |
||
| 97 | $sortableColumns->getSortColumn()->getName() => $sortableColumns->getSortDirection() |
||
| 98 | ], $this->getVar('itemsperpage'), $pos); |
||
| 99 | |||
| 100 | $adminRoutes = []; |
||
| 101 | |||
| 102 | foreach ($pagedResult as $module) { |
||
| 103 | if (Constant::STATE_ACTIVE !== $module['state'] || !isset($module['capabilities']['admin']) || empty($module['capabilities']['admin'])) { |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | |||
| 107 | $adminCapabilityInfo = $module['capabilities']['admin']; |
||
| 108 | $adminUrl = ''; |
||
| 109 | if (isset($adminCapabilityInfo['route'])) { |
||
| 110 | try { |
||
| 111 | $adminUrl = $router->generate($adminCapabilityInfo['route']); |
||
| 112 | } catch (RouteNotFoundException $routeNotFoundException) { |
||
| 113 | // do nothing, just skip this link |
||
| 114 | } |
||
| 115 | } elseif (isset($adminCapabilityInfo['url'])) { |
||
| 116 | $adminUrl = $adminCapabilityInfo['url']; |
||
| 117 | } |
||
| 118 | |||
| 119 | if (!empty($adminUrl)) { |
||
| 120 | $adminRoutes[$module['name']] = $adminUrl; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | return [ |
||
| 125 | 'sort' => $sortableColumns->generateSortableColumns(), |
||
| 126 | 'pager' => [ |
||
| 127 | 'limit' => $this->getVar('itemsperpage'), |
||
| 128 | 'count' => count($pagedResult) |
||
| 129 | ], |
||
| 130 | 'modules' => $pagedResult, |
||
| 131 | 'adminRoutes' => $adminRoutes, |
||
| 132 | 'upgradedExtensions' => $upgradedExtensions |
||
| 133 | ]; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @Route("/modules/activate/{id}/{token}", methods = {"GET"}, requirements={"id" = "^[1-9]\d*$"}) |
||
| 138 | * |
||
| 139 | * Activate an extension. |
||
| 140 | * |
||
| 141 | * @throws AccessDeniedException Thrown if the user doesn't have admin permissions for the module |
||
| 142 | */ |
||
| 143 | public function activateAction( |
||
| 144 | int $id, |
||
| 145 | string $token, |
||
| 146 | ExtensionRepositoryInterface $extensionRepository, |
||
| 147 | ExtensionStateHelper $extensionStateHelper, |
||
| 148 | CacheClearer $cacheClearer |
||
| 149 | ): RedirectResponse { |
||
| 150 | if (!$this->hasPermission('ZikulaExtensionsModule::', '::', ACCESS_ADMIN)) { |
||
| 151 | throw new AccessDeniedException(); |
||
| 152 | } |
||
| 153 | |||
| 154 | if (!$this->isCsrfTokenValid('activate-extension', $token)) { |
||
| 155 | throw new AccessDeniedException(); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** @var ExtensionEntity $extension */ |
||
| 159 | $extension = $extensionRepository->find($id); |
||
| 160 | if (Constant::STATE_NOTALLOWED === $extension->getState()) { |
||
| 161 | $this->addFlash('error', $this->__f('Error! Activation of module %s not allowed.', ['%s' => $extension->getName()])); |
||
| 162 | } else { |
||
| 163 | // Update state |
||
| 164 | $extensionStateHelper->updateState($id, Constant::STATE_ACTIVE); |
||
| 165 | $cacheClearer->clear('symfony.routing'); |
||
| 166 | $this->addFlash('status', $this->__f('Done! Activated %s module.', ['%s' => $extension->getName()])); |
||
| 167 | } |
||
| 168 | |||
| 169 | return $this->redirectToRoute('zikulaextensionsmodule_module_viewmodulelist'); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @Route("/modules/deactivate/{id}/{token}", methods = {"GET"}, requirements={"id" = "^[1-9]\d*$"}) |
||
| 174 | * |
||
| 175 | * Deactivate an extension |
||
| 176 | * |
||
| 177 | * @throws AccessDeniedException Thrown if the user doesn't have admin permissions for the module |
||
| 178 | */ |
||
| 179 | public function deactivateAction( |
||
| 180 | int $id, |
||
| 181 | string $token, |
||
| 182 | ExtensionRepositoryInterface $extensionRepository, |
||
| 183 | ExtensionStateHelper $extensionStateHelper, |
||
| 184 | CacheClearer $cacheClearer |
||
| 185 | ): RedirectResponse { |
||
| 186 | if (!$this->hasPermission('ZikulaExtensionsModule::', '::', ACCESS_ADMIN)) { |
||
| 187 | throw new AccessDeniedException(); |
||
| 188 | } |
||
| 189 | |||
| 190 | if (!$this->isCsrfTokenValid('deactivate-extension', $token)) { |
||
| 191 | throw new AccessDeniedException(); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** @var ExtensionEntity $extension */ |
||
| 195 | $extension = $extensionRepository->find($id); |
||
| 196 | if (null !== $extension) { |
||
| 197 | if (ZikulaKernel::isCoreModule($extension->getName())) { |
||
| 198 | $this->addFlash('error', $this->__f('Error! You cannot deactivate this extension [%s]. It is a mandatory core extension, and is required by the system.', ['%s' => $extension->getName()])); |
||
| 199 | } else { |
||
| 200 | // Update state |
||
| 201 | $extensionStateHelper->updateState($id, Constant::STATE_INACTIVE); |
||
| 202 | $cacheClearer->clear('symfony.routing'); |
||
| 203 | $this->addFlash('status', $this->__('Done! Deactivated module.')); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | return $this->redirectToRoute('zikulaextensionsmodule_module_viewmodulelist'); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @Route("/modify/{id}/{forceDefaults}", requirements={"id" = "^[1-9]\d*$", "forceDefaults" = "0|1"}) |
||
| 212 | * @Theme("admin") |
||
| 213 | * @Template("@ZikulaExtensionsModule/Module/modify.html.twig") |
||
| 214 | * |
||
| 215 | * Modify a module. |
||
| 216 | * |
||
| 217 | * @return array|RedirectResponse |
||
| 218 | * @throws AccessDeniedException Thrown if the user doesn't have admin permissions for modifying the extension |
||
| 219 | */ |
||
| 220 | public function modifyAction( |
||
| 221 | Request $request, |
||
| 222 | ExtensionEntity $extension, |
||
| 223 | CacheClearer $cacheClearer, |
||
| 224 | bool $forceDefaults = false |
||
| 225 | ) { |
||
| 226 | if (!$this->hasPermission('ZikulaExtensionsModule::modify', $extension->getName() . '::' . $extension->getId(), ACCESS_ADMIN)) { |
||
| 227 | throw new AccessDeniedException(); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** @var AbstractBundle $bundle */ |
||
| 231 | $bundle = $this->get('kernel')->getModule($extension->getName()); |
||
| 232 | $metaData = $bundle->getMetaData()->getFilteredVersionInfoArray(); |
||
| 233 | |||
| 234 | if ($forceDefaults) { |
||
| 235 | $extension->setName($metaData['name']); |
||
| 236 | $extension->setUrl($metaData['url']); |
||
| 237 | $extension->setDescription($metaData['description']); |
||
| 238 | } |
||
| 239 | |||
| 240 | $form = $this->createForm(ExtensionModifyType::class, $extension); |
||
| 241 | $form->handleRequest($request); |
||
| 242 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 243 | if ($form->get('defaults')->isClicked()) { |
||
| 244 | $this->addFlash('info', $this->__('Default values reloaded. Save to confirm.')); |
||
| 245 | |||
| 246 | return $this->redirectToRoute('zikulaextensionsmodule_module_modify', ['id' => $extension->getId(), 'forceDefaults' => 1]); |
||
| 247 | } |
||
| 248 | if ($form->get('save')->isClicked()) { |
||
| 249 | $em = $this->getDoctrine()->getManager(); |
||
| 250 | $em->persist($extension); |
||
| 251 | $em->flush(); |
||
| 252 | |||
| 253 | $cacheClearer->clear('symfony.routing'); |
||
| 254 | $this->addFlash('status', $this->__('Done! Extension updated.')); |
||
| 255 | } |
||
| 256 | if ($form->get('cancel')->isClicked()) { |
||
| 257 | $this->addFlash('status', $this->__('Operation cancelled.')); |
||
| 258 | } |
||
| 259 | |||
| 260 | return $this->redirectToRoute('zikulaextensionsmodule_module_viewmodulelist'); |
||
| 261 | } |
||
| 262 | |||
| 263 | return [ |
||
| 264 | 'form' => $form->createView() |
||
| 265 | ]; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @Route("/compatibility/{id}", methods = {"GET"}, requirements={"id" = "^[1-9]\d*$"}) |
||
| 270 | * @Theme("admin") |
||
| 271 | * @Template("@ZikulaExtensionsModule/Module/compatibility.html.twig") |
||
| 272 | * |
||
| 273 | * Display information of a module compatibility with the version of the core |
||
| 274 | * |
||
| 275 | * @throws AccessDeniedException Thrown if the user doesn't have admin permission to the requested module |
||
| 276 | */ |
||
| 277 | public function compatibilityAction(ExtensionEntity $extension): array |
||
| 285 | ]; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @Route("/install/{id}/{token}", requirements={"id" = "^[1-9]\d*$"}) |
||
| 290 | * @Theme("admin") |
||
| 291 | * @Template("@ZikulaExtensionsModule/Module/install.html.twig") |
||
| 292 | * |
||
| 293 | * Install and initialise an extension. |
||
| 294 | * |
||
| 295 | * @return array|RedirectResponse |
||
| 296 | * @throws AccessDeniedException Thrown if the user doesn't have admin permission for the module |
||
| 297 | */ |
||
| 298 | public function installAction( |
||
| 299 | Request $request, |
||
| 300 | ExtensionEntity $extension, |
||
| 301 | string $token, |
||
| 302 | ExtensionRepositoryInterface $extensionRepository, |
||
| 303 | ExtensionHelper $extensionHelper, |
||
| 304 | ExtensionStateHelper $extensionStateHelper, |
||
| 305 | ExtensionDependencyHelper $dependencyHelper, |
||
| 306 | CacheClearer $cacheClearer |
||
| 307 | ) { |
||
| 308 | if (!$this->hasPermission('ZikulaExtensionsModule::', '::', ACCESS_ADMIN)) { |
||
| 309 | throw new AccessDeniedException(); |
||
| 310 | } |
||
| 311 | |||
| 312 | $id = $extension->getId(); |
||
| 313 | if (!$this->isCsrfTokenValid('install-extension', $token)) { |
||
| 314 | throw new AccessDeniedException(); |
||
| 315 | } |
||
| 316 | |||
| 317 | if (!$this->get('kernel')->isBundle($extension->getName())) { |
||
| 318 | $extensionStateHelper->updateState($id, Constant::STATE_TRANSITIONAL); |
||
| 319 | $cacheClearer->clear('symfony'); |
||
| 320 | |||
| 321 | return $this->redirectToRoute('zikulaextensionsmodule_module_install', ['id' => $id]); |
||
| 322 | } |
||
| 323 | $unsatisfiedDependencies = $dependencyHelper->getUnsatisfiedExtensionDependencies($extension); |
||
| 324 | $form = $this->createForm(ExtensionInstallType::class, [ |
||
| 325 | 'dependencies' => $this->formatDependencyCheckboxArray($extensionRepository, $unsatisfiedDependencies) |
||
| 326 | ]); |
||
| 327 | $hasNoUnsatisfiedDependencies = empty($unsatisfiedDependencies); |
||
| 328 | $form->handleRequest($request); |
||
| 329 | if ($hasNoUnsatisfiedDependencies || ($form->isSubmitted() && $form->isValid())) { |
||
| 330 | if ($hasNoUnsatisfiedDependencies || $form->get('install')->isClicked()) { |
||
| 331 | $extensionsInstalled = []; |
||
| 332 | $data = $form->getData(); |
||
| 333 | foreach ($data['dependencies'] as $dependencyId => $installSelected) { |
||
| 334 | if (!$installSelected && MetaData::DEPENDENCY_REQUIRED !== $unsatisfiedDependencies[$dependencyId]->getStatus()) { |
||
| 335 | continue; |
||
| 336 | } |
||
| 337 | $dependencyExtensionEntity = $extensionRepository->get($unsatisfiedDependencies[$dependencyId]->getModname()); |
||
| 338 | if (isset($dependencyExtensionEntity)) { |
||
| 339 | if (!$extensionHelper->install($dependencyExtensionEntity)) { |
||
| 340 | $this->addFlash('error', $this->__f('Failed to install dependency %s!', ['%s' => $dependencyExtensionEntity->getName()])); |
||
| 341 | |||
| 342 | return $this->redirectToRoute('zikulaextensionsmodule_module_viewmodulelist'); |
||
| 343 | } |
||
| 344 | $extensionsInstalled[] = $dependencyExtensionEntity->getId(); |
||
| 345 | $this->addFlash('status', $this->__f('Installed dependency %s.', ['%s' => $dependencyExtensionEntity->getName()])); |
||
| 346 | } else { |
||
| 347 | $this->addFlash('warning', $this->__f('Warning: could not install selected dependency %s', ['%s' => $unsatisfiedDependencies[$dependencyId]->getModname()])); |
||
| 348 | } |
||
| 349 | } |
||
| 350 | if ($extensionHelper->install($extension)) { |
||
| 351 | $this->addFlash('status', $this->__f('Done! Installed %s.', ['%s' => $extension->getName()])); |
||
| 352 | $extensionsInstalled[] = $id; |
||
| 353 | $cacheClearer->clear('symfony'); |
||
| 354 | |||
| 355 | return $this->redirectToRoute('zikulaextensionsmodule_module_postinstall', ['extensions' => json_encode($extensionsInstalled)]); |
||
| 356 | } |
||
| 357 | $extensionStateHelper->updateState($id, Constant::STATE_UNINITIALISED); |
||
| 358 | $this->addFlash('error', $this->__f('Initialization of %s failed!', ['%s' => $extension->getName()])); |
||
| 359 | } |
||
| 360 | if ($form->get('cancel')->isClicked()) { |
||
| 361 | $extensionStateHelper->updateState($id, Constant::STATE_UNINITIALISED); |
||
| 362 | $this->addFlash('status', $this->__('Operation cancelled.')); |
||
| 363 | } |
||
| 364 | |||
| 365 | return $this->redirectToRoute('zikulaextensionsmodule_module_viewmodulelist'); |
||
| 366 | } |
||
| 367 | |||
| 368 | return [ |
||
| 369 | 'dependencies' => $unsatisfiedDependencies, |
||
| 370 | 'extension' => $extension, |
||
| 371 | 'form' => $form->createView() |
||
| 372 | ]; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Post-installation action to trigger the MODULE_POSTINSTALL event. |
||
| 377 | * The additional Action is required because this event must occur AFTER the rebuild of the cache which occurs on Request. |
||
| 378 | * |
||
| 379 | * @Route("/postinstall/{extensions}", methods = {"GET"}) |
||
| 380 | */ |
||
| 381 | public function postInstallAction( |
||
| 382 | ExtensionRepositoryInterface $extensionRepository, |
||
| 383 | ZikulaHttpKernelInterface $kernel, |
||
| 384 | EventDispatcherInterface $eventDispatcher, |
||
| 385 | string $extensions = null |
||
| 386 | ): RedirectResponse { |
||
| 387 | if (!empty($extensions)) { |
||
| 388 | $extensions = json_decode($extensions); |
||
| 389 | foreach ($extensions as $extensionId) { |
||
| 390 | /** @var ExtensionEntity $extensionEntity */ |
||
| 391 | $extensionEntity = $extensionRepository->find($extensionId); |
||
| 392 | if (null === $extensionRepository) { |
||
| 393 | continue; |
||
| 394 | } |
||
| 395 | $bundle = $kernel->getModule($extensionEntity->getName()); |
||
| 396 | if (null === $bundle) { |
||
| 397 | continue; |
||
| 398 | } |
||
| 399 | $event = new ModuleStateEvent($bundle, $extensionEntity->toArray()); |
||
| 400 | $eventDispatcher->dispatch($event, CoreEvents::MODULE_POSTINSTALL); |
||
| 401 | } |
||
| 402 | // currently commented out because it takes a long time. |
||
| 403 | //$extensionHelper->installAssets(); |
||
| 404 | } |
||
| 405 | |||
| 406 | return $this->redirectToRoute('zikulaextensionsmodule_module_viewmodulelist', ['justinstalled' => json_encode($extensions)]); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Create array suitable for checkbox FormType [[ID => bool][ID => bool]]. |
||
| 411 | */ |
||
| 412 | private function formatDependencyCheckboxArray( |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @Route("/upgrade/{id}/{token}", requirements={"id" = "^[1-9]\d*$"}) |
||
| 428 | * |
||
| 429 | * Upgrade an extension. |
||
| 430 | * |
||
| 431 | * @throws AccessDeniedException Thrown if the user doesn't have admin permission for the module |
||
| 432 | */ |
||
| 433 | public function upgradeAction( |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @Route("/uninstall/{id}/{token}", requirements={"id" = "^[1-9]\d*$"}) |
||
| 458 | * @Theme("admin") |
||
| 459 | * @Template("@ZikulaExtensionsModule/Module/uninstall.html.twig") |
||
| 460 | * |
||
| 461 | * Uninstall an extension. |
||
| 462 | * |
||
| 463 | * @return array|Response|RedirectResponse |
||
| 464 | * @throws AccessDeniedException Thrown if the user doesn't have admin permission for the module |
||
| 465 | */ |
||
| 466 | public function uninstallAction( |
||
| 526 | ]; |
||
| 527 | } |
||
| 529 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.