| Total Complexity | 103 |
| Total Lines | 697 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 0 |
Complex classes like DocumentManager 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 DocumentManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class DocumentManager |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * objectManager |
||
| 22 | * |
||
| 23 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
| 24 | * @inject |
||
| 25 | */ |
||
| 26 | protected $objectManager = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * documentRepository |
||
| 30 | * |
||
| 31 | * @var \EWW\Dpf\Domain\Repository\DocumentRepository |
||
| 32 | * @inject |
||
| 33 | */ |
||
| 34 | protected $documentRepository = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * fileRepository |
||
| 38 | * |
||
| 39 | * @var \EWW\Dpf\Domain\Repository\FileRepository |
||
| 40 | * @inject |
||
| 41 | */ |
||
| 42 | protected $fileRepository = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * bookmarkRepository |
||
| 46 | * |
||
| 47 | * @var \EWW\Dpf\Domain\Repository\BookmarkRepository |
||
| 48 | * @inject |
||
| 49 | */ |
||
| 50 | protected $bookmarkRepository = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * persistence manager |
||
| 54 | * |
||
| 55 | * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface |
||
| 56 | * @inject |
||
| 57 | */ |
||
| 58 | protected $persistenceManager; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * signalSlotDispatcher |
||
| 62 | * |
||
| 63 | * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher |
||
| 64 | * @inject |
||
| 65 | */ |
||
| 66 | protected $signalSlotDispatcher = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * notifier |
||
| 70 | * |
||
| 71 | * @var \EWW\Dpf\Services\Email\Notifier |
||
| 72 | * @inject |
||
| 73 | */ |
||
| 74 | protected $notifier = null; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * security |
||
| 78 | * |
||
| 79 | * @var \EWW\Dpf\Security\Security |
||
| 80 | * @inject |
||
| 81 | */ |
||
| 82 | protected $security = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * frontendUserRepository |
||
| 86 | * |
||
| 87 | * @var \EWW\Dpf\Domain\Repository\FrontendUserRepository |
||
| 88 | * @inject |
||
| 89 | */ |
||
| 90 | protected $frontendUserRepository = null; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * elasticSearch |
||
| 94 | * |
||
| 95 | * @var \EWW\Dpf\Services\ElasticSearch\ElasticSearch |
||
| 96 | * @inject |
||
| 97 | */ |
||
| 98 | protected $elasticSearch = null; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * queryBuilder |
||
| 102 | * |
||
| 103 | * @var \EWW\Dpf\Services\ElasticSearch\QueryBuilder |
||
| 104 | * @inject |
||
| 105 | */ |
||
| 106 | protected $queryBuilder = null; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns the localized document identifiers (uid/objectIdentifier). |
||
| 110 | * |
||
| 111 | * @param $identifier |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public function resolveIdentifier($identifier) { |
||
| 149 | } |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * Returns a document specified by repository object identifier, a typo3 uid or a process number. |
||
| 154 | * |
||
| 155 | * @param string $identifier |
||
| 156 | * @return Document|null |
||
| 157 | */ |
||
| 158 | public function read($identifier) |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Updates a document locally or remotely. |
||
| 191 | * |
||
| 192 | * @param Document $document |
||
| 193 | * @param string $workflowTransition |
||
| 194 | * @param array $deletedFiles |
||
| 195 | * @param array $newFiles |
||
| 196 | * @param bool $addedFisIdOnly |
||
| 197 | * @return string|false |
||
| 198 | * @throws \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException |
||
| 199 | * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException |
||
| 200 | * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException |
||
| 201 | */ |
||
| 202 | public function update( |
||
| 203 | Document $document, $workflowTransition = null, $deletedFiles = [], $newFiles = [], $addedFisIdOnly = false |
||
| 204 | ) |
||
| 205 | { |
||
| 206 | // xml data fields are limited to 64 KB |
||
| 207 | // FIXME: Code duplication should be removed and it should be encapsulated or made configurable. |
||
| 208 | if (strlen($document->getXmlData()) >= Document::XML_DATA_SIZE_LIMIT) { |
||
| 209 | throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded."); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** @var \Symfony\Component\Workflow\Workflow $workflow */ |
||
| 213 | $workflow = $this->objectManager->get(DocumentWorkflow::class)->getWorkflow(); |
||
| 214 | |||
| 215 | if ($workflowTransition) { |
||
| 216 | if (!$workflow->can($document, $workflowTransition)) { |
||
| 217 | return false; |
||
| 218 | } |
||
| 219 | $workflow->apply($document, $workflowTransition); |
||
| 220 | } |
||
| 221 | |||
| 222 | if ($document->isSuggestion()) { |
||
| 223 | |||
| 224 | // if local suggestion copy |
||
| 225 | $updateResult = false; |
||
| 226 | |||
| 227 | } elseif ($document->isTemporaryCopy()) { |
||
| 228 | |||
| 229 | // if temporary working copy |
||
| 230 | $updateResult = $this->updateRemotely($document, $workflowTransition, $deletedFiles, $newFiles); |
||
| 231 | |||
| 232 | } elseif ( |
||
| 233 | $document->isWorkingCopy() && |
||
| 234 | ( |
||
| 235 | $workflowTransition === DocumentWorkflow::TRANSITION_POSTPONE || |
||
| 236 | $workflowTransition === DocumentWorkflow::TRANSITION_DISCARD || |
||
| 237 | $workflowTransition === DocumentWorkflow::TRANSITION_RELEASE_ACTIVATE |
||
| 238 | ) |
||
| 239 | ) { |
||
| 240 | // if local working copy with state change |
||
| 241 | $updateResult = $this->updateRemotely($document, $workflowTransition, $deletedFiles, $newFiles); |
||
| 242 | |||
| 243 | } elseif ($document->isWorkingCopy()) { |
||
| 244 | |||
| 245 | // if local working copy with no state change |
||
| 246 | $this->updateFiles($document, $deletedFiles, $newFiles); |
||
| 247 | $this->documentRepository->update($document); |
||
| 248 | $updateResult = $document->getDocumentIdentifier(); |
||
| 249 | |||
| 250 | } elseif ($workflowTransition == DocumentWorkflow::TRANSITION_RELEASE_PUBLISH) { |
||
| 251 | // Fedora ingest |
||
| 252 | if ($ingestedDocument = $this->getDocumentTransferManager()->ingest($document)) { |
||
| 253 | |||
| 254 | // After ingest all related bookmarks need an update of the identifier into an fedora object identifier. |
||
| 255 | if ($ingestedDocument instanceof Document) { |
||
| 256 | /** @var Bookmark $bookmark */ |
||
| 257 | foreach ($this->bookmarkRepository->findByDocumentIdentifier($ingestedDocument->getUid()) as $bookmark) { |
||
| 258 | $bookmark->setDocumentIdentifier($ingestedDocument->getDocumentIdentifier()); |
||
| 259 | $this->bookmarkRepository->update($bookmark); |
||
| 260 | } |
||
| 261 | $this->persistenceManager->persistAll(); |
||
| 262 | } else { |
||
| 263 | throw \Exception("Logical exception while updating bookmarks."); |
||
| 264 | } |
||
| 265 | |||
| 266 | // check embargo |
||
| 267 | if(!$this->hasActiveEmbargo($document)){ |
||
| 268 | $this->removeDocument($document); |
||
| 269 | } else { |
||
| 270 | $document->setState(DocumentWorkflow::constructState(DocumentWorkflow::LOCAL_STATE_IN_PROGRESS, $document->getRemoteState())); |
||
| 271 | } |
||
| 272 | $updateResult = $document->getDocumentIdentifier(); |
||
| 273 | } else { |
||
| 274 | $updateResult = false; |
||
| 275 | } |
||
| 276 | } else { |
||
| 277 | $this->updateFiles($document, $deletedFiles, $newFiles); |
||
| 278 | $this->documentRepository->update($document); |
||
| 279 | $updateResult = $document->getDocumentIdentifier(); |
||
| 280 | } |
||
| 281 | |||
| 282 | if ($updateResult) { |
||
| 283 | |||
| 284 | if (DocumentWorkflow::TRANSITION_RELEASE_PUBLISH) { |
||
| 285 | // delete local document from index |
||
| 286 | $this->signalSlotDispatcher->dispatch( |
||
| 287 | AbstractController::class, 'deleteDocumentFromIndex', [$document->getUid()] |
||
| 288 | ); |
||
| 289 | } |
||
| 290 | |||
| 291 | // index the document |
||
| 292 | $this->signalSlotDispatcher->dispatch( |
||
| 293 | AbstractController::class, 'indexDocument', [$document] |
||
| 294 | ); |
||
| 295 | |||
| 296 | // Notify assigned users |
||
| 297 | $recipients = $this->getUpdateNotificationRecipients($document); |
||
| 298 | $this->notifier->sendMyPublicationUpdateNotification($document, $recipients); |
||
| 299 | |||
| 300 | $recipients = $this->getNewPublicationNotificationRecipients($document); |
||
| 301 | $this->notifier->sendMyPublicationNewNotification($document, $recipients); |
||
| 302 | |||
| 303 | /** @var Notifier $notifier */ |
||
| 304 | $notifier = $this->objectManager->get(Notifier::class); |
||
| 305 | $notifier->sendChangedDocumentNotification($document, $addedFisIdOnly); |
||
| 306 | } |
||
| 307 | |||
| 308 | return $updateResult; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return DocumentTransferManager |
||
| 313 | */ |
||
| 314 | protected function getDocumentTransferManager() |
||
| 315 | { |
||
| 316 | /** @var DocumentTransferManager $documentTransferManager */ |
||
| 317 | $documentTransferManager = $this->objectManager->get(DocumentTransferManager::class); |
||
| 318 | |||
| 319 | /** @var FedoraRepository $remoteRepository */ |
||
| 320 | $remoteRepository = $this->objectManager->get(FedoraRepository::class); |
||
| 321 | |||
| 322 | $documentTransferManager->setRemoteRepository($remoteRepository); |
||
| 323 | |||
| 324 | return $documentTransferManager; |
||
| 325 | } |
||
| 326 | |||
| 327 | |||
| 328 | /** |
||
| 329 | * Adds and delete file model objects attached to the document. |
||
| 330 | * |
||
| 331 | * @param Document $document |
||
| 332 | * @param array $deletedFiles |
||
| 333 | * @param array $newFiles |
||
| 334 | * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException |
||
| 335 | * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException |
||
| 336 | */ |
||
| 337 | protected function updateFiles(Document $document, $deletedFiles, $newFiles) |
||
| 338 | { |
||
| 339 | // Delete files |
||
| 340 | /** @var File $deleteFile */ |
||
| 341 | foreach ($deletedFiles as $deleteFile) { |
||
| 342 | $deleteFile->setStatus(File::STATUS_DELETED); |
||
| 343 | $this->fileRepository->update($deleteFile); |
||
| 344 | } |
||
| 345 | |||
| 346 | // Add or update files |
||
| 347 | /** @var File $newFile */ |
||
| 348 | foreach ($newFiles as $newFile) { |
||
| 349 | |||
| 350 | if ($newFile->getUID()) { |
||
| 351 | $this->fileRepository->update($newFile); |
||
| 352 | } else { |
||
| 353 | $document->addFile($newFile); |
||
| 354 | } |
||
| 355 | |||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Removes the document from the local database. |
||
| 361 | * |
||
| 362 | * @param $document |
||
| 363 | * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException |
||
| 364 | */ |
||
| 365 | protected function removeDocument($document) |
||
| 366 | { |
||
| 367 | $files = $this->fileRepository->findByDocument($document->getUid()); |
||
| 368 | foreach ($files as $file) { |
||
| 369 | $this->fileRepository->remove($file); |
||
| 370 | } |
||
| 371 | $this->documentRepository->remove($document); |
||
| 372 | } |
||
| 373 | |||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * @param Document $document |
||
| 378 | * @param string $workflowTransition |
||
| 379 | * @param array $deletedFiles |
||
| 380 | * @param array $newFiles |
||
| 381 | * @return string |
||
| 382 | * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException |
||
| 383 | * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException |
||
| 384 | */ |
||
| 385 | protected function updateRemotely($document, $workflowTransition = null, $deletedFiles = [], $newFiles = []) |
||
| 386 | { |
||
| 387 | $lastModDate = $this->getDocumentTransferManager()->getLastModDate($document->getObjectIdentifier()); |
||
| 388 | $docLastModDate = $document->getRemoteLastModDate(); |
||
| 389 | if ($lastModDate !== $docLastModDate && !empty($docLastModDate)) { |
||
| 390 | // There is a newer version in the fedora repository. |
||
| 391 | return false; |
||
| 392 | } |
||
| 393 | |||
| 394 | $this->updateFiles($document, $deletedFiles, $newFiles); |
||
| 395 | $this->documentRepository->update($document); |
||
| 396 | |||
| 397 | switch ($workflowTransition) { |
||
| 398 | case DocumentWorkflow::TRANSITION_POSTPONE: |
||
| 399 | $transferState = DocumentTransferManager::INACTIVATE; |
||
| 400 | break; |
||
| 401 | |||
| 402 | case DocumentWorkflow::TRANSITION_DISCARD: |
||
| 403 | $transferState = DocumentTransferManager::DELETE; |
||
| 404 | break; |
||
| 405 | |||
| 406 | case DocumentWorkflow::TRANSITION_RELEASE_ACTIVATE: |
||
| 407 | $transferState = DocumentTransferManager::REVERT; |
||
| 408 | break; |
||
| 409 | |||
| 410 | default: |
||
| 411 | $transferState = null; |
||
| 412 | break; |
||
| 413 | } |
||
| 414 | |||
| 415 | if ($transferState) { |
||
| 416 | if (!$this->getDocumentTransferManager()->delete($document, $transferState)) { |
||
| 417 | return false; |
||
| 418 | } |
||
| 419 | } |
||
| 420 | |||
| 421 | if ($this->getDocumentTransferManager()->update($document)) { |
||
| 422 | |||
| 423 | if(!$this->hasActiveEmbargo($document)){ |
||
| 424 | $this->removeDocument($document); |
||
| 425 | } else { |
||
| 426 | $document->setState(DocumentWorkflow::LOCAL_STATE_IN_PROGRESS . ':' . $document->getRemoteState()); |
||
| 427 | } |
||
| 428 | return $document->getDocumentIdentifier(); |
||
| 429 | } |
||
| 430 | |||
| 431 | return false; |
||
| 432 | } |
||
| 433 | |||
| 434 | public function addSuggestion($editDocument, $restore = false, $comment = '') { |
||
| 435 | // add new document |
||
| 436 | /** @var Document $suggestionDocument */ |
||
| 437 | $suggestionDocument = $this->objectManager->get(Document::class); |
||
| 438 | $this->documentRepository->add($suggestionDocument); |
||
| 439 | $this->persistenceManager->persistAll(); |
||
| 440 | |||
| 441 | // copy properties from origin |
||
| 442 | $suggestionDocument = $suggestionDocument->copy($editDocument); |
||
| 443 | $suggestionDocument->setCreator($editDocument->getCreator()); |
||
| 444 | |||
| 445 | if ($suggestionDocument->isTemporary()) { |
||
| 446 | $suggestionDocument->setTemporary(false); |
||
| 447 | } |
||
| 448 | |||
| 449 | if (empty($suggestionDocument->getFileData())) { |
||
| 450 | // no files are linked to the document |
||
| 451 | $hasFilesFlag = false; |
||
| 452 | } |
||
| 453 | |||
| 454 | if ($editDocument->getObjectIdentifier()) { |
||
| 455 | $suggestionDocument->setLinkedUid($editDocument->getObjectIdentifier()); |
||
| 456 | } else { |
||
| 457 | $suggestionDocument->setLinkedUid($editDocument->getUid()); |
||
| 458 | } |
||
| 459 | |||
| 460 | $suggestionDocument->setSuggestion(true); |
||
| 461 | if ($comment) { |
||
| 462 | $suggestionDocument->setComment($comment); |
||
| 463 | } |
||
| 464 | |||
| 465 | if ($restore) { |
||
| 466 | $suggestionDocument->setTransferStatus("RESTORE"); |
||
| 467 | } |
||
| 468 | |||
| 469 | // if (!$hasFilesFlag) { |
||
| 470 | // // Add or update files |
||
| 471 | // foreach ($documentForm->getNewFiles() as $newFile) { |
||
| 472 | // if ($newFile->getUID()) { |
||
| 473 | // $this->fileRepository->update($newFile); |
||
| 474 | // } else { |
||
| 475 | // $newFile->setDocument($suggestionDocument); |
||
| 476 | // $this->fileRepository->add($newFile); |
||
| 477 | // } |
||
| 478 | // |
||
| 479 | // $suggestionDocument->addFile($newFile); |
||
| 480 | // } |
||
| 481 | // } else { |
||
| 482 | // // remove files for suggest object |
||
| 483 | // $suggestionDocument->setFile($this->objectManager->get(ObjectStorage::class)); |
||
| 484 | // } |
||
| 485 | |||
| 486 | try { |
||
| 487 | // $suggestionDocument->setCreator($this->security->getUser()->getUid()); |
||
| 488 | $this->documentRepository->add($suggestionDocument); |
||
| 489 | } catch (\Throwable $t) { |
||
| 490 | return null; |
||
| 491 | } |
||
| 492 | |||
| 493 | return $suggestionDocument; |
||
| 494 | |||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param $document |
||
| 499 | * @return bool (true: if no embargo is set or embargo is expired, false: embargo is active) |
||
| 500 | * @throws \Exception |
||
| 501 | */ |
||
| 502 | protected function hasActiveEmbargo($document) |
||
| 503 | { |
||
| 504 | $currentDate = new \DateTime('now'); |
||
| 505 | if($currentDate > $document->getEmbargoDate()){ |
||
| 506 | // embargo is expired |
||
| 507 | return false; |
||
| 508 | } else { |
||
| 509 | return true; |
||
| 510 | } |
||
| 511 | |||
| 512 | } |
||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * @param Document $document |
||
| 517 | * @return FrontendUser |
||
| 518 | */ |
||
| 519 | public function getCreatorUser(Document $document) { |
||
| 520 | return $this->frontendUserRepository->findByUid($document->getCreator()); |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @param Document $document |
||
| 525 | * @return array |
||
| 526 | */ |
||
| 527 | public function getAssignedUsers(Document $document) |
||
| 528 | { |
||
| 529 | $assignedUsers = []; |
||
| 530 | |||
| 531 | foreach ($document->getAssignedFobIdentifiers() as $fobId) { |
||
| 532 | $feUsers = $this->frontendUserRepository->findByFisPersId($fobId); |
||
| 533 | foreach ($feUsers as $feUser) { |
||
| 534 | |||
| 535 | $assignedUsers[$feUser->getUid()] = $feUser; |
||
| 536 | } |
||
| 537 | } |
||
| 538 | |||
| 539 | return $assignedUsers; |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @param Document $document |
||
| 544 | * @return array |
||
| 545 | */ |
||
| 546 | public function getNewlyAssignedUsers(Document $document) |
||
| 547 | { |
||
| 548 | $assignedUsers = []; |
||
| 549 | |||
| 550 | foreach ($document->getNewlyAssignedFobIdentifiers() as $fobId) { |
||
| 551 | $feUsers = $this->frontendUserRepository->findByFisPersId($fobId); |
||
| 552 | foreach ($feUsers as $feUser) { |
||
| 553 | $assignedUsers[$feUser->getUid()] = $feUser; |
||
| 554 | } |
||
| 555 | } |
||
| 556 | |||
| 557 | return $assignedUsers; |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * @param Document $document |
||
| 562 | * @return array |
||
| 563 | */ |
||
| 564 | public function getDocumentBookmarkUsers(Document $document) { |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @param Document $document |
||
| 580 | * @return array |
||
| 581 | */ |
||
| 582 | public function getUpdateNotificationRecipients(Document $document) |
||
| 583 | { |
||
| 584 | $users = []; |
||
| 585 | |||
| 586 | if ($document->getCreator()) { |
||
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @param Document $document |
||
| 686 | * @return array |
||
| 687 | */ |
||
| 688 | public function getNewPublicationNotificationRecipients(Document $document) |
||
| 718 |