| Total Complexity | 43 |
| Total Lines | 386 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 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 |
||
| 30 | abstract class AbstractDocumentFormController extends AbstractController |
||
| 31 | { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * documentRepository |
||
| 35 | * |
||
| 36 | * @var \EWW\Dpf\Domain\Repository\DocumentRepository |
||
| 37 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 38 | */ |
||
| 39 | protected $documentRepository = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * fileRepository |
||
| 43 | * |
||
| 44 | * @var \EWW\Dpf\Domain\Repository\FileRepository |
||
| 45 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 46 | */ |
||
| 47 | protected $fileRepository = null; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * documentTypeRepository |
||
| 51 | * |
||
| 52 | * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository |
||
| 53 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 54 | */ |
||
| 55 | protected $documentTypeRepository = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * metadataGroupRepository |
||
| 59 | * |
||
| 60 | * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository |
||
| 61 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 62 | */ |
||
| 63 | protected $metadataGroupRepository = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * metadataObjectRepository |
||
| 67 | * |
||
| 68 | * @var \EWW\Dpf\Domain\Repository\MetadataObjectRepository |
||
| 69 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 70 | */ |
||
| 71 | protected $metadataObjectRepository = null; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * depositLicenseLogRepository |
||
| 75 | * |
||
| 76 | * @var \EWW\Dpf\Domain\Repository\DepositLicenseLogRepository |
||
| 77 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 78 | */ |
||
| 79 | protected $depositLicenseLogRepository = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * persistence manager |
||
| 83 | * |
||
| 84 | * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface |
||
| 85 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 86 | */ |
||
| 87 | protected $persistenceManager; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * fisDataService |
||
| 91 | * |
||
| 92 | * @var \EWW\Dpf\Services\FeUser\FisDataService |
||
| 93 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 94 | */ |
||
| 95 | protected $fisDataService = null; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * action list |
||
| 99 | * |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | public function listAction() |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * initialize newAction |
||
| 121 | * |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | public function initializeNewAction() |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * action new |
||
| 148 | * |
||
| 149 | * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm |
||
| 150 | * @param int $returnDocumentId |
||
| 151 | * @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("newDocumentForm") |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | public function newAction(DocumentForm $newDocumentForm = null, $returnDocumentId = 0) |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | public function initializeCreateAction() |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * action create |
||
| 204 | * |
||
| 205 | * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | public function createAction(DocumentForm $newDocumentForm) |
||
| 209 | { |
||
| 210 | $documentMapper = $this->objectManager->get(DocumentMapper::class); |
||
| 211 | |||
| 212 | /* @var $newDocument \EWW\Dpf\Domain\Model\Document */ |
||
| 213 | $newDocument = $documentMapper->getDocument($newDocumentForm); |
||
| 214 | |||
| 215 | $workflow = $this->objectManager->get(DocumentWorkflow::class)->getWorkflow(); |
||
| 216 | |||
| 217 | if ($this->request->getPluginName() === "Backoffice") { |
||
| 218 | $newDocument->setCreator($this->security->getUser()->getUid()); |
||
| 219 | $workflow->apply($newDocument, DocumentWorkflow::TRANSITION_CREATE); |
||
| 220 | } else { |
||
| 221 | $workflow->apply($newDocument, DocumentWorkflow::TRANSITION_CREATE_REGISTER); |
||
| 222 | } |
||
| 223 | |||
| 224 | // xml data fields are limited to 64 KB |
||
| 225 | if (strlen($newDocument->getXmlData()) >= Document::XML_DATA_SIZE_LIMIT) { |
||
| 226 | throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded."); |
||
| 227 | } |
||
| 228 | |||
| 229 | $this->documentRepository->add($newDocument); |
||
| 230 | $this->persistenceManager->persistAll(); |
||
| 231 | |||
| 232 | $newDocument = $this->documentRepository->findByUid($newDocument->getUid()); |
||
| 233 | $this->persistenceManager->persistAll(); |
||
| 234 | |||
| 235 | $depositLicenseLog = $this->depositLicenseLogRepository->findOneByProcessNumber($newDocument->getProcessNumber()); |
||
| 236 | if (empty($depositLicenseLog) && $newDocument->getDepositLicense()) { |
||
| 237 | // Only if there was no deposit license a notification may be sent |
||
| 238 | |||
| 239 | /** @var DepositLicenseLog $depositLicenseLog */ |
||
| 240 | $depositLicenseLog = $this->objectManager->get(DepositLicenseLog::class); |
||
| 241 | $depositLicenseLog->setUsername($this->security->getUsername()); |
||
| 242 | $depositLicenseLog->setObjectIdentifier($newDocument->getObjectIdentifier()); |
||
| 243 | $depositLicenseLog->setProcessNumber($newDocument->getProcessNumber()); |
||
| 244 | $depositLicenseLog->setTitle($newDocument->getTitle()); |
||
| 245 | $depositLicenseLog->setUrn($newDocument->getPrimaryUrn()); |
||
| 246 | $depositLicenseLog->setLicenceUri($newDocument->getDepositLicense()); |
||
| 247 | |||
| 248 | if ($newDocument->hasFiles()) { |
||
| 249 | $fileList = []; |
||
| 250 | foreach ($newDocument->getFile() as $file) { |
||
| 251 | if (!$file->isFileGroupDeleted()) { |
||
| 252 | $fileList[] = $file->getTitle(); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | $depositLicenseLog->setFileNames(implode(", ", $fileList)); |
||
| 256 | } |
||
| 257 | |||
| 258 | $this->depositLicenseLogRepository->add($depositLicenseLog); |
||
| 259 | |||
| 260 | /** @var Notifier $notifier */ |
||
| 261 | $notifier = $this->objectManager->get(Notifier::class); |
||
| 262 | $notifier->sendDepositLicenseNotification($newDocument); |
||
| 263 | } |
||
| 264 | |||
| 265 | // Add or update files |
||
| 266 | $files = $newDocumentForm->getFiles(); |
||
| 267 | // TODO: Is this still necessary? |
||
| 268 | if (is_array($files)) { |
||
| 269 | foreach ($files as $file) { |
||
| 270 | // TODO: Is this still necessary? |
||
| 271 | if ($file->getUID()) { |
||
| 272 | $this->fileRepository->update($file); |
||
| 273 | } else { |
||
| 274 | $file->setDocument($newDocument); |
||
| 275 | $this->fileRepository->add($file); |
||
| 276 | $newDocument->addFile($file); |
||
| 277 | $this->documentRepository->update($newDocument); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | } |
||
| 281 | $this->persistenceManager->persistAll(); |
||
| 282 | |||
| 283 | // index the document |
||
| 284 | $this->signalSlotDispatcher->dispatch(AbstractController::class, 'indexDocument', [$newDocument]); |
||
| 285 | |||
| 286 | } |
||
| 287 | |||
| 288 | public function initializeEditAction() |
||
| 289 | { |
||
| 290 | $requestArguments = $this->request->getArguments(); |
||
| 291 | |||
| 292 | if (array_key_exists('document', $requestArguments)) { |
||
| 293 | |||
| 294 | $document = $this->documentManager->read( |
||
| 295 | $this->request->getArgument('document'), |
||
| 296 | $this->security->getUser()->getUID() |
||
| 297 | ); |
||
| 298 | |||
| 299 | if ($document) { |
||
| 300 | $mapper = $this->objectManager->get(DocumentMapper::class); |
||
| 301 | $documentForm = $mapper->getDocumentForm($document); |
||
| 302 | } |
||
| 303 | |||
| 304 | } elseif (array_key_exists('documentForm', $requestArguments)) { |
||
| 305 | $documentForm = $this->request->getArgument('documentForm'); |
||
| 306 | } |
||
| 307 | |||
| 308 | $requestArguments['documentForm'] = $documentForm; |
||
| 309 | $this->request->setArguments($requestArguments); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * action edit |
||
| 314 | * |
||
| 315 | * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm |
||
| 316 | * @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("documentForm") |
||
| 317 | * @return void |
||
| 318 | */ |
||
| 319 | public function editAction(DocumentForm $documentForm) |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | public function initializeUpdateAction() |
||
| 333 | { |
||
| 334 | $requestArguments = $this->request->getArguments(); |
||
| 335 | |||
| 336 | if ($this->request->hasArgument('documentData')) { |
||
| 337 | $documentData = $this->request->getArgument('documentData'); |
||
| 338 | |||
| 339 | $formDataReader = $this->objectManager->get(FormDataReader::class); |
||
| 340 | $formDataReader->setFormData($documentData); |
||
| 341 | $docForm = $formDataReader->getDocumentForm(); |
||
| 342 | |||
| 343 | if (!$docForm->hasValidCsrfToken()) { |
||
| 344 | throw new Exception("Invalid CSRF Token"); |
||
| 345 | } |
||
| 346 | |||
| 347 | $requestArguments['documentForm'] = $docForm; |
||
| 348 | |||
| 349 | $docTypeUid = $documentData['type']; |
||
| 350 | $documentType = $this->documentTypeRepository->findByUid($docTypeUid); |
||
| 351 | $virtualType = $documentType->getVirtualType(); |
||
| 352 | |||
| 353 | |||
| 354 | if (!$formDataReader->uploadError() || $virtualType === true) { |
||
| 355 | $this->request->setArguments($requestArguments); |
||
| 356 | } else { |
||
| 357 | $t = $docForm->getFileNames(); |
||
| 358 | $this->redirect('list', 'Document', null, array('message' => 'UPLOAD_MAX_FILESIZE_ERROR', 'errorFiles' => $t)); |
||
| 359 | } |
||
| 360 | } else { |
||
| 361 | $this->redirectToList("UPLOAD_POST_SIZE_ERROR"); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * action update |
||
| 367 | * |
||
| 368 | * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm |
||
| 369 | * @return void |
||
| 370 | */ |
||
| 371 | public function updateAction(DocumentForm $documentForm) |
||
| 372 | { |
||
| 373 | $documentMapper = $this->objectManager->get(DocumentMapper::class); |
||
| 374 | |||
| 375 | /* @var $updateDocument \EWW\Dpf\Domain\Model\Document */ |
||
| 376 | $updateDocument = $documentMapper->getDocument($documentForm); |
||
| 377 | |||
| 378 | // xml data fields are limited to 64 KB |
||
| 379 | if (strlen($updateDocument->getXmlData()) >= Document::XML_DATA_SIZE_LIMIT) { |
||
| 380 | throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded."); |
||
| 381 | } |
||
| 382 | |||
| 383 | // add document to local es index |
||
| 384 | $elasticsearchMapper = $this->objectManager->get(ElasticsearchMapper::class); |
||
| 385 | $json = $elasticsearchMapper->getElasticsearchJson($updateDocument); |
||
| 386 | |||
| 387 | $elasticsearchRepository = $this->objectManager->get(ElasticsearchRepository::class); |
||
| 388 | // send document to index |
||
| 389 | $elasticsearchRepository->add($updateDocument, $json); |
||
| 390 | |||
| 391 | $updateDocument->setChanged(true); |
||
| 392 | $this->documentRepository->update($updateDocument); |
||
| 393 | |||
| 394 | // index the document |
||
| 395 | $this->signalSlotDispatcher->dispatch(AbstractController::class, 'indexDocument', [$updateDocument]); |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * action cancel |
||
| 400 | * |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | public function cancelAction() |
||
| 404 | { |
||
| 405 | $this->redirectToList(); |
||
| 406 | } |
||
| 407 | |||
| 408 | protected function redirectAfterUpdate() |
||
| 411 | } |
||
| 412 | |||
| 413 | protected function redirectToList($message = null) |
||
| 414 | { |
||
| 415 | $this->redirect('list'); |
||
| 419 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.