From 71ed85637e085d5ac4e8b434ee4b03cf0e2a9087 Mon Sep 17 00:00:00 2001 From: mhsdesign <85400359+mhsdesign@users.noreply.github.com> Date: Sat, 30 Mar 2024 11:57:23 +0100 Subject: [PATCH] TASK: #3887 set causationId and meta data for structure adjustments --- .../Classes/EventStore/EventsToPublish.php | 29 +++++++++++++++++++ .../src/StructureAdjustmentService.php | 10 ++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Neos.ContentRepository.Core/Classes/EventStore/EventsToPublish.php b/Neos.ContentRepository.Core/Classes/EventStore/EventsToPublish.php index 3dd0d2567dc..a4983b13d7d 100644 --- a/Neos.ContentRepository.Core/Classes/EventStore/EventsToPublish.php +++ b/Neos.ContentRepository.Core/Classes/EventStore/EventsToPublish.php @@ -7,6 +7,8 @@ use Neos\ContentRepository\Core\CommandHandler\CommandHandlerInterface; use Neos\ContentRepository\Core\ContentRepository; use Neos\EventStore\EventStoreInterface; +use Neos\EventStore\Model\Event; +use Neos\EventStore\Model\Event\EventMetadata; use Neos\EventStore\Model\Event\StreamName; use Neos\EventStore\Model\EventStream\ExpectedVersion; @@ -33,4 +35,31 @@ public static function empty(): self ExpectedVersion::ANY() ); } + + public function withCausationOfFirstEventAndAdditionalMetaData(EventMetadata $metadata): self + { + /** @var list $firstEvent */ + $restEvents = iterator_to_array($this->events); + if (empty($restEvents)) { + return $this; + } + $firstEvent = array_shift($restEvents); + + if ($firstEvent instanceof DecoratedEvent && $firstEvent->eventMetadata) { + $metadata = EventMetadata::fromArray(array_merge($firstEvent->eventMetadata->value, $metadata->value)); + } + + $decoratedFirstEvent = DecoratedEvent::create($firstEvent, eventId: Event\EventId::create(), metadata: $metadata); + + $decoratedRestEvents = []; + foreach ($restEvents as $event) { + $decoratedRestEvents[] = DecoratedEvent::create($event, causationId: $decoratedFirstEvent->eventId); + } + + return new EventsToPublish( + $this->streamName, + Events::fromArray([$decoratedFirstEvent, ...$decoratedRestEvents]), + $this->expectedVersion + ); + } } diff --git a/Neos.ContentRepository.StructureAdjustment/src/StructureAdjustmentService.php b/Neos.ContentRepository.StructureAdjustment/src/StructureAdjustmentService.php index 3c671758d58..50895dc7eca 100644 --- a/Neos.ContentRepository.StructureAdjustment/src/StructureAdjustmentService.php +++ b/Neos.ContentRepository.StructureAdjustment/src/StructureAdjustmentService.php @@ -20,6 +20,7 @@ use Neos\ContentRepository\StructureAdjustment\Adjustment\StructureAdjustment; use Neos\ContentRepository\StructureAdjustment\Adjustment\TetheredNodeAdjustments; use Neos\ContentRepository\StructureAdjustment\Adjustment\UnknownNodeTypeAdjustment; +use Neos\EventStore\Model\Event\EventMetadata; class StructureAdjustmentService implements ContentRepositoryServiceInterface { @@ -95,7 +96,14 @@ public function fixError(StructureAdjustment $adjustment): void $remediation = $adjustment->remediation; $eventsToPublish = $remediation(); assert($eventsToPublish instanceof EventsToPublish); - $this->eventPersister->publishEvents($eventsToPublish)->block(); + + $enrichedEventsToPublish = $eventsToPublish->withCausationOfFirstEventAndAdditionalMetaData( + EventMetadata::fromArray([ + 'structureAdjustment' => mb_strimwidth($adjustment->render() , 0, 250, '…') + ]) + ); + + $this->eventPersister->publishEvents($enrichedEventsToPublish); } } }