| Total Complexity | 61 |
| Total Lines | 376 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AdminController 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 AdminController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class AdminController extends AbstractController |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * @Route("") |
||
| 47 | * |
||
| 48 | * The main administration function. |
||
| 49 | */ |
||
| 50 | public function index(): RedirectResponse |
||
| 51 | { |
||
| 52 | // Security check will be done in view() |
||
| 53 | return $this->redirectToRoute('zikulaadminmodule_admin_view'); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @Route("/categories/{page}", methods = {"GET"}, requirements={"page" = "\d+"}) |
||
| 58 | * @PermissionCheck("edit") |
||
| 59 | * @Theme("admin") |
||
| 60 | * @Template("@ZikulaAdminModule/Admin/view.html.twig") |
||
| 61 | * |
||
| 62 | * Views all admin categories. |
||
| 63 | */ |
||
| 64 | public function view(AdminCategoryRepositoryInterface $repository, int $page = 1): array |
||
| 65 | { |
||
| 66 | $pageSize = $this->getVar('itemsperpage'); |
||
| 67 | |||
| 68 | /** @var PaginatorInterface $paginator */ |
||
| 69 | $paginator = $repository->getPagedCategories(['sortorder' => 'ASC'], $page, $pageSize); |
||
|
|
|||
| 70 | $paginator->setRoute('zikulaadminmodule_admin_view'); |
||
| 71 | |||
| 72 | return [ |
||
| 73 | 'paginator' => $paginator |
||
| 74 | ]; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @Route("/newcategory") |
||
| 79 | * @PermissionCheck({"$_zkModule::Item", "::", "add"}) |
||
| 80 | * @Theme("admin") |
||
| 81 | * @Template("@ZikulaAdminModule/Admin/editCategory.html.twig") |
||
| 82 | * |
||
| 83 | * Displays and handles a form for the user to input the details of the new category. |
||
| 84 | * |
||
| 85 | * @return array|RedirectResponse |
||
| 86 | */ |
||
| 87 | public function create(Request $request) |
||
| 88 | { |
||
| 89 | $form = $this->createForm(AdminCategoryType::class, new AdminCategoryEntity()); |
||
| 90 | $form->handleRequest($request); |
||
| 91 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 92 | if ($form->get('save')->isClicked()) { |
||
| 93 | $adminCategory = $form->getData(); |
||
| 94 | $this->getDoctrine()->getManager()->persist($adminCategory); |
||
| 95 | $this->getDoctrine()->getManager()->flush(); |
||
| 96 | $this->addFlash('status', 'Done! Created new category.'); |
||
| 97 | } elseif ($form->get('cancel')->isClicked()) { |
||
| 98 | $this->addFlash('status', 'Operation cancelled.'); |
||
| 99 | } |
||
| 100 | |||
| 101 | return $this->redirectToRoute('zikulaadminmodule_admin_view'); |
||
| 102 | } |
||
| 103 | |||
| 104 | return [ |
||
| 105 | 'form' => $form->createView() |
||
| 106 | ]; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @Route("/modifycategory/{cid}", requirements={"cid" = "^[1-9]\d*$"}) |
||
| 111 | * @Theme("admin") |
||
| 112 | * @Template("@ZikulaAdminModule/Admin/editCategory.html.twig") |
||
| 113 | * |
||
| 114 | * Displays and handles a modify category form. |
||
| 115 | * |
||
| 116 | * @return array|RedirectResponse |
||
| 117 | * @throws AccessDeniedException Thrown if the user doesn't have permission to edit a category |
||
| 118 | */ |
||
| 119 | public function modify(Request $request, AdminCategoryEntity $category) |
||
| 120 | { |
||
| 121 | if (!$this->hasPermission('ZikulaAdminModule::Category', $category['name'] . '::' . $category->getCid(), ACCESS_EDIT)) { |
||
| 122 | throw new AccessDeniedException(); |
||
| 123 | } |
||
| 124 | |||
| 125 | $form = $this->createForm(AdminCategoryType::class, $category); |
||
| 126 | $form->handleRequest($request); |
||
| 127 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 128 | if ($form->get('save')->isClicked()) { |
||
| 129 | $category = $form->getData(); |
||
| 130 | if (!$this->hasPermission('ZikulaAdminModule::Category', $category->getName() . '::' . $category->getCid(), ACCESS_EDIT)) { |
||
| 131 | throw new AccessDeniedException(); |
||
| 132 | } |
||
| 133 | $this->getDoctrine()->getManager()->flush(); |
||
| 134 | $this->addFlash('status', 'Done! Saved category.'); |
||
| 135 | } elseif ($form->get('cancel')->isClicked()) { |
||
| 136 | $this->addFlash('status', 'Operation cancelled.'); |
||
| 137 | } |
||
| 138 | |||
| 139 | return $this->redirectToRoute('zikulaadminmodule_admin_view'); |
||
| 140 | } |
||
| 141 | |||
| 142 | return [ |
||
| 143 | 'form' => $form->createView() |
||
| 144 | ]; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @Route("/deletecategory/{cid}", requirements={"cid" = "^[1-9]\d*$"}) |
||
| 149 | * @Theme("admin") |
||
| 150 | * @Template("@ZikulaAdminModule/Admin/delete.html.twig") |
||
| 151 | * |
||
| 152 | * Deletes an admin category. |
||
| 153 | * |
||
| 154 | * @return array|RedirectResponse |
||
| 155 | * @throws AccessDeniedException Thrown if the user doesn't have permission to edit a category |
||
| 156 | */ |
||
| 157 | public function delete(Request $request, AdminCategoryEntity $category) |
||
| 181 | ]; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @Route("/panel/{acid}", methods = {"GET"}, requirements={"acid" = "^[1-9]\d*$"}) |
||
| 186 | * @PermissionCheck("edit") |
||
| 187 | * @Theme("admin") |
||
| 188 | * @Template("@ZikulaAdminModule/Admin/adminpanel.html.twig") |
||
| 189 | * |
||
| 190 | * Displays main admin panel for a category. |
||
| 191 | * |
||
| 192 | * @return array|Response |
||
| 193 | * @throws AccessDeniedException Thrown if the user doesn't have edit permission for the module |
||
| 194 | */ |
||
| 195 | public function adminpanel( |
||
| 196 | ZikulaHttpKernelInterface $kernel, |
||
| 197 | AdminCategoryRepositoryInterface $adminCategoryRepository, |
||
| 198 | AdminModuleRepositoryInterface $adminModuleRepository, |
||
| 199 | CapabilityApiInterface $capabilityApi, |
||
| 200 | RouterInterface $router, |
||
| 201 | AdminLinksHelper $adminLinksHelper, |
||
| 202 | ExtensionMenuCollector $extensionMenuCollector, |
||
| 203 | int $acid = null |
||
| 204 | ) { |
||
| 205 | if (empty($acid)) { |
||
| 206 | $acid = $this->getVar('startcategory', 1); |
||
| 207 | } |
||
| 208 | |||
| 209 | // Check to see if we have access to the requested category. |
||
| 210 | if (!$this->hasPermission('ZikulaAdminModule::', "::${acid}", ACCESS_ADMIN)) { |
||
| 211 | $acid = -1; |
||
| 212 | } |
||
| 213 | |||
| 214 | // Get details for selected category |
||
| 215 | $category = null; |
||
| 216 | if ($acid > 0) { |
||
| 217 | $category = $adminCategoryRepository->findOneBy(['cid' => $acid]); |
||
| 218 | } |
||
| 219 | |||
| 220 | if (!$category) { |
||
| 221 | // get the default category |
||
| 222 | $acid = $this->getVar('startcategory'); |
||
| 223 | |||
| 224 | // Check to see if we have access to the requested category. |
||
| 225 | if (!$this->hasPermission('ZikulaAdminModule::', "::${acid}", ACCESS_ADMIN)) { |
||
| 226 | throw new AccessDeniedException(); |
||
| 227 | } |
||
| 228 | |||
| 229 | $category = $adminCategoryRepository->findOneBy(['cid' => $acid]); |
||
| 230 | } |
||
| 231 | |||
| 232 | $templateParameters = []; |
||
| 233 | |||
| 234 | // assign the category |
||
| 235 | $templateParameters['category'] = $category; |
||
| 236 | |||
| 237 | $displayNameType = $this->getVar('displaynametype', 1); |
||
| 238 | $moduleEntities = $adminModuleRepository->findAll(); |
||
| 239 | |||
| 240 | // get admin capable modules |
||
| 241 | $adminModules = $capabilityApi->getExtensionsCapableOf('admin'); |
||
| 242 | $adminLinks = []; |
||
| 243 | foreach ($adminModules as $adminModule) { |
||
| 244 | if (!$this->hasPermission($adminModule['name'] . '::', 'ANY', ACCESS_EDIT)) { |
||
| 245 | continue; |
||
| 246 | } |
||
| 247 | |||
| 248 | $moduleId = (int)$adminModule['id']; |
||
| 249 | $moduleCategory = $adminCategoryRepository->getModuleCategory($moduleId); |
||
| 250 | $catid = null !== $moduleCategory ? $moduleCategory['cid'] : 0; |
||
| 251 | |||
| 252 | $sortOrder = -1; |
||
| 253 | foreach ($moduleEntities as $association) { |
||
| 254 | if ($association['mid'] !== $adminModule['id']) { |
||
| 255 | continue; |
||
| 256 | } |
||
| 257 | |||
| 258 | $sortOrder = $association['sortorder']; |
||
| 259 | break; |
||
| 260 | } |
||
| 261 | |||
| 262 | if ($catid === $acid || (false === $catid && $acid === $this->getVar('defaultcategory'))) { |
||
| 263 | $menuText = ''; |
||
| 264 | if (1 === $displayNameType) { |
||
| 265 | $menuText = $adminModule['displayname']; |
||
| 266 | } elseif (2 === $displayNameType) { |
||
| 267 | $menuText = $adminModule['name']; |
||
| 268 | } elseif (3 === $displayNameType) { |
||
| 269 | $menuText = $adminModule['displayname'] . ' (' . $adminModule['name'] . ')'; |
||
| 270 | } |
||
| 271 | |||
| 272 | try { |
||
| 273 | $menuTextUrl = isset($adminModule['capabilities']['admin']['route']) |
||
| 274 | ? $router->generate($adminModule['capabilities']['admin']['route']) |
||
| 275 | : ''; |
||
| 276 | } catch (RouteNotFoundException $routeNotFoundException) { |
||
| 277 | $menuTextUrl = 'javascript:void(0)'; |
||
| 278 | $menuText .= ' (<i class="fas fa-exclamation-triangle"></i> ' . $this->trans('invalid route') . ')'; |
||
| 279 | } |
||
| 280 | |||
| 281 | $moduleName = (string)$adminModule['name']; |
||
| 282 | /** @var \Knp\Menu\ItemInterface $extensionMenu */ |
||
| 283 | $extensionMenu = $extensionMenuCollector->get($moduleName, ExtensionMenuInterface::TYPE_ADMIN); |
||
| 284 | if (isset($extensionMenu)) { |
||
| 285 | $extensionMenu->setChildrenAttribute('class', 'dropdown-menu'); |
||
| 286 | } |
||
| 287 | |||
| 288 | $adminLinks[] = [ |
||
| 289 | 'menuTextUrl' => $menuTextUrl, |
||
| 290 | 'menuText' => $menuText, |
||
| 291 | 'menuTextTitle' => $adminModule['description'], |
||
| 292 | 'moduleName' => $adminModule['name'], |
||
| 293 | 'adminIcon' => $adminModule['icon'], |
||
| 294 | 'id' => $adminModule['id'], |
||
| 295 | 'order' => $sortOrder, |
||
| 296 | 'extensionMenu' => $extensionMenu |
||
| 297 | ]; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | $templateParameters['adminLinks'] = $adminLinksHelper->sortAdminModsByOrder($adminLinks); |
||
| 301 | |||
| 302 | return $templateParameters; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @Route("/categorymenu/{acid}", methods = {"GET"}, requirements={"acid" = "^[1-9]\d*$"}) |
||
| 307 | * @Theme("admin") |
||
| 308 | * |
||
| 309 | * Displays main category menu. |
||
| 310 | */ |
||
| 311 | public function categorymenu( |
||
| 419 | ]); |
||
| 420 | } |
||
| 421 | } |
||
| 422 |