| Conditions | 19 | 
| Paths | 22 | 
| Total Lines | 68 | 
| Code Lines | 39 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| 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 | ||
| 97 | public function submitAction(Request $request): Response | ||
| 98 |     { | ||
| 99 |         $uiElementType = $request->request->get('uiElementType'); | ||
| 100 |         if (!$request->isXmlHttpRequest() || empty($uiElementType)) { | ||
| 101 | throw $this->createNotFoundException(); | ||
| 102 | } | ||
| 103 | |||
| 104 | // Find UI Element from type | ||
| 105 |         try { | ||
| 106 | $uiElement = $this->uiElementFactory->getUiElementByType($uiElementType); | ||
| 107 |         } catch (UiElementNotFoundException $exception) { | ||
| 108 | throw $this->createNotFoundException($exception->getMessage()); | ||
| 109 | } | ||
| 110 | |||
| 111 | // Create and validate form | ||
| 112 | $data = $request->request->get($uiElement->getType()); | ||
| 113 | $form = $this->createForm($uiElement->getFormClass(), $data); | ||
| 114 | $form->handleRequest($request); | ||
| 115 |         if (!$form->isSubmitted()) { | ||
| 116 | throw $this->createNotFoundException(); | ||
| 117 | } | ||
| 118 | |||
| 119 | // Generate array of errors with field name | ||
| 120 |         if (!$form->isValid()) { | ||
| 121 | $errors = []; | ||
| 122 |             foreach ($form as $child) { | ||
| 123 |                 if (!$child->isValid()) { | ||
| 124 |                     foreach ($child->getErrors() as $error) { | ||
| 125 |                         $childLabel = $this->translator->trans(sprintf('monsieurbiz_richeditor_plugin.ui_element.%s.field.%s', $uiElementType, $child->getName())); | ||
| 126 | $errors[$childLabel][] = $error->getMessage(); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | } | ||
| 130 | return new JsonResponse(['errors' => $errors], Response::HTTP_NOT_ACCEPTABLE); | ||
| 131 | } | ||
| 132 | |||
| 133 | // Create object with UiElement data | ||
| 134 | $element = new \stdClass(); | ||
| 135 | $element->type = $uiElement->getType(); | ||
| 136 | $element->fields = new \stdClass(); | ||
| 137 | $element->name = null; | ||
| 138 |         foreach ($uiElement->getFields() as $field) { | ||
| 139 | // If file, upload it and retrieve the path | ||
| 140 |             if (($file = $request->files->get($uiElementType)) && isset($file[$field])) { | ||
| 141 |                 $element->fields->{$field} = $this->uploadAndReturnPath($file[$field]); | ||
| 142 | // Value in form exists, we take it | ||
| 143 |             } elseif (($value = $request->request->get($uiElementType)) && isset($value[$field])) { | ||
| 144 | // Allow array if choices inputs | ||
| 145 |                 if (is_array($value[$field])) { | ||
| 146 |                     $element->fields->{$field} = $value[$field]; | ||
| 147 |                 } else { | ||
| 148 |                     $element->fields->{$field} = (string) $value[$field]; | ||
| 149 | } | ||
| 150 | // Value is not set, set an empty one | ||
| 151 |             } else { | ||
| 152 |                 $element->fields->{$field} = ''; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | // UI Element's name | ||
| 157 | $innerType = $form->getConfig()->getType()->getInnerType(); | ||
| 158 |         if ($uiElement instanceof NameableInterface && $innerType instanceof NameableTypeInterface) { | ||
| 159 |             if (($value = $request->request->get($uiElementType)) && isset($value[$innerType->getUiElementNameName()])) { | ||
| 160 | $element->name = $value[$innerType->getUiElementNameName()]; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | return new JsonResponse(['element' => $element]); | ||
| 165 | } | ||
| 193 |