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