Skip to content

Commit

Permalink
[MAINTENANCE] Improvements in BaseCommand class (#1130)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Meyer <[email protected]>
  • Loading branch information
beatrycze-volk and sebastian-meyer committed Jan 29, 2024
1 parent 1336b0a commit 815b50f
Showing 1 changed file with 68 additions and 44 deletions.
112 changes: 68 additions & 44 deletions Classes/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ class BaseCommand extends Command
*/
protected ConfigurationManager $configurationManager;

public function __construct(CollectionRepository $collectionRepository,
DocumentRepository $documentRepository,
LibraryRepository $libraryRepository,
StructureRepository $structureRepository,
ConfigurationManager $configurationManager)
{
public function __construct(
CollectionRepository $collectionRepository,
DocumentRepository $documentRepository,
LibraryRepository $libraryRepository,
StructureRepository $structureRepository,
ConfigurationManager $configurationManager
) {
parent::__construct();

$this->collectionRepository = $collectionRepository;
Expand Down Expand Up @@ -226,24 +227,7 @@ protected function saveToDatabase(Document $document): bool
$document->setStructure($structure);

if (is_array($metadata['collection'])) {
foreach ($metadata['collection'] as $collection) {
$documentCollection = $this->collectionRepository->findOneByIndexName($collection);
if (!$documentCollection) {
// create new Collection object
$documentCollection = GeneralUtility::makeInstance(Collection::class);
$documentCollection->setIndexName($collection);
$documentCollection->setLabel($collection);
$documentCollection->setOaiName((!empty($this->extConf['publishNewCollections']) ? Helper::getCleanString($collection) : ''));
$documentCollection->setIndexSearch('');
$documentCollection->setDescription('');
// add to CollectionRepository
$this->collectionRepository->add($documentCollection);
// persist collection to prevent duplicates
$persistenceManager->persistAll();
}
// add to document
$document->addCollection($documentCollection);
}
$this->addCollections($document, $metadata['collection'], $persistenceManager);
}

// set identifiers
Expand All @@ -264,25 +248,8 @@ protected function saveToDatabase(Document $document): bool
$document->setRightsInfo($metadata['rights_info'][0] ? : '');
$document->setStatus(0);

if ($this->owner) {
// library / owner is set by parameter --> take it.
$document->setOwner($this->owner);
} else {
// owner is not set set but found by metadata --> take it or take default library
$owner = $metadata['owner'][0] ? : 'default';
$this->owner = $this->libraryRepository->findOneByIndexName($owner);
if ($this->owner) {
$document->setOwner($this->owner);
} else {
// create library
$this->owner = GeneralUtility::makeInstance(Library::class);

$this->owner->setLabel($owner);
$this->owner->setIndexName($owner);
$this->libraryRepository->add($this->owner);
$document->setOwner($this->owner);
}
}
$this->setOwner($metadata['owner'][0]);
$document->setOwner($this->owner);

// set volume data
$document->setVolume($metadata['volume'][0] ? : '');
Expand Down Expand Up @@ -311,7 +278,7 @@ protected function saveToDatabase(Document $document): bool
* Currently only applies to METS documents.
*
* @access protected
*
*
* @param Document $document for which parent UID should be taken
*
* @return int The parent document's id.
Expand Down Expand Up @@ -350,4 +317,61 @@ protected function getParentDocumentUidForSaving(Document $document): int
return 0;
}

/**
* Add collections.
*
* @access private
*
* @param Document &$document
* @param array $collections
* @param PersistenceManager $persistenceManager
*
* @return void
*/
private function addCollections(Document &$document, array $collections, PersistenceManager $persistenceManager): void
{
foreach ($collections as $collection) {
$documentCollection = $this->collectionRepository->findOneByIndexName($collection);
if (!$documentCollection) {
// create new Collection object
$documentCollection = GeneralUtility::makeInstance(Collection::class);
$documentCollection->setIndexName($collection);
$documentCollection->setLabel($collection);
$documentCollection->setOaiName((!empty($this->extConf['publishNewCollections']) ? Helper::getCleanString($collection) : ''));
$documentCollection->setIndexSearch('');
$documentCollection->setDescription('');
// add to CollectionRepository
$this->collectionRepository->add($documentCollection);
// persist collection to prevent duplicates
$persistenceManager->persistAll();
}
// add to document
$document->addCollection($documentCollection);
}
}

/**
* If owner is not set set but found by metadata, take it or take default library, if nothing found in database then create new owner.
*
* @access private
*
* @param ?string $owner
*
* @return void
*/
private function setOwner($owner): void
{
if (empty($this->owner)) {
// owner is not set set but found by metadata --> take it or take default library
$owner = $owner ? : 'default';
$this->owner = $this->libraryRepository->findOneByIndexName($owner);
if (empty($this->owner)) {
// create library
$this->owner = GeneralUtility::makeInstance(Library::class);
$this->owner->setLabel($owner);
$this->owner->setIndexName($owner);
$this->libraryRepository->add($this->owner);
}
}
}
}

0 comments on commit 815b50f

Please sign in to comment.