| Total Complexity | 103 |
| Total Lines | 692 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | 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 | if (strlen($document->getXmlData()) >= 64 * 1024) { |
||
| 208 | throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded."); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** @var \Symfony\Component\Workflow\Workflow $workflow */ |
||
| 212 | $workflow = $this->objectManager->get(DocumentWorkflow::class)->getWorkflow(); |
||
| 213 | |||
| 214 | if ($workflowTransition) { |
||
| 215 | if (!$workflow->can($document, $workflowTransition)) { |
||
| 216 | return false; |
||
| 217 | } |
||
| 218 | $workflow->apply($document, $workflowTransition); |
||
| 219 | } |
||
| 220 | |||
| 221 | if ($document->isSuggestion()) { |
||
| 222 | |||
| 223 | // if local suggestion copy |
||
| 224 | $updateResult = false; |
||
| 225 | |||
| 226 | } elseif ($document->isTemporaryCopy()) { |
||
| 227 | |||
| 228 | // if temporary working copy |
||
| 229 | $updateResult = $this->updateRemotely($document, $workflowTransition, $deletedFiles, $newFiles); |
||
| 230 | |||
| 231 | } elseif ( |
||
| 232 | $document->isWorkingCopy() && |
||
| 233 | ( |
||
| 234 | $workflowTransition === DocumentWorkflow::TRANSITION_POSTPONE || |
||
| 235 | $workflowTransition === DocumentWorkflow::TRANSITION_DISCARD || |
||
| 236 | $workflowTransition === DocumentWorkflow::TRANSITION_RELEASE_ACTIVATE |
||
| 237 | ) |
||
| 238 | ) { |
||
| 239 | // if local working copy with state change |
||
| 240 | $updateResult = $this->updateRemotely($document, $workflowTransition, $deletedFiles, $newFiles); |
||
| 241 | |||
| 242 | } elseif ($document->isWorkingCopy()) { |
||
| 243 | |||
| 244 | // if local working copy with no state change |
||
| 245 | $this->updateFiles($document, $deletedFiles, $newFiles); |
||
| 246 | $this->documentRepository->update($document); |
||
| 247 | $updateResult = $document->getDocumentIdentifier(); |
||
| 248 | |||
| 249 | } elseif ($workflowTransition == DocumentWorkflow::TRANSITION_RELEASE_PUBLISH) { |
||
| 250 | // Fedora ingest |
||
| 251 | if ($ingestedDocument = $this->getDocumentTransferManager()->ingest($document)) { |
||
| 252 | |||
| 253 | // After ingest all related bookmarks need an update of the identifier into an fedora object identifier. |
||
| 254 | if ($ingestedDocument instanceof Document) { |
||
| 255 | /** @var Bookmark $bookmark */ |
||
| 256 | foreach ($this->bookmarkRepository->findByDocumentIdentifier($ingestedDocument->getUid()) as $bookmark) { |
||
| 257 | $bookmark->setDocumentIdentifier($ingestedDocument->getDocumentIdentifier()); |
||
| 258 | $this->bookmarkRepository->update($bookmark); |
||
| 259 | } |
||
| 260 | $this->persistenceManager->persistAll(); |
||
| 261 | } else { |
||
| 262 | throw \Exception("Logical exception while updating bookmarks."); |
||
| 263 | } |
||
| 264 | |||
| 265 | // check embargo |
||
| 266 | if(!$this->hasActiveEmbargo($document)){ |
||
| 267 | $this->removeDocument($document); |
||
| 268 | } else { |
||
| 269 | $document->setState(DocumentWorkflow::constructState(DocumentWorkflow::LOCAL_STATE_IN_PROGRESS, $document->getRemoteState())); |
||
| 270 | } |
||
| 271 | $updateResult = $document->getDocumentIdentifier(); |
||
| 272 | } else { |
||
| 273 | $updateResult = false; |
||
| 274 | } |
||
| 275 | } else { |
||
| 276 | $this->updateFiles($document, $deletedFiles, $newFiles); |
||
| 277 | $this->documentRepository->update($document); |
||
| 278 | $updateResult = $document->getDocumentIdentifier(); |
||
| 279 | } |
||
| 280 | |||
| 281 | if ($updateResult) { |
||
| 282 | |||
| 283 | if (DocumentWorkflow::TRANSITION_RELEASE_PUBLISH) { |
||
| 284 | // delete local document from index |
||
| 285 | $this->signalSlotDispatcher->dispatch( |
||
| 286 | AbstractController::class, 'deleteDocumentFromIndex', [$document->getUid()] |
||
| 287 | ); |
||
| 288 | } |
||
| 289 | |||
| 290 | // index the document |
||
| 291 | $this->signalSlotDispatcher->dispatch( |
||
| 292 | AbstractController::class, 'indexDocument', [$document] |
||
| 293 | ); |
||
| 294 | |||
| 295 | // Notify assigned users |
||
| 296 | $recipients = $this->getUpdateNotificationRecipients($document); |
||
| 297 | $this->notifier->sendMyPublicationUpdateNotification($document, $recipients); |
||
| 298 | |||
| 299 | $recipients = $this->getNewPublicationNotificationRecipients($document); |
||
| 300 | $this->notifier->sendMyPublicationNewNotification($document, $recipients); |
||
| 301 | |||
| 302 | /** @var Notifier $notifier */ |
||
| 303 | $notifier = $this->objectManager->get(Notifier::class); |
||
| 304 | $notifier->sendChangedDocumentNotification($document, $addedFisIdOnly); |
||
| 305 | } |
||
| 306 | |||
| 307 | return $updateResult; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return DocumentTransferManager |
||
| 312 | */ |
||
| 313 | protected function getDocumentTransferManager() |
||
| 314 | { |
||
| 315 | /** @var DocumentTransferManager $documentTransferManager */ |
||
| 316 | $documentTransferManager = $this->objectManager->get(DocumentTransferManager::class); |
||
| 317 | |||
| 318 | /** @var FedoraRepository $remoteRepository */ |
||
| 319 | $remoteRepository = $this->objectManager->get(FedoraRepository::class); |
||
| 320 | |||
| 321 | $documentTransferManager->setRemoteRepository($remoteRepository); |
||
| 322 | |||
| 323 | return $documentTransferManager; |
||
| 324 | } |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * Adds and delete file model objects attached to the document. |
||
| 329 | * |
||
| 330 | * @param Document $document |
||
| 331 | * @param array $deletedFiles |
||
| 332 | * @param array $newFiles |
||
| 333 | * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException |
||
| 334 | * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException |
||
| 335 | */ |
||
| 336 | protected function updateFiles(Document $document, $deletedFiles, $newFiles) |
||
| 337 | { |
||
| 338 | // Delete files |
||
| 339 | /** @var File $deleteFile */ |
||
| 340 | foreach ($deletedFiles as $deleteFile) { |
||
| 341 | $deleteFile->setStatus(File::STATUS_DELETED); |
||
| 342 | $this->fileRepository->update($deleteFile); |
||
| 343 | } |
||
| 344 | |||
| 345 | // Add or update files |
||
| 346 | /** @var File $newFile */ |
||
| 347 | foreach ($newFiles as $newFile) { |
||
| 348 | |||
| 349 | if ($newFile->getUID()) { |
||
| 350 | $this->fileRepository->update($newFile); |
||
| 351 | } else { |
||
| 352 | $document->addFile($newFile); |
||
| 353 | } |
||
| 354 | |||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Removes the document from the local database. |
||
| 360 | * |
||
| 361 | * @param $document |
||
| 362 | * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException |
||
| 363 | */ |
||
| 364 | protected function removeDocument($document) |
||
| 365 | { |
||
| 366 | $files = $this->fileRepository->findByDocument($document->getUid()); |
||
| 367 | foreach ($files as $file) { |
||
| 368 | $this->fileRepository->remove($file); |
||
| 369 | } |
||
| 370 | $this->documentRepository->remove($document); |
||
| 371 | } |
||
| 372 | |||
| 373 | |||
| 374 | |||
| 375 | /** |
||
| 376 | * @param Document $document |
||
| 377 | * @param string $workflowTransition |
||
| 378 | * @param array $deletedFiles |
||
| 379 | * @param array $newFiles |
||
| 380 | * @return string |
||
| 381 | * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException |
||
| 382 | * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException |
||
| 383 | */ |
||
| 384 | protected function updateRemotely($document, $workflowTransition = null, $deletedFiles = [], $newFiles = []) |
||
| 385 | { |
||
| 386 | $lastModDate = $this->getDocumentTransferManager()->getLastModDate($document->getObjectIdentifier()); |
||
| 387 | $docLastModDate = $document->getRemoteLastModDate(); |
||
| 388 | if ($lastModDate !== $docLastModDate && !empty($docLastModDate)) { |
||
| 389 | // There is a newer version in the fedora repository. |
||
| 390 | return false; |
||
| 391 | } |
||
| 392 | |||
| 393 | $this->updateFiles($document, $deletedFiles, $newFiles); |
||
| 394 | $this->documentRepository->update($document); |
||
| 395 | |||
| 396 | switch ($workflowTransition) { |
||
| 397 | case DocumentWorkflow::TRANSITION_POSTPONE: |
||
| 398 | $transferState = DocumentTransferManager::INACTIVATE; |
||
| 399 | break; |
||
| 400 | |||
| 401 | case DocumentWorkflow::TRANSITION_DISCARD: |
||
| 402 | $transferState = DocumentTransferManager::DELETE; |
||
| 403 | break; |
||
| 404 | |||
| 405 | case DocumentWorkflow::TRANSITION_RELEASE_ACTIVATE: |
||
| 406 | $transferState = DocumentTransferManager::REVERT; |
||
| 407 | break; |
||
| 408 | |||
| 409 | default: |
||
| 410 | $transferState = null; |
||
| 411 | break; |
||
| 412 | } |
||
| 413 | |||
| 414 | if ($transferState) { |
||
| 415 | if (!$this->getDocumentTransferManager()->delete($document, $transferState)) { |
||
| 416 | return false; |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | if ($this->getDocumentTransferManager()->update($document)) { |
||
| 421 | |||
| 422 | if(!$this->hasActiveEmbargo($document)){ |
||
| 423 | $this->removeDocument($document); |
||
| 424 | } else { |
||
| 425 | $document->setState(DocumentWorkflow::LOCAL_STATE_IN_PROGRESS . ':' . $document->getRemoteState()); |
||
| 426 | } |
||
| 427 | return $document->getDocumentIdentifier(); |
||
| 428 | } |
||
| 429 | |||
| 430 | return false; |
||
| 431 | } |
||
| 432 | |||
| 433 | public function addSuggestion($editDocument, $restore = false, $comment = '') { |
||
| 434 | // add new document |
||
| 435 | /** @var Document $suggestionDocument */ |
||
| 436 | $suggestionDocument = $this->objectManager->get(Document::class); |
||
| 437 | $this->documentRepository->add($suggestionDocument); |
||
| 438 | $this->persistenceManager->persistAll(); |
||
| 439 | |||
| 440 | // copy properties from origin |
||
| 441 | $suggestionDocument = $suggestionDocument->copy($editDocument); |
||
| 442 | $suggestionDocument->setCreator($editDocument->getCreator()); |
||
| 443 | |||
| 444 | if ($suggestionDocument->isTemporary()) { |
||
| 445 | $suggestionDocument->setTemporary(false); |
||
| 446 | } |
||
| 447 | |||
| 448 | if (empty($suggestionDocument->getFileData())) { |
||
| 449 | // no files are linked to the document |
||
| 450 | $hasFilesFlag = false; |
||
| 451 | } |
||
| 452 | |||
| 453 | if ($editDocument->getObjectIdentifier()) { |
||
| 454 | $suggestionDocument->setLinkedUid($editDocument->getObjectIdentifier()); |
||
| 455 | } else { |
||
| 456 | $suggestionDocument->setLinkedUid($editDocument->getUid()); |
||
| 457 | } |
||
| 458 | |||
| 459 | $suggestionDocument->setSuggestion(true); |
||
| 460 | if ($comment) { |
||
| 461 | $suggestionDocument->setComment($comment); |
||
| 462 | } |
||
| 463 | |||
| 464 | if ($restore) { |
||
| 465 | $suggestionDocument->setTransferStatus("RESTORE"); |
||
| 466 | } |
||
| 467 | |||
| 468 | // if (!$hasFilesFlag) { |
||
| 469 | // // Add or update files |
||
| 470 | // foreach ($documentForm->getNewFiles() as $newFile) { |
||
| 471 | // if ($newFile->getUID()) { |
||
| 472 | // $this->fileRepository->update($newFile); |
||
| 473 | // } else { |
||
| 474 | // $newFile->setDocument($suggestionDocument); |
||
| 475 | // $this->fileRepository->add($newFile); |
||
| 476 | // } |
||
| 477 | // |
||
| 478 | // $suggestionDocument->addFile($newFile); |
||
| 479 | // } |
||
| 480 | // } else { |
||
| 481 | // // remove files for suggest object |
||
| 482 | // $suggestionDocument->setFile($this->objectManager->get(ObjectStorage::class)); |
||
| 483 | // } |
||
| 484 | |||
| 485 | try { |
||
| 486 | // $suggestionDocument->setCreator($this->security->getUser()->getUid()); |
||
| 487 | $this->documentRepository->add($suggestionDocument); |
||
| 488 | } catch (\Throwable $t) { |
||
| 489 | return null; |
||
| 490 | } |
||
| 491 | |||
| 492 | return $suggestionDocument; |
||
| 493 | |||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param $document |
||
| 498 | * @return bool (true: if no embargo is set or embargo is expired, false: embargo is active) |
||
| 499 | * @throws \Exception |
||
| 500 | */ |
||
| 501 | protected function hasActiveEmbargo($document) |
||
| 502 | { |
||
| 503 | $currentDate = new \DateTime('now'); |
||
| 504 | if($currentDate > $document->getEmbargoDate()){ |
||
| 505 | // embargo is expired |
||
| 506 | return false; |
||
| 507 | } else { |
||
| 508 | return true; |
||
| 509 | } |
||
| 510 | |||
| 511 | } |
||
| 512 | |||
| 513 | |||
| 514 | /** |
||
| 515 | * @param Document $document |
||
| 516 | * @return FrontendUser |
||
| 517 | */ |
||
| 518 | public function getCreatorUser(Document $document) { |
||
| 519 | return $this->frontendUserRepository->findByUid($document->getCreator()); |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param Document $document |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | public function getAssignedUsers(Document $document) |
||
| 527 | { |
||
| 528 | $assignedUsers = []; |
||
| 529 | |||
| 530 | foreach ($document->getAssignedFobIdentifiers() as $fobId) { |
||
| 531 | $feUsers = $this->frontendUserRepository->findByFisPersId($fobId); |
||
| 532 | foreach ($feUsers as $feUser) { |
||
| 533 | |||
| 534 | $assignedUsers[$feUser->getUid()] = $feUser; |
||
| 535 | } |
||
| 536 | } |
||
| 537 | |||
| 538 | return $assignedUsers; |
||
| 539 | } |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @param Document $document |
||
| 543 | * @return array |
||
| 544 | */ |
||
| 545 | public function getNewlyAssignedUsers(Document $document) |
||
| 546 | { |
||
| 547 | $assignedUsers = []; |
||
| 548 | |||
| 549 | foreach ($document->getNewlyAssignedFobIdentifiers() as $fobId) { |
||
| 550 | $feUsers = $this->frontendUserRepository->findByFisPersId($fobId); |
||
| 551 | foreach ($feUsers as $feUser) { |
||
| 552 | $assignedUsers[$feUser->getUid()] = $feUser; |
||
| 553 | } |
||
| 554 | } |
||
| 555 | |||
| 556 | return $assignedUsers; |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @param Document $document |
||
| 561 | * @return array |
||
| 562 | */ |
||
| 563 | public function getDocumentBookmarkUsers(Document $document) { |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * @param Document $document |
||
| 579 | * @return array |
||
| 580 | */ |
||
| 581 | public function getUpdateNotificationRecipients(Document $document) |
||
| 582 | { |
||
| 677 | } |
||
| 678 | |||
| 679 | /** |
||
| 680 | * @param Document $document |
||
| 681 | * @return array |
||
| 682 | */ |
||
| 683 | public function getNewPublicationNotificationRecipients(Document $document) |
||
| 713 |