Skip to content

Commit

Permalink
NGSTACK-811 add transaction inside loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Marijan Klaric committed Jul 23, 2024
1 parent efdb0cc commit 872ee9f
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions bundle/Command/TagContentByTypesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;

use function array_filter;
use function array_map;
Expand Down Expand Up @@ -72,8 +73,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->style->newLine();
$this->style->progressStart($totalResults);

Check failure on line 74 in bundle/Command/TagContentByTypesCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $max of method Symfony\Component\Console\Style\SymfonyStyle::progressStart() expects int, int|null given.

$this->repository->beginTransaction();

if ($this->input->getOption('field-identifiers') === null) {
$fieldIdentifiers = $this->configResolver->getParameter('tag_command_default_field_identifiers', 'ngsite');
} else {
Expand All @@ -85,6 +84,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$query->limit = $batchSize;
$searchResults = $this->repository->getSearchService()->findContent($query);

$this->repository->beginTransaction();

foreach ($searchResults->searchHits as $searchHit) {
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\Content $content */
$content = $searchHit->valueObject;
Expand All @@ -94,36 +95,48 @@ protected function execute(InputInterface $input, OutputInterface $output): int
continue;
}

if (!$content->getField($fieldIdentifier)->value instanceof TagFieldValue) {
$this->style->error(sprintf('Field with identifier %s must be a type of eztags', $fieldIdentifier));
try {
if (!$content->getField($fieldIdentifier)->value instanceof TagFieldValue) {

Check failure on line 99 in bundle/Command/TagContentByTypesCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Cannot access property $value on Ibexa\Contracts\Core\Repository\Values\Content\Field|null.
$this->style->error(sprintf('Field with identifier %s must be a type of eztags', $fieldIdentifier));

return Command::FAILURE;
}
$this->repository->rollback();

$alreadyAssignedTags = $content->getFieldValue($fieldIdentifier)->tags;
$tag = $this->getTag();
$tagsToAssign = array_filter($alreadyAssignedTags, fn ($alreadyAssignedTag) => $tag->id !== $alreadyAssignedTag->id);
$tagsToAssign[] = $tag;
return Command::FAILURE;
}

$this->repository->sudo(
function () use ($content, $fieldIdentifier, $tagsToAssign): void {
$contentDraft = $this->repository->getContentService()->createContentDraft($content->contentInfo);
$contentUpdateStruct = $this->repository->getContentService()->newContentUpdateStruct();
$alreadyAssignedTags = $content->getFieldValue($fieldIdentifier)->tags;
$tag = $this->getTag();
$tagsToAssign = array_filter($alreadyAssignedTags, fn ($alreadyAssignedTag) => $tag->id !== $alreadyAssignedTag->id);
$tagsToAssign[] = $tag;

$contentUpdateStruct->setField($fieldIdentifier, new TagFieldValue($tagsToAssign));
$this->repository->sudo(
function () use ($content, $fieldIdentifier, $tagsToAssign): void {
$contentDraft = $this->repository->getContentService()->createContentDraft($content->contentInfo);
$contentUpdateStruct = $this->repository->getContentService()->newContentUpdateStruct();

$this->repository->getContentService()->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
$this->repository->getContentService()->publishVersion($contentDraft->versionInfo);
}
);
break;
$contentUpdateStruct->setField($fieldIdentifier, new TagFieldValue($tagsToAssign));

$this->repository->getContentService()->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
$this->repository->getContentService()->publishVersion($contentDraft->versionInfo);
}
);

break;

} catch (Throwable $t) {
$this->style->error($t->getMessage());

$this->repository->rollback();

continue;
}
}

$this->style->progressAdvance();
}
}

$this->repository->commit();
$this->repository->commit();
}

$this->style->progressFinish();
$this->style->success('Tags assigned successfully');
Expand Down

0 comments on commit 872ee9f

Please sign in to comment.