| Conditions | 13 |
| Paths | 10 |
| Total Lines | 74 |
| Code Lines | 38 |
| 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 |
||
| 108 | protected function _new(Request $request, EntityManagerInterface $em, EntityImporter $importer) |
||
| 109 | { |
||
| 110 | /** @var StructuralDBElement $new_entity */ |
||
| 111 | $new_entity = new $this->entity_class(); |
||
| 112 | |||
| 113 | $this->denyAccessUnlessGranted('read', $new_entity); |
||
| 114 | |||
| 115 | //Basic edit form |
||
| 116 | $form = $this->createForm($this->form_class, $new_entity); |
||
| 117 | |||
| 118 | $form->handleRequest($request); |
||
| 119 | |||
| 120 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 121 | if ($new_entity instanceof User && !empty($form['new_password']->getData())) { |
||
| 122 | $password = $this->passwordEncoder->encodePassword($new_entity, $form['new_password']->getData()); |
||
| 123 | $new_entity->setPassword($password); |
||
| 124 | //By default the user must change the password afterwards |
||
| 125 | $new_entity->setNeedPwChange(true); |
||
| 126 | } |
||
| 127 | $em->persist($new_entity); |
||
| 128 | $em->flush(); |
||
| 129 | $this->addFlash('success', $this->translator->trans('entity.created_flash')); |
||
| 130 | |||
| 131 | return $this->redirectToRoute($this->route_base . '_edit', ['id' => $new_entity->getID()]); |
||
| 132 | } |
||
| 133 | |||
| 134 | if ($form->isSubmitted() && ! $form->isValid()) { |
||
| 135 | $this->addFlash('error', $this->translator->trans('entity.created_flash.invalid')); |
||
| 136 | } |
||
| 137 | |||
| 138 | //Import form |
||
| 139 | $import_form = $this->createForm(ImportType::class, ['entity_class' => $this->entity_class]); |
||
| 140 | $import_form->handleRequest($request); |
||
| 141 | |||
| 142 | if ($import_form->isSubmitted() && $import_form->isValid()) { |
||
| 143 | /** @var UploadedFile $file */ |
||
| 144 | $file = $import_form['file']->getData(); |
||
| 145 | $data = $import_form->getData(); |
||
| 146 | |||
| 147 | $options = array('parent' => $data['parent'], 'preserve_children' => $data['preserve_children'], |
||
| 148 | 'format' => $data['format'], 'csv_separator' => $data['csv_separator']); |
||
| 149 | |||
| 150 | $errors = $importer->fileToDBEntities($file, $this->entity_class, $options); |
||
| 151 | |||
| 152 | foreach ($errors as $name => $error) { |
||
| 153 | /** @var $error ConstraintViolationList */ |
||
| 154 | $this->addFlash('error', $name . ':' . $error); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | //Mass creation form |
||
| 159 | $mass_creation_form = $this->createForm(MassCreationForm::class, ['entity_class' => $this->entity_class]); |
||
| 160 | $mass_creation_form->handleRequest($request); |
||
| 161 | |||
| 162 | if ($mass_creation_form->isSubmitted() && $mass_creation_form->isValid()) { |
||
| 163 | $data = $mass_creation_form->getData(); |
||
| 164 | |||
| 165 | dump($data); |
||
| 166 | |||
| 167 | //Create entries based on input |
||
| 168 | $errors = $importer->massCreation($data['lines'], $this->entity_class, $data['parent']); |
||
| 169 | |||
| 170 | //Show errors to user: |
||
| 171 | foreach ($errors as $name => $error) { |
||
| 172 | /** @var $error ConstraintViolationList */ |
||
| 173 | $this->addFlash('error', $name . ':' . $error); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | return $this->render($this->twig_template, [ |
||
| 178 | 'entity' => $new_entity, |
||
| 179 | 'form' => $form->createView(), |
||
| 180 | 'import_form' => $import_form->createView(), |
||
| 181 | 'mass_creation_form' => $mass_creation_form->createView() |
||
| 182 | ]); |
||
| 236 | } |