| Total Complexity | 43 |
| Total Lines | 391 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| 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) |
||
| 155 | { |
||
| 156 | $this->view->assign('returnDocumentId', $returnDocumentId); |
||
| 157 | $this->view->assign('documentForm', $newDocumentForm); |
||
| 158 | |||
| 159 | if (!empty($this->security->getUserAccessToGroups())) { |
||
| 160 | $this->view->assign('currentUserAccessToGroup', $this->security->getUserAccessToGroups()); |
||
| 161 | } |
||
| 162 | |||
| 163 | if ($this->fisDataService->getPersonData($this->security->getFisPersId())) { |
||
| 164 | $this->view->assign('fisPersId', $this->security->getFisPersId()); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | public function initializeCreateAction() |
||
| 169 | { |
||
| 170 | |||
| 171 | $requestArguments = $this->request->getArguments(); |
||
| 172 | |||
| 173 | if ($this->request->hasArgument('documentData')) { |
||
| 174 | $documentData = $this->request->getArgument('documentData'); |
||
| 175 | |||
| 176 | $formDataReader = $this->objectManager->get(FormDataReader::class); |
||
| 177 | $formDataReader->setFormData($documentData); |
||
| 178 | |||
| 179 | $docForm = $formDataReader->getDocumentForm(); |
||
| 180 | $requestArguments['newDocumentForm'] = $docForm; |
||
| 181 | |||
| 182 | $docTypeUid = $documentData['type']; |
||
| 183 | $documentType = $this->documentTypeRepository->findByUid($docTypeUid); |
||
| 184 | $virtualType = $documentType->getVirtualType(); |
||
| 185 | |||
| 186 | if (!$formDataReader->uploadError() || $virtualType === true) { |
||
| 187 | $this->request->setArguments($requestArguments); |
||
| 188 | } else { |
||
| 189 | $t = $docForm->getNewFileNames(); |
||
| 190 | $this->redirect('list', 'DocumentForm', null, array('message' => 'UPLOAD_MAX_FILESIZE_ERROR', 'errorFiles' => $t)); |
||
| 191 | } |
||
| 192 | } else { |
||
| 193 | $this->redirectToList("UPLOAD_POST_SIZE_ERROR"); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * action create |
||
| 199 | * |
||
| 200 | * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function createAction(DocumentForm $newDocumentForm) |
||
| 204 | { |
||
| 205 | $documentMapper = $this->objectManager->get(DocumentMapper::class); |
||
| 206 | |||
| 207 | /* @var $newDocument \EWW\Dpf\Domain\Model\Document */ |
||
| 208 | $newDocument = $documentMapper->getDocument($newDocumentForm); |
||
| 209 | |||
| 210 | $workflow = $this->objectManager->get(DocumentWorkflow::class)->getWorkflow(); |
||
| 211 | |||
| 212 | if ($this->request->getPluginName() === "Backoffice") { |
||
| 213 | $newDocument->setCreator($this->security->getUser()->getUid()); |
||
| 214 | $workflow->apply($newDocument, DocumentWorkflow::TRANSITION_CREATE); |
||
| 215 | } else { |
||
| 216 | $workflow->apply($newDocument, DocumentWorkflow::TRANSITION_CREATE_REGISTER); |
||
| 217 | } |
||
| 218 | |||
| 219 | // xml data fields are limited to 64 KB |
||
| 220 | if (strlen($newDocument->getXmlData()) >= Document::XML_DATA_SIZE_LIMIT) { |
||
| 221 | throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded."); |
||
| 222 | } |
||
| 223 | |||
| 224 | $this->documentRepository->add($newDocument); |
||
| 225 | $this->persistenceManager->persistAll(); |
||
| 226 | |||
| 227 | $newDocument = $this->documentRepository->findByUid($newDocument->getUid()); |
||
| 228 | $this->persistenceManager->persistAll(); |
||
| 229 | |||
| 230 | $depositLicenseLog = $this->depositLicenseLogRepository->findOneByProcessNumber($newDocument->getProcessNumber()); |
||
| 231 | if (empty($depositLicenseLog) && $newDocument->getDepositLicense()) { |
||
| 232 | // Only if there was no deposit license a notification may be sent |
||
| 233 | |||
| 234 | /** @var DepositLicenseLog $depositLicenseLog */ |
||
| 235 | $depositLicenseLog = $this->objectManager->get(DepositLicenseLog::class); |
||
| 236 | $depositLicenseLog->setUsername($this->security->getUsername()); |
||
| 237 | $depositLicenseLog->setObjectIdentifier($newDocument->getObjectIdentifier()); |
||
| 238 | $depositLicenseLog->setProcessNumber($newDocument->getProcessNumber()); |
||
| 239 | $depositLicenseLog->setTitle($newDocument->getTitle()); |
||
| 240 | $depositLicenseLog->setUrn($newDocument->getPrimaryUrn()); |
||
| 241 | $depositLicenseLog->setLicenceUri($newDocument->getDepositLicense()); |
||
| 242 | |||
| 243 | if ($newDocument->getFileData()) { |
||
| 244 | |||
| 245 | $fileList = []; |
||
| 246 | foreach ($newDocument->getFile() as $file) { |
||
| 247 | if (!$file->isFileGroupDeleted()) { |
||
| 248 | $fileList[] = $file->getTitle(); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | $depositLicenseLog->setFileNames(implode(", ", $fileList)); |
||
| 252 | } |
||
| 253 | |||
| 254 | $this->depositLicenseLogRepository->add($depositLicenseLog); |
||
| 255 | |||
| 256 | /** @var Notifier $notifier */ |
||
| 257 | $notifier = $this->objectManager->get(Notifier::class); |
||
| 258 | $notifier->sendDepositLicenseNotification($newDocument); |
||
| 259 | } |
||
| 260 | |||
| 261 | // Add or update files |
||
| 262 | $newFiles = $newDocumentForm->getNewFiles(); |
||
| 263 | |||
| 264 | if (is_array($newFiles)) { |
||
| 265 | foreach ($newFiles as $newFile) { |
||
| 266 | |||
| 267 | if ($newFile->getUID()) { |
||
| 268 | $this->fileRepository->update($newFile); |
||
| 269 | } else { |
||
| 270 | $newFile->setDocument($newDocument); |
||
| 271 | $this->fileRepository->add($newFile); |
||
| 272 | $newDocument->addFile($newFile); |
||
| 273 | $this->documentRepository->update($newDocument); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | $this->persistenceManager->persistAll(); |
||
| 278 | |||
| 279 | // index the document |
||
| 280 | $this->signalSlotDispatcher->dispatch(AbstractController::class, 'indexDocument', [$newDocument]); |
||
| 281 | |||
| 282 | } |
||
| 283 | |||
| 284 | public function initializeEditAction() |
||
| 285 | { |
||
| 286 | $requestArguments = $this->request->getArguments(); |
||
| 287 | |||
| 288 | if (array_key_exists('document', $requestArguments)) { |
||
| 289 | |||
| 290 | $document = $this->documentManager->read( |
||
| 291 | $this->request->getArgument('document'), |
||
| 292 | $this->security->getUser()->getUID() |
||
| 293 | ); |
||
| 294 | |||
| 295 | if ($document) { |
||
| 296 | $mapper = $this->objectManager->get(DocumentMapper::class); |
||
| 297 | $documentForm = $mapper->getDocumentForm($document); |
||
| 298 | } |
||
| 299 | |||
| 300 | } elseif (array_key_exists('documentForm', $requestArguments)) { |
||
| 301 | $documentForm = $this->request->getArgument('documentForm'); |
||
| 302 | } |
||
| 303 | |||
| 304 | $requestArguments['documentForm'] = $documentForm; |
||
| 305 | $this->request->setArguments($requestArguments); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * action edit |
||
| 310 | * |
||
| 311 | * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm |
||
| 312 | * @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("documentForm") |
||
| 313 | * @return void |
||
| 314 | */ |
||
| 315 | public function editAction(DocumentForm $documentForm) |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | public function initializeUpdateAction() |
||
| 325 | { |
||
| 326 | $requestArguments = $this->request->getArguments(); |
||
| 327 | |||
| 328 | if ($this->request->hasArgument('documentData')) { |
||
| 329 | $documentData = $this->request->getArgument('documentData'); |
||
| 330 | |||
| 331 | $formDataReader = $this->objectManager->get(FormDataReader::class); |
||
| 332 | $formDataReader->setFormData($documentData); |
||
| 333 | $docForm = $formDataReader->getDocumentForm(); |
||
| 334 | |||
| 335 | $requestArguments['documentForm'] = $docForm; |
||
| 336 | |||
| 337 | $docTypeUid = $documentData['type']; |
||
| 338 | $documentType = $this->documentTypeRepository->findByUid($docTypeUid); |
||
| 339 | $virtualType = $documentType->getVirtualType(); |
||
| 340 | |||
| 341 | if (!$formDataReader->uploadError() || $virtualType === true) { |
||
| 342 | $this->request->setArguments($requestArguments); |
||
| 343 | } else { |
||
| 344 | $t = $docForm->getNewFileNames(); |
||
| 345 | $this->redirect('list', 'Document', null, array('message' => 'UPLOAD_MAX_FILESIZE_ERROR', 'errorFiles' => $t)); |
||
| 346 | } |
||
| 347 | } else { |
||
| 348 | $this->redirectToList("UPLOAD_POST_SIZE_ERROR"); |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * action update |
||
| 354 | * |
||
| 355 | * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm |
||
| 356 | * @return void |
||
| 357 | */ |
||
| 358 | public function updateAction(DocumentForm $documentForm) |
||
| 359 | { |
||
| 360 | $documentMapper = $this->objectManager->get(DocumentMapper::class); |
||
| 361 | |||
| 362 | /* @var $updateDocument \EWW\Dpf\Domain\Model\Document */ |
||
| 363 | $updateDocument = $documentMapper->getDocument($documentForm); |
||
| 364 | |||
| 365 | // xml data fields are limited to 64 KB |
||
| 366 | if (strlen($updateDocument->getXmlData()) >= Document::XML_DATA_SIZE_LIMIT) { |
||
| 367 | throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded."); |
||
| 368 | } |
||
| 369 | |||
| 370 | // add document to local es index |
||
| 371 | $elasticsearchMapper = $this->objectManager->get(ElasticsearchMapper::class); |
||
| 372 | $json = $elasticsearchMapper->getElasticsearchJson($updateDocument); |
||
| 373 | |||
| 374 | $elasticsearchRepository = $this->objectManager->get(ElasticsearchRepository::class); |
||
| 375 | // send document to index |
||
| 376 | $elasticsearchRepository->add($updateDocument, $json); |
||
| 377 | |||
| 378 | $updateDocument->setChanged(true); |
||
| 379 | $this->documentRepository->update($updateDocument); |
||
| 380 | |||
| 381 | |||
| 382 | // Delete files |
||
| 383 | foreach ($documentForm->getDeletedFiles() as $deleteFile) { |
||
| 384 | $deleteFile->setStatus(File::STATUS_DELETED); |
||
| 385 | $this->fileRepository->update($deleteFile); |
||
| 386 | } |
||
| 387 | |||
| 388 | // Add or update files |
||
| 389 | foreach ($documentForm->getNewFiles() as $newFile) { |
||
| 390 | |||
| 391 | if ($newFile->getUID()) { |
||
| 392 | $this->fileRepository->update($newFile); |
||
| 393 | } else { |
||
| 394 | $updateDocument->addFile($newFile); |
||
| 395 | } |
||
| 396 | |||
| 397 | } |
||
| 398 | |||
| 399 | // index the document |
||
| 400 | $this->signalSlotDispatcher->dispatch(AbstractController::class, 'indexDocument', [$updateDocument]); |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * action cancel |
||
| 405 | * |
||
| 406 | * @return void |
||
| 407 | */ |
||
| 408 | public function cancelAction() |
||
| 409 | { |
||
| 410 | $this->redirectToList(); |
||
| 411 | } |
||
| 412 | |||
| 413 | protected function redirectAfterUpdate() |
||
| 416 | } |
||
| 417 | |||
| 418 | protected function redirectToList($message = null) |
||
| 419 | { |
||
| 420 | $this->redirect('list'); |
||
| 421 | } |
||
| 422 | |||
| 423 | } |
||
| 424 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.