Skip to content

Commit

Permalink
TASK: neos#3887 set causationId and meta data for structure adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsdesign committed May 13, 2024
1 parent 96ce9e9 commit 71ed856
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Neos.ContentRepository.Core/Classes/EventStore/EventsToPublish.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,4 +35,31 @@ public static function empty(): self
ExpectedVersion::ANY()
);
}

public function withCausationOfFirstEventAndAdditionalMetaData(EventMetadata $metadata): self
{
/** @var list<EventInterface|DecoratedEvent> $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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 71ed856

Please sign in to comment.