| Conditions | 25 |
| Paths | > 20000 |
| Total Lines | 145 |
| Code Lines | 90 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 170 | public function menuAction( |
||
| 171 | RequestStack $requestStack, |
||
| 172 | RouterInterface $router, |
||
| 173 | ExtensionRepositoryInterface $extensionRepository, |
||
| 174 | ExtensionMenuCollector $extensionMenuCollector, |
||
| 175 | CapabilityApiInterface $capabilityApi, |
||
| 176 | AdminModuleRepositoryInterface $adminModuleRepository, |
||
| 177 | AdminCategoryRepositoryInterface $adminCategoryRepository |
||
| 178 | ): Response { |
||
| 179 | if (!$this->hasPermission('ZikulaAdminModule::', '::', ACCESS_ADMIN)) { |
||
| 180 | throw new AccessDeniedException(); |
||
| 181 | } |
||
| 182 | |||
| 183 | $masterRequest = $requestStack->getMasterRequest(); |
||
| 184 | $currentRequest = $requestStack->getCurrentRequest(); |
||
| 185 | |||
| 186 | // get caller info |
||
| 187 | $caller = []; |
||
| 188 | $caller['_zkModule'] = $masterRequest->attributes->get('_zkModule'); |
||
| 189 | $caller['_zkType'] = $masterRequest->attributes->get('_zkType'); |
||
| 190 | $caller['_zkFunc'] = $masterRequest->attributes->get('_zkFunc'); |
||
| 191 | $caller['path'] = $masterRequest->getPathInfo(); |
||
| 192 | $caller['info'] = !empty($caller['_zkModule']) ? $extensionRepository->get($caller['_zkModule']) : []; |
||
| 193 | |||
| 194 | // category we are in |
||
| 195 | $requestedCid = $masterRequest->attributes->get('acid'); |
||
| 196 | $defaultCid = empty($requestedCid) ? $this->getVar('startcategory') : $requestedCid; |
||
| 197 | |||
| 198 | $categoryId = $defaultCid; |
||
| 199 | if (!empty($caller['_zkModule']) && 'ZikulaAdminModule' !== $caller['_zkModule']) { |
||
| 200 | $moduleRelation = $adminModuleRepository->findOneBy(['mid' => $caller['info']['id']]); |
||
| 201 | if (null !== $moduleRelation) { |
||
| 202 | $categoryId = $moduleRelation->getCid(); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | $caller['category'] = $adminCategoryRepository->find($categoryId); |
||
| 206 | |||
| 207 | // mode requested |
||
| 208 | $mode = $currentRequest->attributes->has('mode') ? $currentRequest->attributes->get('mode') : 'categories'; |
||
| 209 | $mode = in_array($mode, ['categories', 'modules']) ? $mode : 'categories'; |
||
| 210 | // template requested |
||
| 211 | $template = $currentRequest->attributes->has('template') ? $currentRequest->attributes->get('template') : 'tabs'; |
||
| 212 | $template = in_array($template, ['tabs', 'panel']) ? $template : 'tabs'; |
||
| 213 | |||
| 214 | // get admin capable modules |
||
| 215 | $adminModules = $capabilityApi->getExtensionsCapableOf('admin'); |
||
| 216 | |||
| 217 | // sort modules by displayname |
||
| 218 | $moduleNames = []; |
||
| 219 | foreach ($adminModules as $key => $module) { |
||
| 220 | $moduleNames[$key] = $module['displayname']; |
||
| 221 | } |
||
| 222 | array_multisort($moduleNames, SORT_ASC, $adminModules); |
||
| 223 | |||
| 224 | $moduleCategories = $adminCategoryRepository->getIndexedCollection('cid'); |
||
| 225 | $menuModules = []; |
||
| 226 | $menuCategories = []; |
||
| 227 | foreach ($adminModules as $adminModule) { |
||
| 228 | if (!$this->hasPermission($adminModule['name'] . '::', '::', ACCESS_EDIT)) { |
||
| 229 | continue; |
||
| 230 | } |
||
| 231 | |||
| 232 | $categoryAssignment = $adminModuleRepository->findOneBy(['mid' => $adminModule['id']]); |
||
| 233 | if (null !== $categoryAssignment) { |
||
| 234 | $catid = $categoryAssignment->getCid(); |
||
| 235 | $order = $categoryAssignment->getSortorder(); |
||
| 236 | } else { |
||
| 237 | $catid = $this->getVar('startcategory'); |
||
| 238 | $order = 999; |
||
| 239 | } |
||
| 240 | |||
| 241 | $menuText = $adminModule['displayname']; |
||
| 242 | |||
| 243 | // url |
||
| 244 | try { |
||
| 245 | $menuTextUrl = isset($adminModule['capabilities']['admin']['route']) |
||
| 246 | ? $router->generate($adminModule['capabilities']['admin']['route']) |
||
| 247 | : ''; |
||
| 248 | } catch (RouteNotFoundException $routeNotFoundException) { |
||
| 249 | $menuTextUrl = 'javascript:void(0)'; |
||
| 250 | $menuText .= ' (<i class="fas fa-exclamation-triangle"></i> ' . $this->trans('invalid route') . ')'; |
||
| 251 | } |
||
| 252 | |||
| 253 | $moduleName = (string)$adminModule['name']; |
||
| 254 | $extensionMenu = $extensionMenuCollector->get($moduleName, ExtensionMenuInterface::TYPE_ADMIN); |
||
| 255 | if (isset($extensionMenu) && 'modules' === $mode && 'tabs' === $template) { |
||
| 256 | $extensionMenu->setChildrenAttribute('class', 'dropdown-menu'); |
||
| 257 | } |
||
| 258 | |||
| 259 | $module = [ |
||
| 260 | 'menutexturl' => $menuTextUrl, |
||
| 261 | 'menutext' => $menuText, |
||
| 262 | 'menutexttitle' => $adminModule['description'], |
||
| 263 | 'modname' => $adminModule['name'], |
||
| 264 | 'order' => $order, |
||
| 265 | 'id' => $adminModule['id'], |
||
| 266 | 'extensionMenu' => $extensionMenu, |
||
| 267 | 'icon' => $adminModule['icon'] |
||
| 268 | ]; |
||
| 269 | |||
| 270 | $menuModules[$adminModule['name']] = $module; |
||
| 271 | |||
| 272 | // category menu |
||
| 273 | if (!$this->hasPermission('ZikulaAdminModule:Category:', $moduleCategories[$catid]['name'] . '::' . $moduleCategories[$catid]['cid'], ACCESS_ADMIN)) { |
||
| 274 | continue; |
||
| 275 | } |
||
| 276 | |||
| 277 | $categorySortOrder = $moduleCategories[$catid]['sortorder']; |
||
| 278 | $menuCategories[$categorySortOrder]['title'] = $moduleCategories[$catid]['name']; |
||
| 279 | $menuCategories[$categorySortOrder]['url'] = $router->generate('zikulaadminmodule_admin_adminpanel', [ |
||
| 280 | 'acid' => $moduleCategories[$catid]['cid'] |
||
| 281 | ]); |
||
| 282 | $menuCategories[$categorySortOrder]['description'] = $moduleCategories[$catid]['description']; |
||
| 283 | $menuCategories[$categorySortOrder]['icon'] = $moduleCategories[$catid]['icon']; |
||
| 284 | $menuCategories[$categorySortOrder]['cid'] = $moduleCategories[$catid]['cid']; |
||
| 285 | $menuCategories[$categorySortOrder]['modules'][$adminModule['name']] = $module; |
||
| 286 | } |
||
| 287 | |||
| 288 | // add empty categories |
||
| 289 | /** @var AdminCategoryEntity[] $moduleCategories */ |
||
| 290 | foreach ($moduleCategories as $moduleCategory) { |
||
| 291 | if (array_key_exists($moduleCategory->getSortorder(), $menuCategories)) { |
||
| 292 | continue; |
||
| 293 | } |
||
| 294 | if (!$this->hasPermission('ZikulaAdminModule:Category:', $moduleCategory->getName() . '::' . $moduleCategory->getCid(), ACCESS_ADMIN)) { |
||
| 295 | continue; |
||
| 296 | } |
||
| 297 | |||
| 298 | $menuCategories[$moduleCategory->getSortOrder()] = [ |
||
| 299 | 'title' => $moduleCategory->getName(), |
||
| 300 | 'url' => $router->generate('zikulaadminmodule_admin_adminpanel', [ |
||
| 301 | 'acid' => $moduleCategory->getCid() |
||
| 302 | ]), |
||
| 303 | 'description' => $moduleCategory->getDescription(), |
||
| 304 | 'cid' => $moduleCategory->getCid(), |
||
| 305 | 'modules' => [] |
||
| 306 | ]; |
||
| 307 | } |
||
| 308 | ksort($menuCategories); |
||
| 309 | $fullTemplateName = $mode . '.' . $template; |
||
| 310 | |||
| 311 | return $this->render("@ZikulaAdminModule/AdminInterface/${fullTemplateName}.html.twig", [ |
||
| 312 | 'adminMenu' => ('categories' === $mode) ? $menuCategories : $menuModules, |
||
| 313 | 'mode' => $mode, |
||
| 314 | 'caller' => $caller |
||
| 315 | ]); |
||
| 318 |