| Total Complexity | 45 |
| Total Lines | 311 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BaseAdminController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseAdminController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 73 | abstract class BaseAdminController extends AbstractController |
||
| 74 | { |
||
| 75 | protected $entity_class = ''; |
||
| 76 | protected $form_class = ''; |
||
| 77 | protected $twig_template = ''; |
||
| 78 | protected $route_base = ''; |
||
| 79 | protected $attachment_class = ''; |
||
| 80 | protected $parameter_class = ''; |
||
| 81 | |||
| 82 | protected $passwordEncoder; |
||
| 83 | protected $translator; |
||
| 84 | protected $attachmentHelper; |
||
| 85 | protected $attachmentSubmitHandler; |
||
| 86 | protected $commentHelper; |
||
| 87 | |||
| 88 | protected $historyHelper; |
||
| 89 | protected $timeTravel; |
||
| 90 | protected $dataTableFactory; |
||
| 91 | |||
| 92 | public function __construct(TranslatorInterface $translator, UserPasswordEncoderInterface $passwordEncoder, |
||
| 93 | AttachmentManager $attachmentHelper, AttachmentSubmitHandler $attachmentSubmitHandler, |
||
| 94 | EventCommentHelper $commentHelper, HistoryHelper $historyHelper, TimeTravel $timeTravel, |
||
| 95 | DataTableFactory $dataTableFactory) |
||
| 96 | { |
||
| 97 | if ('' === $this->entity_class || '' === $this->form_class || '' === $this->twig_template || '' === $this->route_base) { |
||
| 98 | throw new InvalidArgumentException('You have to override the $entity_class, $form_class, $route_base and $twig_template value in your subclasss!'); |
||
| 99 | } |
||
| 100 | |||
| 101 | if ('' === $this->attachment_class) { |
||
| 102 | throw new InvalidArgumentException('You have to override the $attachment_class value in your subclass!'); |
||
| 103 | } |
||
| 104 | |||
| 105 | if ('' === $this->parameter_class) { |
||
| 106 | throw new InvalidArgumentException('You have to override the $parameter_class value in your subclass!'); |
||
| 107 | } |
||
| 108 | |||
| 109 | $this->translator = $translator; |
||
| 110 | $this->passwordEncoder = $passwordEncoder; |
||
| 111 | $this->attachmentHelper = $attachmentHelper; |
||
| 112 | $this->attachmentSubmitHandler = $attachmentSubmitHandler; |
||
| 113 | $this->commentHelper = $commentHelper; |
||
| 114 | $this->historyHelper = $historyHelper; |
||
| 115 | $this->timeTravel = $timeTravel; |
||
| 116 | $this->dataTableFactory = $dataTableFactory; |
||
| 117 | } |
||
| 118 | |||
| 119 | protected function _edit(AbstractNamedDBElement $entity, Request $request, EntityManagerInterface $em, ?string $timestamp = null): Response |
||
| 120 | { |
||
| 121 | $this->denyAccessUnlessGranted('read', $entity); |
||
| 122 | |||
| 123 | $timeTravel_timestamp = null; |
||
| 124 | if (null !== $timestamp) { |
||
| 125 | $this->denyAccessUnlessGranted('@tools.timetravel'); |
||
| 126 | $this->denyAccessUnlessGranted('show_history', $entity); |
||
| 127 | //If the timestamp only contains numbers interpret it as unix timestamp |
||
| 128 | if (ctype_digit($timestamp)) { |
||
| 129 | $timeTravel_timestamp = new \DateTime(); |
||
| 130 | $timeTravel_timestamp->setTimestamp((int) $timestamp); |
||
| 131 | } else { //Try to parse it via DateTime |
||
| 132 | $timeTravel_timestamp = new \DateTime($timestamp); |
||
| 133 | } |
||
| 134 | $this->timeTravel->revertEntityToTimestamp($entity, $timeTravel_timestamp); |
||
| 135 | } |
||
| 136 | |||
| 137 | if ($this->isGranted('show_history', $entity)) { |
||
| 138 | $table = $this->dataTableFactory->createFromType( |
||
| 139 | LogDataTable::class, |
||
| 140 | [ |
||
| 141 | 'filter_elements' => $this->historyHelper->getAssociatedElements($entity), |
||
| 142 | 'mode' => 'element_history', |
||
| 143 | ], |
||
| 144 | ['pageLength' => 10] |
||
| 145 | ) |
||
| 146 | ->handleRequest($request); |
||
| 147 | |||
| 148 | if ($table->isCallback()) { |
||
| 149 | return $table->getResponse(); |
||
| 150 | } |
||
| 151 | } else { |
||
| 152 | $table = null; |
||
| 153 | } |
||
| 154 | |||
| 155 | $form = $this->createForm($this->form_class, $entity, [ |
||
| 156 | 'attachment_class' => $this->attachment_class, |
||
| 157 | 'parameter_class' => $this->parameter_class, |
||
| 158 | 'disabled' => null !== $timeTravel_timestamp ? true : null, |
||
| 159 | ]); |
||
| 160 | |||
| 161 | $form->handleRequest($request); |
||
| 162 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 163 | //Check if we editing a user and if we need to change the password of it |
||
| 164 | if ($entity instanceof User && ! empty($form['new_password']->getData())) { |
||
| 165 | $password = $this->passwordEncoder->encodePassword($entity, $form['new_password']->getData()); |
||
| 166 | $entity->setPassword($password); |
||
| 167 | //By default the user must change the password afterwards |
||
| 168 | $entity->setNeedPwChange(true); |
||
| 169 | } |
||
| 170 | |||
| 171 | //Upload passed files |
||
| 172 | $attachments = $form['attachments']; |
||
| 173 | foreach ($attachments as $attachment) { |
||
| 174 | /** @var FormInterface $attachment */ |
||
| 175 | $options = [ |
||
| 176 | 'secure_attachment' => $attachment['secureFile']->getData(), |
||
| 177 | 'download_url' => $attachment['downloadURL']->getData(), |
||
| 178 | ]; |
||
| 179 | |||
| 180 | try { |
||
| 181 | $this->attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData(), $options); |
||
| 182 | } catch (AttachmentDownloadException $attachmentDownloadException) { |
||
| 183 | $this->addFlash( |
||
| 184 | 'error', |
||
| 185 | $this->translator->trans('attachment.download_failed').' '.$attachmentDownloadException->getMessage() |
||
| 186 | ); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | $this->commentHelper->setMessage($form['log_comment']->getData()); |
||
| 191 | |||
| 192 | $em->persist($entity); |
||
| 193 | $em->flush(); |
||
| 194 | $this->addFlash('success', 'entity.edit_flash'); |
||
| 195 | |||
| 196 | //Rebuild form, so it is based on the updated data. Important for the parent field! |
||
| 197 | //We can not use dynamic form events here, because the parent entity list is build from database! |
||
| 198 | $form = $this->createForm($this->form_class, $entity, [ |
||
| 199 | 'attachment_class' => $this->attachment_class, |
||
| 200 | 'parameter_class' => $this->parameter_class |
||
| 201 | ]); |
||
| 202 | } elseif ($form->isSubmitted() && ! $form->isValid()) { |
||
| 203 | $this->addFlash('error', 'entity.edit_flash.invalid'); |
||
| 204 | } |
||
| 205 | |||
| 206 | return $this->render($this->twig_template, [ |
||
| 207 | 'entity' => $entity, |
||
| 208 | 'form' => $form->createView(), |
||
| 209 | 'attachment_helper' => $this->attachmentHelper, |
||
| 210 | 'route_base' => $this->route_base, |
||
| 211 | 'datatable' => $table, |
||
| 212 | 'timeTravel' => $timeTravel_timestamp, |
||
| 213 | ]); |
||
| 214 | } |
||
| 215 | |||
| 216 | protected function _new(Request $request, EntityManagerInterface $em, EntityImporter $importer) |
||
| 217 | { |
||
| 218 | /** @var AbstractStructuralDBElement|User $new_entity */ |
||
| 219 | $new_entity = new $this->entity_class(); |
||
| 220 | |||
| 221 | $this->denyAccessUnlessGranted('read', $new_entity); |
||
| 222 | |||
| 223 | //Basic edit form |
||
| 224 | $form = $this->createForm($this->form_class, $new_entity, [ |
||
| 225 | 'attachment_class' => $this->attachment_class, |
||
| 226 | 'parameter_class' => $this->parameter_class, |
||
| 227 | ]); |
||
| 228 | |||
| 229 | $form->handleRequest($request); |
||
| 230 | |||
| 231 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 232 | if ($new_entity instanceof User && ! empty($form['new_password']->getData())) { |
||
| 233 | $password = $this->passwordEncoder->encodePassword($new_entity, $form['new_password']->getData()); |
||
| 234 | $new_entity->setPassword($password); |
||
| 235 | //By default the user must change the password afterwards |
||
| 236 | $new_entity->setNeedPwChange(true); |
||
| 237 | } |
||
| 238 | |||
| 239 | //Upload passed files |
||
| 240 | $attachments = $form['attachments']; |
||
| 241 | foreach ($attachments as $attachment) { |
||
| 242 | /** @var FormInterface $attachment */ |
||
| 243 | $options = [ |
||
| 244 | 'secure_attachment' => $attachment['secureFile']->getData(), |
||
| 245 | 'download_url' => $attachment['downloadURL']->getData(), |
||
| 246 | ]; |
||
| 247 | |||
| 248 | try { |
||
| 249 | $this->attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData(), $options); |
||
| 250 | } catch (AttachmentDownloadException $attachmentDownloadException) { |
||
| 251 | $this->addFlash( |
||
| 252 | 'error', |
||
| 253 | $this->translator->trans('attachment.download_failed').' '.$attachmentDownloadException->getMessage() |
||
| 254 | ); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | $this->commentHelper->setMessage($form['log_comment']->getData()); |
||
| 259 | |||
| 260 | $em->persist($new_entity); |
||
| 261 | $em->flush(); |
||
| 262 | $this->addFlash('success', 'entity.created_flash'); |
||
| 263 | |||
| 264 | return $this->redirectToRoute($this->route_base.'_edit', ['id' => $new_entity->getID()]); |
||
| 265 | } |
||
| 266 | |||
| 267 | if ($form->isSubmitted() && ! $form->isValid()) { |
||
| 268 | $this->addFlash('error', 'entity.created_flash.invalid'); |
||
| 269 | } |
||
| 270 | |||
| 271 | //Import form |
||
| 272 | $import_form = $this->createForm(ImportType::class, ['entity_class' => $this->entity_class]); |
||
| 273 | $import_form->handleRequest($request); |
||
| 274 | |||
| 275 | if ($import_form->isSubmitted() && $import_form->isValid()) { |
||
| 276 | /** @var UploadedFile $file */ |
||
| 277 | $file = $import_form['file']->getData(); |
||
| 278 | $data = $import_form->getData(); |
||
| 279 | |||
| 280 | $options = [ |
||
| 281 | 'parent' => $data['parent'], |
||
| 282 | 'preserve_children' => $data['preserve_children'], |
||
| 283 | 'format' => $data['format'], |
||
| 284 | 'csv_separator' => $data['csv_separator'], |
||
| 285 | ]; |
||
| 286 | |||
| 287 | $this->commentHelper->setMessage('Import '.$file->getClientOriginalName()); |
||
| 288 | |||
| 289 | $errors = $importer->fileToDBEntities($file, $this->entity_class, $options); |
||
| 290 | |||
| 291 | foreach ($errors as $name => $error) { |
||
| 292 | /** @var ConstraintViolationList $error */ |
||
| 293 | $this->addFlash('error', $name.':'.$error); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | //Mass creation form |
||
| 298 | $mass_creation_form = $this->createForm(MassCreationForm::class, ['entity_class' => $this->entity_class]); |
||
| 299 | $mass_creation_form->handleRequest($request); |
||
| 300 | |||
| 301 | if ($mass_creation_form->isSubmitted() && $mass_creation_form->isValid()) { |
||
| 302 | $data = $mass_creation_form->getData(); |
||
| 303 | |||
| 304 | //Create entries based on input |
||
| 305 | $errors = []; |
||
| 306 | $results = $importer->massCreation($data['lines'], $this->entity_class, $data['parent'], $errors); |
||
| 307 | |||
| 308 | //Show errors to user: |
||
| 309 | foreach ($errors as $error) { |
||
| 310 | $this->addFlash('error', $error['entity']->getFullPath().':'.$error['violations']); |
||
| 311 | } |
||
| 312 | |||
| 313 | //Persist valid entities to DB |
||
| 314 | foreach ($results as $result) { |
||
| 315 | $em->persist($result); |
||
| 316 | } |
||
| 317 | $em->flush(); |
||
| 318 | } |
||
| 319 | |||
| 320 | return $this->render($this->twig_template, [ |
||
| 321 | 'entity' => $new_entity, |
||
| 322 | 'form' => $form->createView(), |
||
| 323 | 'import_form' => $import_form->createView(), |
||
| 324 | 'mass_creation_form' => $mass_creation_form->createView(), |
||
| 325 | 'attachment_helper' => $this->attachmentHelper, |
||
| 326 | 'route_base' => $this->route_base, |
||
| 327 | ]); |
||
| 328 | } |
||
| 329 | |||
| 330 | protected function _delete(Request $request, AbstractNamedDBElement $entity, StructuralElementRecursionHelper $recursionHelper): RedirectResponse |
||
| 366 | } |
||
| 367 | |||
| 368 | protected function _exportAll(EntityManagerInterface $em, EntityExporter $exporter, Request $request): Response |
||
| 369 | { |
||
| 370 | $entity = new $this->entity_class(); |
||
| 371 | |||
| 372 | $this->denyAccessUnlessGranted('read', $entity); |
||
| 373 | |||
| 374 | $entities = $em->getRepository($this->entity_class)->findAll(); |
||
| 375 | |||
| 376 | return $exporter->exportEntityFromRequest($entities, $request); |
||
| 377 | } |
||
| 378 | |||
| 379 | protected function _exportEntity(AbstractNamedDBElement $entity, EntityExporter $exporter, Request $request): \Symfony\Component\HttpFoundation\Response |
||
| 384 | } |
||
| 385 | } |
||
| 386 |