Conditions | 24 |
Paths | > 20000 |
Total Lines | 141 |
Code Lines | 88 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
158 | public function menu( |
||
159 | RequestStack $requestStack, |
||
160 | RouterInterface $router, |
||
161 | ExtensionRepositoryInterface $extensionRepository, |
||
162 | ExtensionMenuCollector $extensionMenuCollector, |
||
163 | CapabilityApiInterface $capabilityApi, |
||
164 | AdminModuleRepositoryInterface $adminModuleRepository, |
||
165 | AdminCategoryRepositoryInterface $adminCategoryRepository |
||
166 | ): Response { |
||
167 | $masterRequest = $requestStack->getMasterRequest(); |
||
168 | $currentRequest = $requestStack->getCurrentRequest(); |
||
169 | |||
170 | // get caller info |
||
171 | $caller = []; |
||
172 | $caller['_zkModule'] = $masterRequest->attributes->get('_zkModule'); |
||
173 | $caller['_zkType'] = $masterRequest->attributes->get('_zkType'); |
||
174 | $caller['_zkFunc'] = $masterRequest->attributes->get('_zkFunc'); |
||
175 | $caller['path'] = $masterRequest->getPathInfo(); |
||
176 | $caller['info'] = !empty($caller['_zkModule']) ? $extensionRepository->get($caller['_zkModule']) : []; |
||
177 | |||
178 | // category we are in |
||
179 | $requestedCid = $masterRequest->attributes->get('acid'); |
||
180 | $defaultCid = empty($requestedCid) ? $this->getVar('startcategory') : $requestedCid; |
||
181 | |||
182 | $categoryId = $defaultCid; |
||
183 | if (!empty($caller['_zkModule']) && 'ZikulaAdminModule' !== $caller['_zkModule']) { |
||
184 | $moduleRelation = $adminModuleRepository->findOneBy(['mid' => $caller['info']['id']]); |
||
185 | if (null !== $moduleRelation) { |
||
186 | $categoryId = $moduleRelation->getCid(); |
||
187 | } |
||
188 | } |
||
189 | $caller['category'] = $adminCategoryRepository->find($categoryId); |
||
190 | |||
191 | // mode requested |
||
192 | $mode = $currentRequest->attributes->has('mode') ? $currentRequest->attributes->get('mode') : 'categories'; |
||
193 | $mode = in_array($mode, ['categories', 'modules']) ? $mode : 'categories'; |
||
194 | // template requested |
||
195 | $template = $currentRequest->attributes->has('template') ? $currentRequest->attributes->get('template') : 'tabs'; |
||
196 | $template = in_array($template, ['tabs', 'panel']) ? $template : 'tabs'; |
||
197 | |||
198 | // get admin capable modules |
||
199 | $adminModules = $capabilityApi->getExtensionsCapableOf('admin'); |
||
200 | |||
201 | // sort modules by displayname |
||
202 | $moduleNames = []; |
||
203 | foreach ($adminModules as $key => $module) { |
||
204 | $moduleNames[$key] = $module['displayname']; |
||
205 | } |
||
206 | array_multisort($moduleNames, SORT_ASC, $adminModules); |
||
207 | |||
208 | $moduleCategories = $adminCategoryRepository->getIndexedCollection('cid'); |
||
209 | $menuModules = []; |
||
210 | $menuCategories = []; |
||
211 | foreach ($adminModules as $adminModule) { |
||
212 | if (!$this->hasPermission($adminModule['name'] . '::', '::', ACCESS_EDIT)) { |
||
213 | continue; |
||
214 | } |
||
215 | |||
216 | $categoryAssignment = $adminModuleRepository->findOneBy(['mid' => $adminModule['id']]); |
||
217 | if (null !== $categoryAssignment) { |
||
218 | $catid = $categoryAssignment->getCid(); |
||
219 | $order = $categoryAssignment->getSortorder(); |
||
220 | } else { |
||
221 | $catid = $this->getVar('startcategory'); |
||
222 | $order = 999; |
||
223 | } |
||
224 | |||
225 | $menuText = $adminModule['displayname']; |
||
226 | |||
227 | // url |
||
228 | try { |
||
229 | $menuTextUrl = isset($adminModule['capabilities']['admin']['route']) |
||
230 | ? $router->generate($adminModule['capabilities']['admin']['route']) |
||
231 | : ''; |
||
232 | } catch (RouteNotFoundException $routeNotFoundException) { |
||
233 | $menuTextUrl = 'javascript:void(0)'; |
||
234 | $menuText .= ' (<i class="fas fa-exclamation-triangle"></i> ' . $this->trans('invalid route') . ')'; |
||
235 | } |
||
236 | |||
237 | $moduleName = (string)$adminModule['name']; |
||
238 | $extensionMenu = $extensionMenuCollector->get($moduleName, ExtensionMenuInterface::TYPE_ADMIN); |
||
239 | if (isset($extensionMenu) && 'modules' === $mode && 'tabs' === $template) { |
||
240 | $extensionMenu->setChildrenAttribute('class', 'dropdown-menu'); |
||
241 | } |
||
242 | |||
243 | $module = [ |
||
244 | 'menutexturl' => $menuTextUrl, |
||
245 | 'menutext' => $menuText, |
||
246 | 'menutexttitle' => $adminModule['description'], |
||
247 | 'modname' => $adminModule['name'], |
||
248 | 'order' => $order, |
||
249 | 'id' => $adminModule['id'], |
||
250 | 'extensionMenu' => $extensionMenu, |
||
251 | 'icon' => $adminModule['icon'] |
||
252 | ]; |
||
253 | |||
254 | $menuModules[$adminModule['name']] = $module; |
||
255 | |||
256 | // category menu |
||
257 | if (!$this->hasPermission('ZikulaAdminModule:Category:', $moduleCategories[$catid]['name'] . '::' . $moduleCategories[$catid]['cid'], ACCESS_ADMIN)) { |
||
258 | continue; |
||
259 | } |
||
260 | |||
261 | $categorySortOrder = $moduleCategories[$catid]['sortorder']; |
||
262 | $menuCategories[$categorySortOrder]['title'] = $moduleCategories[$catid]['name']; |
||
263 | $menuCategories[$categorySortOrder]['url'] = $router->generate('zikulaadminmodule_admin_adminpanel', [ |
||
264 | 'acid' => $moduleCategories[$catid]['cid'] |
||
265 | ]); |
||
266 | $menuCategories[$categorySortOrder]['description'] = $moduleCategories[$catid]['description']; |
||
267 | $menuCategories[$categorySortOrder]['icon'] = $moduleCategories[$catid]['icon']; |
||
268 | $menuCategories[$categorySortOrder]['cid'] = $moduleCategories[$catid]['cid']; |
||
269 | $menuCategories[$categorySortOrder]['modules'][$adminModule['name']] = $module; |
||
270 | } |
||
271 | |||
272 | // add empty categories |
||
273 | /** @var AdminCategoryEntity[] $moduleCategories */ |
||
274 | foreach ($moduleCategories as $moduleCategory) { |
||
275 | if (array_key_exists($moduleCategory->getSortorder(), $menuCategories)) { |
||
276 | continue; |
||
277 | } |
||
278 | if (!$this->hasPermission('ZikulaAdminModule:Category:', $moduleCategory->getName() . '::' . $moduleCategory->getCid(), ACCESS_ADMIN)) { |
||
279 | continue; |
||
280 | } |
||
281 | |||
282 | $menuCategories[$moduleCategory->getSortOrder()] = [ |
||
283 | 'title' => $moduleCategory->getName(), |
||
284 | 'url' => $router->generate('zikulaadminmodule_admin_adminpanel', [ |
||
285 | 'acid' => $moduleCategory->getCid() |
||
286 | ]), |
||
287 | 'description' => $moduleCategory->getDescription(), |
||
288 | 'cid' => $moduleCategory->getCid(), |
||
289 | 'modules' => [] |
||
290 | ]; |
||
291 | } |
||
292 | ksort($menuCategories); |
||
293 | $fullTemplateName = $mode . '.' . $template; |
||
294 | |||
295 | return $this->render("@ZikulaAdminModule/AdminInterface/${fullTemplateName}.html.twig", [ |
||
296 | 'adminMenu' => ('categories' === $mode) ? $menuCategories : $menuModules, |
||
297 | 'mode' => $mode, |
||
298 | 'caller' => $caller |
||
299 | ]); |
||
302 |