| Conditions | 16 | 
| Paths | 67 | 
| Total Lines | 132 | 
| 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 | ||
| 44 | public function __invoke(Request $request) | ||
| 45 |     { | ||
| 46 |         $admin = $this->pool->getInstance($request->get('admin_code')); | ||
| 47 | $admin->setRequest($request); | ||
| 48 |         $context = $request->get('_context', ''); | ||
| 49 | |||
| 50 |         if ('filter' === $context) { | ||
| 51 |             $admin->checkAccess('list'); | ||
| 52 |         } elseif (!$admin->hasAccess('create') && !$admin->hasAccess('edit')) { | ||
| 53 | throw new AccessDeniedException(); | ||
| 54 | } | ||
| 55 | |||
| 56 | // subject will be empty to avoid unnecessary database requests and keep autocomplete function fast | ||
| 57 | $admin->setSubject($admin->getNewInstance()); | ||
| 58 | |||
| 59 |         if ('filter' === $context) { | ||
| 60 | // filter | ||
| 61 |             $fieldDescription = $this->retrieveFilterFieldDescription($admin, $request->get('field')); | ||
| 62 | $filterAutocomplete = $admin->getDatagrid()->getFilter($fieldDescription->getName()); | ||
| 63 | |||
| 64 |             $property = $filterAutocomplete->getFieldOption('property'); | ||
| 65 |             $callback = $filterAutocomplete->getFieldOption('callback'); | ||
| 66 |             $minimumInputLength = $filterAutocomplete->getFieldOption('minimum_input_length', 3); | ||
| 67 |             $itemsPerPage = $filterAutocomplete->getFieldOption('items_per_page', 10); | ||
| 68 |             $reqParamPageNumber = $filterAutocomplete->getFieldOption('req_param_name_page_number', '_page'); | ||
| 69 |             $toStringCallback = $filterAutocomplete->getFieldOption('to_string_callback'); | ||
| 70 |             $targetAdminAccessAction = $filterAutocomplete->getFieldOption('target_admin_access_action', 'list'); | ||
| 71 |         } else { | ||
| 72 | // create/edit form | ||
| 73 |             $fieldDescription = $this->retrieveFormFieldDescription($admin, $request->get('field')); | ||
| 74 | $formAutocomplete = $admin->getForm()->get($fieldDescription->getName()); | ||
| 75 | |||
| 76 | $formAutocompleteConfig = $formAutocomplete->getConfig(); | ||
| 77 |             if ($formAutocompleteConfig->getAttribute('disabled')) { | ||
| 78 | throw new AccessDeniedException( | ||
| 79 | 'Autocomplete list can`t be retrieved because the form element is disabled or read_only.' | ||
| 80 | ); | ||
| 81 | } | ||
| 82 | |||
| 83 |             $property = $formAutocompleteConfig->getAttribute('property'); | ||
| 84 |             $callback = $formAutocompleteConfig->getAttribute('callback'); | ||
| 85 |             $minimumInputLength = $formAutocompleteConfig->getAttribute('minimum_input_length'); | ||
| 86 |             $itemsPerPage = $formAutocompleteConfig->getAttribute('items_per_page'); | ||
| 87 |             $reqParamPageNumber = $formAutocompleteConfig->getAttribute('req_param_name_page_number'); | ||
| 88 |             $toStringCallback = $formAutocompleteConfig->getAttribute('to_string_callback'); | ||
| 89 |             $targetAdminAccessAction = $formAutocompleteConfig->getAttribute('target_admin_access_action'); | ||
| 90 | } | ||
| 91 | |||
| 92 |         $searchText = $request->get('q'); | ||
| 93 | |||
| 94 | $targetAdmin = $fieldDescription->getAssociationAdmin(); | ||
| 95 | |||
| 96 | // check user permission | ||
| 97 | $targetAdmin->checkAccess($targetAdminAccessAction); | ||
| 98 | |||
| 99 |         if (mb_strlen($searchText, 'UTF-8') < $minimumInputLength) { | ||
| 100 | return new JsonResponse(['status' => 'KO', 'message' => 'Too short search string.'], 403); | ||
| 101 | } | ||
| 102 | |||
| 103 | $targetAdmin->setFilterPersister(null); | ||
| 104 | $datagrid = $targetAdmin->getDatagrid(); | ||
| 105 | |||
| 106 |         if (null !== $callback) { | ||
| 107 |             if (!is_callable($callback)) { | ||
| 108 |                 throw new \RuntimeException('Callback does not contain callable function.'); | ||
| 109 | } | ||
| 110 | |||
| 111 | call_user_func($callback, $targetAdmin, $property, $searchText); | ||
| 112 |         } else { | ||
| 113 |             if (is_array($property)) { | ||
| 114 | // multiple properties | ||
| 115 |                 foreach ($property as $prop) { | ||
| 116 |                     if (!$datagrid->hasFilter($prop)) { | ||
| 117 | throw new \RuntimeException(sprintf( | ||
| 118 | 'To retrieve autocomplete items,' | ||
| 119 | .' you should add filter "%s" to "%s" in configureDatagridFilters() method.', | ||
| 120 | $prop, get_class($targetAdmin) | ||
| 121 | )); | ||
| 122 | } | ||
| 123 | |||
| 124 | $filter = $datagrid->getFilter($prop); | ||
| 125 | $filter->setCondition(FilterInterface::CONDITION_OR); | ||
| 126 | |||
| 127 | $datagrid->setValue($filter->getFormName(), null, $searchText); | ||
| 128 | } | ||
| 129 |             } else { | ||
| 130 |                 if (!$datagrid->hasFilter($property)) { | ||
| 131 | throw new \RuntimeException(sprintf( | ||
| 132 | 'To retrieve autocomplete items,' | ||
| 133 | .' you should add filter "%s" to "%s" in configureDatagridFilters() method.', | ||
| 134 | $property, | ||
| 135 | get_class($targetAdmin) | ||
| 136 | )); | ||
| 137 | } | ||
| 138 | |||
| 139 | $datagrid->setValue($datagrid->getFilter($property)->getFormName(), null, $searchText); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 |         $datagrid->setValue('_per_page', null, $itemsPerPage); | ||
| 144 |         $datagrid->setValue('_page', null, $request->query->get($reqParamPageNumber, 1)); | ||
| 145 | $datagrid->buildPager(); | ||
| 146 | |||
| 147 | $pager = $datagrid->getPager(); | ||
| 148 | |||
| 149 | $items = []; | ||
| 150 | $results = $pager->getResults(); | ||
| 151 | |||
| 152 |         foreach ($results as $entity) { | ||
| 153 |             if (null !== $toStringCallback) { | ||
| 154 |                 if (!is_callable($toStringCallback)) { | ||
| 155 |                     throw new \RuntimeException('Option "to_string_callback" does not contain callable function.'); | ||
| 156 | } | ||
| 157 | |||
| 158 | $label = call_user_func($toStringCallback, $entity, $property); | ||
| 159 |             } else { | ||
| 160 | $resultMetadata = $targetAdmin->getObjectMetadata($entity); | ||
| 161 | $label = $resultMetadata->getTitle(); | ||
| 162 | } | ||
| 163 | |||
| 164 | $items[] = [ | ||
| 165 | 'id' => $admin->id($entity), | ||
| 166 | 'label' => $label, | ||
| 167 | ]; | ||
| 168 | } | ||
| 169 | |||
| 170 | return new JsonResponse([ | ||
| 171 | 'status' => 'OK', | ||
| 172 | 'more' => !$pager->isLastPage(), | ||
| 173 | 'items' => $items, | ||
| 174 | ]); | ||
| 175 | } | ||
| 176 | |||
| 229 |