| Conditions | 19 |
| Paths | 214 |
| Total Lines | 88 |
| Code Lines | 63 |
| 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 |
||
| 32 | public function executeAction(Request $request, $page = -1) |
||
| 33 | { |
||
| 34 | if (!$this->hasPermission('ZikulaSearchModule::', '::', ACCESS_READ)) { |
||
| 35 | throw new AccessDeniedException(); |
||
| 36 | } |
||
| 37 | $setActiveDefaults = false; |
||
| 38 | if (!$request->query->has('active')) { |
||
| 39 | $setActiveDefaults = true; |
||
| 40 | $activeModules = []; |
||
| 41 | } else { |
||
| 42 | $activeModules = $request->query->get('active'); |
||
| 43 | if (!is_array($activeModules)) { |
||
| 44 | $activeModules = [$activeModules]; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | $searchableModules = $this->get('zikula_search_module.internal.searchable_module_collector')->getAll(); |
||
| 48 | |||
| 49 | if (count($searchableModules) == 0) { |
||
| 50 | return $this->render('@ZikulaSearchModule/Search/unsearchable.html.twig'); |
||
| 51 | } |
||
| 52 | |||
| 53 | $moduleFormBuilder = $this->get('form.factory') |
||
| 54 | ->createNamedBuilder('modules', 'Symfony\Component\Form\Extension\Core\Type\FormType', [], [ |
||
| 55 | 'auto_initialize' => false, |
||
| 56 | 'required' => false |
||
| 57 | ]); |
||
| 58 | foreach ($searchableModules as $moduleName => $searchableInstance) { |
||
| 59 | if ($setActiveDefaults) { |
||
| 60 | $activeModules[$moduleName] = 1; |
||
| 61 | } |
||
| 62 | if ($this->getVar('disable_' . $moduleName, false)) { |
||
| 63 | continue; |
||
| 64 | } |
||
| 65 | if (!$this->hasPermission('ZikulaSearchModule::Item', $moduleName . '::', ACCESS_READ)) { |
||
| 66 | continue; |
||
| 67 | } |
||
| 68 | $moduleFormBuilder->add($moduleName, 'Zikula\SearchModule\Form\Type\AmendableModuleSearchType', [ |
||
| 69 | 'label' => $this->get('kernel')->getModule($moduleName)->getMetaData()->getDisplayName(), |
||
| 70 | 'translator' => $this->getTranslator(), |
||
| 71 | 'active' => !$setActiveDefaults || (isset($activeModules[$moduleName]) && ($activeModules[$moduleName] == 1)), |
||
| 72 | 'permissionApi' => $this->get('zikula_permissions_module.api.permission') |
||
| 73 | ]); |
||
| 74 | $searchableInstance->amendForm($moduleFormBuilder->get($moduleName)); |
||
| 75 | } |
||
| 76 | $form = $this->createForm('Zikula\SearchModule\Form\Type\SearchType', [], [ |
||
| 77 | 'translator' => $this->get('translator.default'), |
||
| 78 | ]); |
||
| 79 | $form->add($moduleFormBuilder->getForm()); |
||
| 80 | $q = $request->query->get('q', ''); |
||
| 81 | if ($request->isMethod('GET') && !empty($q)) { |
||
| 82 | $token = $request->query->get('_token'); |
||
| 83 | $form->submit(['q' => $q, '_token' => $token]); |
||
| 84 | } else { |
||
| 85 | $form->handleRequest($request); |
||
| 86 | } |
||
| 87 | $noResultsFound = false; |
||
| 88 | |||
| 89 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 90 | $searchApi = $this->get('zikula_search_module.api.search_api'); |
||
| 91 | $formData = $form->getData(); |
||
| 92 | $formData['numlimit'] = $this->getVar('itemsperpage', 25); |
||
| 93 | $formData['firstPage'] = $page < 1; |
||
| 94 | $formData['page'] = $page < 1 ? 1 : $page; |
||
| 95 | $result = $searchApi->search($formData['q'], $page < 1, $formData['searchType'], $formData['searchOrder'], $this->getVar('itemsperpage', 25), $page, $formData['modules']); |
||
| 96 | $searchApiErrors = $result['errors']; |
||
| 97 | if ($result['resultCount'] > 0) { |
||
| 98 | $templateParameters = array_merge($formData, [ |
||
| 99 | 'resultCount' => $result['resultCount'], |
||
| 100 | 'results' => $result['sqlResult'], |
||
| 101 | 'limitSummary' => $this->getVar('limitsummary', 200), |
||
| 102 | 'errors' => isset($searchApiErrors) ? $searchApiErrors : [] |
||
| 103 | ]); |
||
| 104 | // log the search if on first page |
||
| 105 | if ($formData['firstPage']) { |
||
| 106 | $searchApi->log($formData['q']); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $this->render('@ZikulaSearchModule/Search/results.html.twig', $templateParameters); |
||
| 110 | } else { |
||
| 111 | $noResultsFound = true; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | return [ |
||
| 116 | 'noResultsFound' => $noResultsFound, |
||
| 117 | 'form' => $form->createView(), |
||
| 118 | ]; |
||
| 119 | } |
||
| 120 | |||
| 179 |