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