| Total Complexity | 41 |
| Total Lines | 368 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AbstractDocumentFormController 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 AbstractDocumentFormController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | abstract class AbstractDocumentFormController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController |
||
| 28 | { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * documentRepository |
||
| 32 | * |
||
| 33 | * @var \EWW\Dpf\Domain\Repository\DocumentRepository |
||
| 34 | * @inject |
||
| 35 | */ |
||
| 36 | protected $documentRepository = null; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * fileRepository |
||
| 40 | * |
||
| 41 | * @var \EWW\Dpf\Domain\Repository\FileRepository |
||
| 42 | * @inject |
||
| 43 | */ |
||
| 44 | protected $fileRepository = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * documentTypeRepository |
||
| 48 | * |
||
| 49 | * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository |
||
| 50 | * @inject |
||
| 51 | */ |
||
| 52 | protected $documentTypeRepository = null; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * metadataGroupRepository |
||
| 56 | * |
||
| 57 | * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository |
||
| 58 | * @inject |
||
| 59 | */ |
||
| 60 | protected $metadataGroupRepository = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * metadataObjectRepository |
||
| 64 | * |
||
| 65 | * @var \EWW\Dpf\Domain\Repository\MetadataObjectRepository |
||
| 66 | * @inject |
||
| 67 | */ |
||
| 68 | protected $metadataObjectRepository = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * persistence manager |
||
| 72 | * |
||
| 73 | * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface |
||
| 74 | * @inject |
||
| 75 | */ |
||
| 76 | protected $persistenceManager; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * action list |
||
| 80 | * |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | public function listAction() |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * action show |
||
| 127 | * |
||
| 128 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | public function showAction(\EWW\Dpf\Domain\Model\Document $document) |
||
| 132 | { |
||
| 133 | |||
| 134 | $this->view->assign('document', $document); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * initialize newAction |
||
| 139 | * |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | public function initializeNewAction() |
||
| 143 | { |
||
| 144 | |||
| 145 | $requestArguments = $this->request->getArguments(); |
||
| 146 | |||
| 147 | if (array_key_exists('documentData', $requestArguments)) { |
||
| 148 | die('Error: initializeNewAction'); |
||
|
|
|||
| 149 | } elseif (array_key_exists('documentType', $requestArguments)) { |
||
| 150 | $docTypeUid = $this->request->getArgument('documentType'); |
||
| 151 | $documentType = $this->documentTypeRepository->findByUid($docTypeUid); |
||
| 152 | $document = $this->objectManager->get(Document::class); |
||
| 153 | $document->setDocumentType($documentType); |
||
| 154 | $mapper = $this->objectManager->get(DocumentMapper::class); |
||
| 155 | $docForm = $mapper->getDocumentForm($document); |
||
| 156 | } elseif (array_key_exists('newDocumentForm', $requestArguments)) { |
||
| 157 | $docForm = $this->request->getArgument('newDocumentForm'); |
||
| 158 | } |
||
| 159 | |||
| 160 | $requestArguments['newDocumentForm'] = $docForm; |
||
| 161 | $this->request->setArguments($requestArguments); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * action new |
||
| 166 | * |
||
| 167 | * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm |
||
| 168 | * @ignorevalidation $newDocumentForm |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | public function newAction(\EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm = null) |
||
| 174 | } |
||
| 175 | |||
| 176 | public function initializeCreateAction() |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * action create |
||
| 207 | * |
||
| 208 | * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm |
||
| 209 | * @return void |
||
| 210 | */ |
||
| 211 | public function createAction(\EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm) |
||
| 260 | } |
||
| 261 | |||
| 262 | } |
||
| 263 | |||
| 264 | public function initializeEditAction() |
||
| 265 | { |
||
| 266 | |||
| 267 | $requestArguments = $this->request->getArguments(); |
||
| 268 | |||
| 269 | if (array_key_exists('document', $requestArguments)) { |
||
| 270 | $documentUid = $this->request->getArgument('document'); |
||
| 271 | $document = $this->documentRepository->findByUid($documentUid); |
||
| 272 | $mapper = $this->objectManager->get(DocumentMapper::class); |
||
| 273 | $documentForm = $mapper->getDocumentForm($document); |
||
| 274 | } elseif (array_key_exists('documentForm', $requestArguments)) { |
||
| 275 | $documentForm = $this->request->getArgument('documentForm'); |
||
| 276 | } |
||
| 277 | |||
| 278 | $requestArguments['documentForm'] = $documentForm; |
||
| 279 | $this->request->setArguments($requestArguments); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * action edit |
||
| 284 | * |
||
| 285 | * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm |
||
| 286 | * @ignorevalidation $documentForm |
||
| 287 | * @return void |
||
| 288 | */ |
||
| 289 | public function editAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm) |
||
| 292 | } |
||
| 293 | |||
| 294 | public function initializeUpdateAction() |
||
| 295 | { |
||
| 296 | $requestArguments = $this->request->getArguments(); |
||
| 297 | |||
| 298 | if ($this->request->hasArgument('documentData')) { |
||
| 299 | $documentData = $this->request->getArgument('documentData'); |
||
| 300 | |||
| 301 | $formDataReader = $this->objectManager->get(FormDataReader::class); |
||
| 302 | $formDataReader->setFormData($documentData); |
||
| 303 | $docForm = $formDataReader->getDocumentForm(); |
||
| 304 | |||
| 305 | $requestArguments['documentForm'] = $docForm; |
||
| 306 | |||
| 307 | $docTypeUid = $documentData['type']; |
||
| 308 | $documentType = $this->documentTypeRepository->findByUid($docTypeUid); |
||
| 309 | $virtual = $documentType->getVirtual(); |
||
| 310 | |||
| 311 | if (!$formDataReader->uploadError() || $virtual === true) { |
||
| 312 | $this->request->setArguments($requestArguments); |
||
| 313 | } else { |
||
| 314 | $t = $docForm->getNewFileNames(); |
||
| 315 | $this->redirect('list', 'Document', null, array('message' => 'UPLOAD_MAX_FILESIZE_ERROR', 'errorFiles' => $t)); |
||
| 316 | } |
||
| 317 | } else { |
||
| 318 | $this->redirectToList("UPLOAD_POST_SIZE_ERROR"); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * action update |
||
| 324 | * |
||
| 325 | * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm |
||
| 326 | * @return void |
||
| 327 | */ |
||
| 328 | public function updateAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm) |
||
| 329 | { |
||
| 330 | |||
| 331 | $requestArguments = $this->request->getArguments(); |
||
| 332 | |||
| 333 | $documentMapper = $this->objectManager->get(DocumentMapper::class); |
||
| 334 | $updateDocument = $documentMapper->getDocument($documentForm); |
||
| 335 | |||
| 336 | // xml data fields are limited to 64 KB |
||
| 337 | if (strlen($updateDocument->getXmlData()) >= 64 * 1024 || strlen($updateDocument->getSlubInfoData() >= 64 * 1024)) { |
||
| 338 | throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded."); |
||
| 339 | } |
||
| 340 | |||
| 341 | // add document to local es index |
||
| 342 | $elasticsearchMapper = $this->objectManager->get(ElasticsearchMapper::class); |
||
| 343 | $json = $elasticsearchMapper->getElasticsearchJson($updateDocument); |
||
| 344 | |||
| 345 | $elasticsearchRepository = $this->objectManager->get(ElasticsearchRepository::class); |
||
| 346 | // send document to index |
||
| 347 | $elasticsearchRepository->add($updateDocument, $json); |
||
| 348 | |||
| 349 | $updateDocument->setChanged(true); |
||
| 350 | |||
| 351 | $this->documentRepository->update($updateDocument); |
||
| 352 | |||
| 353 | // Delete files |
||
| 354 | foreach ($documentForm->getDeletedFiles() as $deleteFile) { |
||
| 355 | $deleteFile->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_DELETED); |
||
| 356 | $this->fileRepository->update($deleteFile); |
||
| 357 | } |
||
| 358 | |||
| 359 | // Add or update files |
||
| 360 | foreach ($documentForm->getNewFiles() as $newFile) { |
||
| 361 | |||
| 362 | if ($newFile->getUID()) { |
||
| 363 | $this->fileRepository->update($newFile); |
||
| 364 | } else { |
||
| 365 | $updateDocument->addFile($newFile); |
||
| 366 | } |
||
| 367 | |||
| 368 | } |
||
| 369 | |||
| 370 | if (array_key_exists('savecontinue', $requestArguments)) { |
||
| 371 | $this->forward('edit', null, null, array('documentForm' => $documentForm)); |
||
| 372 | } |
||
| 373 | |||
| 374 | $this->redirectToList(); |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * action cancel |
||
| 379 | * |
||
| 380 | * @return void |
||
| 381 | */ |
||
| 382 | public function cancelAction() |
||
| 383 | { |
||
| 384 | $this->redirectToList(); |
||
| 385 | } |
||
| 386 | |||
| 387 | public function initializeAction() |
||
| 390 | } |
||
| 391 | |||
| 392 | protected function redirectToList($message = null) |
||
| 393 | { |
||
| 394 | $this->redirect('list'); |
||
| 398 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.