Created
October 19, 2021 15:25
-
-
Save arfaram/130cf1d1316af37f867befbb54ff797d to your computer and use it in GitHub Desktop.
Patch to support Ibexa v3.3.3 migration bundle to add actions on content update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/bundle/Resources/config/services/step_executors.yaml b/src/bundle/Resources/config/services/step_executors.yaml | |
index 206aa87..571f083 100644 | |
--- a/src/bundle/Resources/config/services/step_executors.yaml | |
+++ b/src/bundle/Resources/config/services/step_executors.yaml | |
@@ -42,6 +42,8 @@ services: | |
$defaultUserLogin: '%ibexa.migrations.default.user_login%' | |
Ibexa\Platform\Migration\StepExecutor\ContentUpdateStepExecutor: | |
+ arguments: | |
+ $actionExecutor: '@Ibexa\Platform\Migration\StepExecutor\ActionExecutor\Content\Create\Executor' | |
Ibexa\Platform\Migration\StepExecutor\LanguageCreateStepExecutor: | |
Ibexa\Platform\Migration\StepExecutor\ReferenceLoadStepExecutor: | |
Ibexa\Platform\Migration\StepExecutor\ReferenceSaveStepExecutor: | |
diff --git a/src/bundle/Serializer/Normalizer/Step/ContentUpdateStepNormalizer.php b/src/bundle/Serializer/Normalizer/Step/ContentUpdateStepNormalizer.php | |
index 011660a..239b188 100644 | |
--- a/src/bundle/Serializer/Normalizer/Step/ContentUpdateStepNormalizer.php | |
+++ b/src/bundle/Serializer/Normalizer/Step/ContentUpdateStepNormalizer.php | |
@@ -85,10 +85,21 @@ final class ContentUpdateStepNormalizer implements StepNormalizerInterface, Deno | |
/** @var \Ibexa\Platform\Migration\ValueObject\Content\Field[] $fields */ | |
$fields = $this->denormalizer->denormalize($data['fields'] ?? [], Field::class . '[]', $format, $context); | |
+ $actions = []; | |
+ if (array_key_exists('actions', $data)) { | |
+ /** @var ValueObject\Step\Action[] $actions */ | |
+ $actions = $this->denormalizer->denormalize( | |
+ $data['actions'], | |
+ ValueObject\Step\Action::class . '[]', | |
+ $format, | |
+ $context | |
+ ); | |
+ } | |
return new ContentUpdateStep( | |
UpdateMetadata::createFromArray($data['metadata'] ?? []), | |
$match, | |
$fields, | |
+ $actions | |
); | |
} | |
diff --git a/src/lib/StepExecutor/ContentCreateStepExecutor.php b/src/lib/StepExecutor/ContentCreateStepExecutor.php | |
index 6b926b6..1a7bf3e 100644 | |
--- a/src/lib/StepExecutor/ContentCreateStepExecutor.php | |
+++ b/src/lib/StepExecutor/ContentCreateStepExecutor.php | |
@@ -89,6 +89,7 @@ final class ContentCreateStepExecutor implements StepExecutorInterface, UserCont | |
public function handle(StepInterface $step): void | |
{ | |
$actions = $step->actions; | |
+ | |
Assert::allIsInstanceOf($actions, ValueObject\Step\Action::class); | |
$contentType = $this->contentTypeService->loadContentTypeByIdentifier($step->metadata->contentType); | |
@@ -98,6 +99,7 @@ final class ContentCreateStepExecutor implements StepExecutorInterface, UserCont | |
$createContentStruct->ownerId = $step->metadata->creatorId; | |
$createContentStruct->alwaysAvailable = $step->metadata->alwaysAvailable; | |
$createContentStruct->modificationDate = $step->metadata->modificationDate; | |
+ $createContentStruct->sectionId = $step->metadata->section; | |
$locationParentId = $this->getLocationParentId($step->location); | |
diff --git a/src/lib/StepExecutor/ContentUpdateStepExecutor.php b/src/lib/StepExecutor/ContentUpdateStepExecutor.php | |
index 5dba723..202d588 100644 | |
--- a/src/lib/StepExecutor/ContentUpdateStepExecutor.php | |
+++ b/src/lib/StepExecutor/ContentUpdateStepExecutor.php | |
@@ -11,16 +11,20 @@ namespace Ibexa\Platform\Migration\StepExecutor; | |
use Exception; | |
use eZ\Publish\API\Repository\ContentService; | |
use eZ\Publish\API\Repository\SearchService; | |
+use eZ\Publish\API\Repository\Values\Content\Content; | |
use eZ\Publish\API\Repository\Values\Content\Query; | |
use eZ\Publish\SPI\Persistence\TransactionHandler; | |
use Ibexa\Platform\Migration\Generator\Content\CriterionFactory; | |
use Ibexa\Platform\Migration\Service\FieldTypeServiceInterface; | |
+use Ibexa\Platform\Migration\StepExecutor\ActionExecutor\ExecutorInterface; | |
use Ibexa\Platform\Migration\ValueObject\Step\ContentUpdateStep; | |
use Ibexa\Platform\Migration\ValueObject\Step\StepInterface; | |
use Psr\Log\LoggerAwareInterface; | |
use Psr\Log\LoggerAwareTrait; | |
use Psr\Log\LoggerInterface; | |
use Psr\Log\NullLogger; | |
+use Webmozart\Assert\Assert; | |
+use Ibexa\Platform\Migration\ValueObject; | |
final class ContentUpdateStepExecutor implements StepExecutorInterface, LoggerAwareInterface | |
{ | |
@@ -41,13 +45,17 @@ final class ContentUpdateStepExecutor implements StepExecutorInterface, LoggerAw | |
/** @var \Ibexa\Platform\Migration\Service\FieldTypeServiceInterface */ | |
private $fieldTypeService; | |
+ /** @var ExecutorInterface*/ | |
+ private $actionExecutor; | |
+ | |
public function __construct( | |
TransactionHandler $transactionHandler, | |
ContentService $contentService, | |
SearchService $searchService, | |
FieldTypeServiceInterface $fieldTypeService, | |
CriterionFactory $criterionFactory, | |
- ?LoggerInterface $logger = null | |
+ ?LoggerInterface $logger = null, | |
+ ExecutorInterface $actionExecutor | |
) { | |
$this->transactionHandler = $transactionHandler; | |
$this->contentService = $contentService; | |
@@ -55,6 +63,7 @@ final class ContentUpdateStepExecutor implements StepExecutorInterface, LoggerAw | |
$this->fieldTypeService = $fieldTypeService; | |
$this->criterionFactory = $criterionFactory; | |
$this->logger = $logger ?? new NullLogger(); | |
+ $this->actionExecutor = $actionExecutor; | |
} | |
public function canHandle(StepInterface $step): bool | |
@@ -67,6 +76,10 @@ final class ContentUpdateStepExecutor implements StepExecutorInterface, LoggerAw | |
*/ | |
public function handle(StepInterface $step): void | |
{ | |
+ $actions = $step->actions; | |
+ | |
+ Assert::allIsInstanceOf($actions, ValueObject\Step\Action::class); | |
+ | |
$query = new Query(); | |
$query->query = new Query\Criterion\LogicalAnd([ | |
$this->criterionFactory->build($step->match->field, (array) $step->match->value), | |
@@ -75,7 +88,7 @@ final class ContentUpdateStepExecutor implements StepExecutorInterface, LoggerAw | |
$searchResult = $this->searchService->findContent($query); | |
$this->transactionHandler->beginTransaction(); | |
- | |
+ | |
try { | |
/** @var \eZ\Publish\API\Repository\Values\Content\Search\SearchHit $searchHit */ | |
foreach ($searchResult as $searchHit) { | |
@@ -115,7 +128,9 @@ final class ContentUpdateStepExecutor implements StepExecutorInterface, LoggerAw | |
} | |
$contentDraft = $this->contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct); | |
- $this->contentService->publishVersion($contentDraft->versionInfo); | |
+ $content = $this->contentService->publishVersion($contentDraft->versionInfo); | |
+ | |
+ $this->handleActions($actions, $content); | |
$this->logger->notice(sprintf( | |
'Updated content "%s" (ID: %s) of type "%s', | |
@@ -131,4 +146,15 @@ final class ContentUpdateStepExecutor implements StepExecutorInterface, LoggerAw | |
throw $e; | |
} | |
} | |
+ | |
+ | |
+ /** | |
+ * @param array<\Ibexa\Platform\Migration\ValueObject\Step\Action> $actions | |
+ */ | |
+ private function handleActions(array $actions, Content $content): void | |
+ { | |
+ foreach ($actions as $action) { | |
+ $this->actionExecutor->handle($action, $content); | |
+ } | |
+ } | |
} | |
diff --git a/src/lib/ValueObject/Content/CreateMetadata.php b/src/lib/ValueObject/Content/CreateMetadata.php | |
index 3bd2e00..393a7e7 100644 | |
--- a/src/lib/ValueObject/Content/CreateMetadata.php | |
+++ b/src/lib/ValueObject/Content/CreateMetadata.php | |
@@ -89,7 +89,7 @@ final class CreateMetadata | |
isset($data['modificationDate']) ? new DateTime($data['modificationDate']) : null, | |
isset($data['publicationDate']) ? new DateTime($data['publicationDate']) : null, | |
$data['remoteId'] ?? null, | |
- $data['alwaysAvailable'] ?? null, | |
+ $data['defaultAlwaysAvailable'] ?? null, | |
$data['section'] ?? null, | |
); | |
} | |
diff --git a/src/lib/ValueObject/Content/UpdateMetadata.php b/src/lib/ValueObject/Content/UpdateMetadata.php | |
index 509194a..b9445c3 100644 | |
--- a/src/lib/ValueObject/Content/UpdateMetadata.php | |
+++ b/src/lib/ValueObject/Content/UpdateMetadata.php | |
@@ -91,12 +91,13 @@ final class UpdateMetadata | |
public static function createFromArray(array $data): self | |
{ | |
+ | |
return new self( | |
- $data['initialLanguageCode'] ?? null, | |
- $data['creatorId'] ?? null, | |
+ $data['initialLanguageCode'] ?? $data['mainTranslation'], | |
+ $data['creatorId'] ?? $data['modifierId'], | |
$data['remoteId'] ?? null, | |
- $data['alwaysAvailable'] ?? null, | |
- $data['mainLanguageCode'] ?? null, | |
+ $data['alwaysAvailable'] ?? $data['defaultAlwaysAvailable'], | |
+ $data['mainLanguageCode'] ?? $data['mainTranslation'], | |
$data['mainLocationId'] ?? null, | |
isset($data['modificationDate']) ? new DateTime($data['modificationDate']) : null, | |
$data['name'] ?? null, | |
diff --git a/src/lib/ValueObject/Step/ContentUpdateStep.php b/src/lib/ValueObject/Step/ContentUpdateStep.php | |
index 01a465e..2854b57 100644 | |
--- a/src/lib/ValueObject/Step/ContentUpdateStep.php | |
+++ b/src/lib/ValueObject/Step/ContentUpdateStep.php | |
@@ -24,15 +24,21 @@ final class ContentUpdateStep implements StepInterface | |
/** @var \Ibexa\Platform\Migration\ValueObject\Content\Field[] */ | |
public $fields; | |
+ /** @var \Ibexa\Platform\Migration\ValueObject\Step\Action[] */ | |
+ public $actions; | |
+ | |
/** | |
* @param \Ibexa\Platform\Migration\ValueObject\Content\Field[] $fields | |
+ * @param \Ibexa\Platform\Migration\ValueObject\Step\Action[] $actions | |
*/ | |
- public function __construct(UpdateMetadata $metadata, Match $match, iterable $fields) | |
+ public function __construct(UpdateMetadata $metadata, Match $match, iterable $fields, ?array $actions = []) | |
{ | |
+ Assert::allIsInstanceOf($actions, Action::class); | |
Assert::allIsInstanceOf($fields, Field::class); | |
$this->metadata = $metadata; | |
$this->match = $match; | |
$this->fields = $fields; | |
+ $this->actions = $actions; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment