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