| Total Complexity | 54 |
| Total Lines | 465 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 0 |
Complex classes like DocumentFormBackofficeController 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 DocumentFormBackofficeController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class DocumentFormBackofficeController extends AbstractDocumentFormController |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * documentTransferManager |
||
| 34 | * |
||
| 35 | * @var \EWW\Dpf\Services\Transfer\DocumentTransferManager $documentTransferManager |
||
| 36 | */ |
||
| 37 | protected $documentTransferManager; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * fedoraRepository |
||
| 41 | * |
||
| 42 | * @var \EWW\Dpf\Services\Transfer\FedoraRepository $fedoraRepository |
||
| 43 | */ |
||
| 44 | protected $fedoraRepository; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * DocumentController constructor. |
||
| 48 | */ |
||
| 49 | public function __construct() |
||
| 50 | { |
||
| 51 | parent::__construct(); |
||
| 52 | |||
| 53 | $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class); |
||
| 54 | $this->documentTransferManager = $objectManager->get(DocumentTransferManager::class); |
||
| 55 | $this->fedoraRepository = $objectManager->get(FedoraRepository::class); |
||
| 56 | $this->documentTransferManager->setRemoteRepository($this->fedoraRepository); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function arrayRecursiveDiff($aArray1, $aArray2) { |
||
| 60 | $aReturn = array(); |
||
| 61 | |||
| 62 | foreach ($aArray1 as $mKey => $mValue) { |
||
| 63 | if (array_key_exists($mKey, $aArray2)) { |
||
| 64 | if (is_array($mValue)) { |
||
| 65 | $aRecursiveDiff = $this->arrayRecursiveDiff($mValue, $aArray2[$mKey]); |
||
| 66 | if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; } |
||
| 67 | } else { |
||
| 68 | if ($mValue != $aArray2[$mKey]) { |
||
| 69 | $aReturn[$mKey] = $mValue; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } else { |
||
| 73 | $aReturn[$mKey] = $mValue; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | return $aReturn; |
||
| 77 | } |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * action edit |
||
| 82 | * |
||
| 83 | * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm |
||
| 84 | * @param bool $suggestMod |
||
| 85 | * @ignorevalidation $documentForm |
||
| 86 | * @return void |
||
| 87 | */ |
||
| 88 | public function editAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm, bool $suggestMod = false) |
||
| 89 | { |
||
| 90 | /** @var \EWW\Dpf\Domain\Model\Document $document */ |
||
| 91 | $document = $this->documentRepository->findByUid($documentForm->getDocumentUid()); |
||
| 92 | |||
| 93 | if ($suggestMod) { |
||
| 94 | $documentVoterAttribute = DocumentVoter::SUGGEST_MODIFICATION; |
||
| 95 | } else { |
||
| 96 | $documentVoterAttribute = DocumentVoter::EDIT; |
||
| 97 | } |
||
| 98 | |||
| 99 | if (!$this->authorizationChecker->isGranted($documentVoterAttribute, $document)) { |
||
| 100 | |||
| 101 | if ($document->getOwner() !== $this->security->getUser()->getUid()) { |
||
| 102 | $message = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate( |
||
| 103 | 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_edit.accessDenied', |
||
| 104 | 'dpf', |
||
| 105 | array($document->getTitle()) |
||
| 106 | ); |
||
| 107 | } else { |
||
| 108 | $message = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate( |
||
| 109 | 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_edit.failureBlocked', |
||
| 110 | 'dpf', |
||
| 111 | array($document->getTitle()) |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | $this->addFlashMessage($message, '', AbstractMessage::ERROR); |
||
| 116 | $this->redirect('showDetails', 'Document', null, ['document' => $document]); |
||
| 117 | return FALSE; |
||
| 118 | } |
||
| 119 | |||
| 120 | $this->view->assign('document', $document); |
||
| 121 | $this->view->assign('suggestMod', $suggestMod); |
||
| 122 | $document->setEditorUid($this->security->getUser()->getUid()); |
||
| 123 | $this->documentRepository->update($document); |
||
| 124 | $this->persistenceManager->persistAll(); |
||
| 125 | parent::editAction($documentForm); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm |
||
| 130 | * @param bool $restore |
||
| 131 | */ |
||
| 132 | public function createSuggestionDocumentAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm, $restore = FALSE) |
||
| 133 | { |
||
| 134 | $documentMapper = $this->objectManager->get(DocumentMapper::class); |
||
| 135 | |||
| 136 | $hasFilesFlag = true; |
||
| 137 | |||
| 138 | $workingCopy = $this->documentRepository->findByUid($documentForm->getDocumentUid()); |
||
| 139 | |||
| 140 | if ($workingCopy->isTemporary()) { |
||
| 141 | $workingCopy->setTemporary(false); |
||
| 142 | $workingCopy->setEditorUid(0); |
||
| 143 | } |
||
| 144 | |||
| 145 | if (empty($workingCopy->getFileData())) { |
||
| 146 | // no files are linked to the document |
||
| 147 | $hasFilesFlag = false; |
||
| 148 | } |
||
| 149 | |||
| 150 | $newDocument = $this->objectManager->get(Document::class); |
||
| 151 | |||
| 152 | $this->documentRepository->add($newDocument); |
||
| 153 | $this->persistenceManager->persistAll(); |
||
| 154 | |||
| 155 | /* @var $document \EWW\Dpf\Domain\Model\Document */ |
||
| 156 | $document = $documentMapper->getDocument($documentForm); |
||
| 157 | |||
| 158 | /* @var $newDocument \EWW\Dpf\Domain\Model\Document */ |
||
| 159 | $newDocument = $newDocument->copy($document); |
||
| 160 | |||
| 161 | if ($document->getObjectIdentifier()) { |
||
| 162 | $newDocument->setLinkedUid($document->getObjectIdentifier()); |
||
| 163 | } else { |
||
| 164 | $newDocument->setLinkedUid($document->getUid()); |
||
| 165 | } |
||
| 166 | |||
| 167 | $newDocument->setSuggestion(true); |
||
| 168 | $newDocument->setComment($document->getComment()); |
||
| 169 | |||
| 170 | if ($restore) { |
||
| 171 | $newDocument->setTransferStatus("RESTORE"); |
||
| 172 | } |
||
| 173 | |||
| 174 | if (!$hasFilesFlag) { |
||
| 175 | // Add or update files |
||
| 176 | foreach ($documentForm->getNewFiles() as $newFile) { |
||
| 177 | if ($newFile->getUID()) { |
||
| 178 | $this->fileRepository->update($newFile); |
||
| 179 | } else { |
||
| 180 | $newFile->setDocument($newDocument); |
||
| 181 | $this->fileRepository->add($newFile); |
||
| 182 | } |
||
| 183 | |||
| 184 | } |
||
| 185 | } else { |
||
| 186 | // remove files for suggest object |
||
| 187 | $newDocument->setFile($this->objectManager->get(ObjectStorage::class)); |
||
| 188 | } |
||
| 189 | |||
| 190 | |||
| 191 | try { |
||
| 192 | $newDocument->setOwner($this->security->getUser()->getUid()); |
||
| 193 | $this->documentRepository->add($newDocument); |
||
| 194 | $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::SUCCESS; |
||
| 195 | $this->addFlashMessage("Success", '', $severity,false); |
||
| 196 | } catch (\Throwable $t) { |
||
| 197 | $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR; |
||
| 198 | $this->addFlashMessage("Failed", '', $severity,false); |
||
| 199 | } |
||
| 200 | |||
| 201 | $this->redirectToDocumentList(); |
||
| 202 | } |
||
| 203 | |||
| 204 | public function updateAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm) |
||
| 205 | { |
||
| 206 | if ($this->request->getArgument('documentData')['suggestMod']) { |
||
| 207 | $restore = $this->request->getArgument('documentData')['suggestRestore']; |
||
| 208 | $this->forward('createSuggestionDocument', null, null, ['documentForm' => $documentForm, 'restore' => $restore]); |
||
| 209 | } |
||
| 210 | if ($this->request->hasArgument('saveAndUpdate')) { |
||
| 211 | $this->forward('updateRemote',NULL, NULL, ['documentForm' => $documentForm]); |
||
| 212 | } else { |
||
| 213 | $this->forward( |
||
| 214 | 'updateLocally', |
||
| 215 | NULL, |
||
| 216 | NULL, |
||
| 217 | [ |
||
| 218 | 'documentForm' => $documentForm, |
||
| 219 | 'workingCopy' => $this->request->hasArgument('saveWorkingCopy') |
||
| 220 | ] |
||
| 221 | ); |
||
| 222 | |||
| 223 | } |
||
| 224 | |||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm |
||
| 229 | * @param bool $workingCopy |
||
| 230 | * @return bool |
||
| 231 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException |
||
| 232 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException |
||
| 233 | */ |
||
| 234 | public function updateLocallyAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm, $workingCopy) |
||
| 235 | { |
||
| 236 | /** @var \EWW\Dpf\Domain\Model\Document $document */ |
||
| 237 | $document = $this->documentRepository->findByUid($documentForm->getDocumentUid()); |
||
| 238 | |||
| 239 | if (!$this->authorizationChecker->isGranted(DocumentVoter::UPDATE, $document)) { |
||
| 240 | $message = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate( |
||
| 241 | 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_updateLocally.accessDenied', |
||
| 242 | 'dpf', |
||
| 243 | array($document->getTitle()) |
||
| 244 | ); |
||
| 245 | $this->addFlashMessage($message, '', AbstractMessage::ERROR); |
||
| 246 | $this->redirect('showDetails', 'Document', null, ['document' => $document]); |
||
| 247 | return FALSE; |
||
| 248 | } |
||
| 249 | |||
| 250 | $documentMapper = $this->objectManager->get(DocumentMapper::class); |
||
| 251 | |||
| 252 | /** @var \EWW\Dpf\Domain\Model\Document $updateDocument */ |
||
| 253 | $updateDocument = $documentMapper->getDocument($documentForm); |
||
| 254 | |||
| 255 | try { |
||
| 256 | parent::updateAction($documentForm); |
||
| 257 | |||
| 258 | if ($updateDocument->isTemporary()) { |
||
| 259 | if ($workingCopy) { |
||
| 260 | $documents = $this->documentRepository->findByObjectIdentifier($updateDocument->getObjectIdentifier()); |
||
| 261 | foreach ($documents as $document) { |
||
| 262 | if (!$document->isTemporary() && !$document->isSuggestion()) { |
||
| 263 | $message = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate( |
||
| 264 | 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_updateLocally.failureCreateWorkingCopy', |
||
| 265 | 'dpf', |
||
| 266 | array($document->getTitle()) |
||
| 267 | ); |
||
| 268 | $this->addFlashMessage($message, '', AbstractMessage::ERROR); |
||
| 269 | $this->redirect('showDetails', 'Document', null, ['document' => $updateDocument]); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | $updateDocument->setTemporary(false); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | if ($this->security->getUserRole() === Security::ROLE_LIBRARIAN && |
||
| 277 | $updateDocument->getState() === DocumentWorkflow::STATE_REGISTERED_NONE) { |
||
| 278 | |||
| 279 | $state = explode(":", $updateDocument->getState()); |
||
| 280 | |||
| 281 | $state[0] = DocumentWorkflow::LOCAL_STATE_IN_PROGRESS; |
||
| 282 | $updateDocument->setState(implode(":", $state)); |
||
| 283 | } |
||
| 284 | |||
| 285 | if (!$updateDocument->isTemporary()) { |
||
| 286 | $updateDocument->setEditorUid(0); |
||
| 287 | } |
||
| 288 | |||
| 289 | $this->documentRepository->update($updateDocument); |
||
| 290 | |||
| 291 | $message = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate( |
||
| 292 | 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_updateLocally.success', |
||
| 293 | 'dpf', |
||
| 294 | array($updateDocument->getTitle()) |
||
| 295 | ); |
||
| 296 | $this->addFlashMessage($message, '', AbstractMessage::OK); |
||
| 297 | $this->redirect('showDetails', 'Document', null, ['document' => $updateDocument]); |
||
| 298 | |||
| 299 | } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $e) { |
||
| 300 | // A redirect always throws this exception, but in this case, however, |
||
| 301 | // redirection is desired and should not lead to an exception handling |
||
| 302 | } catch (\Exception $exception) { |
||
| 303 | $severity = AbstractMessage::ERROR; |
||
| 304 | |||
| 305 | if ($exception instanceof DPFExceptionInterface) { |
||
| 306 | $key = $exception->messageLanguageKey(); |
||
| 307 | } else { |
||
| 308 | $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected'; |
||
| 309 | } |
||
| 310 | |||
| 311 | $message[] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate( |
||
| 312 | 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_update.failure', |
||
| 313 | 'dpf', |
||
| 314 | array($updateDocument->getTitle()) |
||
| 315 | ); |
||
| 316 | |||
| 317 | $message[] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf'); |
||
| 318 | |||
| 319 | $this->addFlashMessage(implode(" ", $message), '', $severity,true); |
||
| 320 | $this->redirect('showDetails', 'Document', null, ['document' => $updateDocument]); |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | |||
| 325 | public function updateRemoteAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm) |
||
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | |||
| 390 | |||
| 391 | public function createAction(\EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm) |
||
| 392 | { |
||
| 393 | /** @var \EWW\Dpf\Helper\DocumentMapper $documentMapper */ |
||
| 394 | $documentMapper = $this->objectManager->get(DocumentMapper::class); |
||
| 395 | |||
| 396 | /** @var \EWW\Dpf\Domain\Model\Document $document */ |
||
| 397 | $document = $documentMapper->getDocument($newDocumentForm); |
||
| 398 | |||
| 399 | if (!$this->authorizationChecker->isGranted(DocumentVoter::CREATE, $document)) { |
||
| 400 | $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:documentForm.create.accessDenied'; |
||
| 401 | $args[] = $document->getTitle(); |
||
| 402 | $message = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf', $args); |
||
| 403 | $this->addFlashMessage($message, '', AbstractMessage::ERROR); |
||
| 404 | $this->redirect('showDetails', 'Document', null, ['document' => $document]); |
||
| 405 | return FALSE; |
||
| 406 | } |
||
| 407 | |||
| 408 | try { |
||
| 409 | parent::createAction($newDocumentForm); |
||
| 410 | |||
| 411 | $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK; |
||
| 412 | $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:documentForm.create.ok'; |
||
| 413 | $message = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf'); |
||
| 414 | $this->addFlashMessage( |
||
| 415 | $message, |
||
| 416 | '', |
||
| 417 | $severity, |
||
| 418 | true |
||
| 419 | ); |
||
| 420 | |||
| 421 | } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $e) { |
||
| 422 | // A redirect always throws this exception, but in this case, however, |
||
| 423 | // redirection is desired and should not lead to an exception handling |
||
| 424 | } catch (\Exception $exception) { |
||
| 425 | |||
| 426 | $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR; |
||
| 427 | |||
| 428 | if ($exception instanceof DPFExceptionInterface) { |
||
| 429 | $key = $exception->messageLanguageKey(); |
||
| 430 | } else { |
||
| 431 | $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected'; |
||
| 432 | } |
||
| 433 | |||
| 434 | $message[] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf'); |
||
| 435 | |||
| 436 | $this->addFlashMessage( |
||
| 437 | implode(" ", $message), |
||
| 438 | '', |
||
| 439 | $severity, |
||
| 440 | true |
||
| 441 | ); |
||
| 442 | } |
||
| 443 | |||
| 444 | $this->redirect('list', 'Document'); |
||
| 445 | } |
||
| 446 | |||
| 447 | |||
| 448 | /** |
||
| 449 | * action cancel edit |
||
| 450 | * |
||
| 451 | * @param integer $documentUid |
||
| 452 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException |
||
| 453 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException |
||
| 454 | * |
||
| 455 | * @return void |
||
| 456 | */ |
||
| 457 | public function cancelEditAction($documentUid = 0) |
||
| 471 | } |
||
| 472 | |||
| 473 | } |
||
| 474 | |||
| 475 | public function initializeAction() |
||
| 476 | { |
||
| 477 | $this->authorizationChecker->denyAccessUnlessLoggedIn(); |
||
| 478 | |||
| 479 | parent::initializeAction(); |
||
| 480 | |||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Redirect to the current document list. |
||
| 485 | * |
||
| 486 | * @param null $message |
||
| 487 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException |
||
| 488 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException |
||
| 489 | */ |
||
| 490 | protected function redirectToDocumentList($message = null) |
||
| 495 | } |
||
| 496 | |||
| 497 | |||
| 498 | } |
||
| 499 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths