| Conditions | 8 |
| Paths | 33 |
| Total Lines | 57 |
| 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 |
||
| 118 | public function importAction(Request $request) |
||
| 119 | { |
||
| 120 | /** @var $admin ImportableAdminTrait */ |
||
| 121 | $admin = $this->admin; |
||
| 122 | |||
| 123 | /** @var $is ImportService */ |
||
| 124 | $is = $this->get('sigmapix.sonata.import.service'); |
||
| 125 | |||
| 126 | // todo: $admin->checkAccess('import'); |
||
| 127 | |||
| 128 | $fileName = $request->get('fileName'); |
||
| 129 | if (!empty($fileName)) { |
||
| 130 | try { |
||
| 131 | $file = new UploadedFile($this->getParameter('import_directory').$fileName, $fileName); |
||
| 132 | |||
| 133 | $headers = $is->getHeaders($file); |
||
| 134 | |||
| 135 | /** @var $form \Symfony\Component\Form\Form */ |
||
| 136 | $form = $admin->getImportForm($headers); |
||
| 137 | $form->handleRequest($request); |
||
| 138 | |||
| 139 | if ($form->isSubmitted()) { |
||
| 140 | $preResponse = $admin->preImport($request, $form); |
||
| 141 | if (null !== $preResponse) { |
||
| 142 | return $preResponse; |
||
| 143 | } |
||
| 144 | |||
| 145 | try { |
||
| 146 | $results = $is->import($file, $form, $admin, $request); |
||
| 147 | |||
| 148 | $postResponse = $admin->postImport($request, $file, $form, $results); |
||
| 149 | |||
| 150 | if (null !== $postResponse) { |
||
| 151 | return $postResponse; |
||
| 152 | } |
||
| 153 | |||
| 154 | $this->addFlash('success', 'message_success'); |
||
| 155 | } catch (ConstraintViolationException $constraintViolationException) { |
||
| 156 | $this->addFlash('error', $constraintViolationException->getMessage()); |
||
| 157 | } catch (\Exception $exception) { |
||
| 158 | $this->addFlash('error', $exception->getMessage()); |
||
| 159 | } |
||
| 160 | |||
| 161 | return $this->redirect($admin->generateUrl('list')); |
||
| 162 | } |
||
| 163 | } catch (FileException $e) { |
||
| 164 | // TODO: show an error message if the file is missing |
||
| 165 | } |
||
| 166 | } |
||
| 167 | // todo: show an error message if the fileName is missing |
||
| 168 | |||
| 169 | return $this->render('SigmapixSonataImportBundle:CRUD:base_import_form.html.twig', [ |
||
| 170 | 'action' => 'import', |
||
| 171 | 'form' => $form->createView(), |
||
| 172 | 'object' => null, |
||
| 173 | ], null); |
||
| 174 | } |
||
| 175 | } |
||
| 176 |
If you suppress an error, we recommend checking for the error condition explicitly: