/** * @param AggregateType $repositoryAggregateType * @param string $aggregateId * @param Message[] $streamEvents * @param object $aggregateRoot * @throws Exception\InvalidArgumentException * @return void */ public function appendEvents(AggregateType $repositoryAggregateType, $aggregateId, array $streamEvents, $aggregateRoot) { $arType = AggregateType::fromAggregateRoot($aggregateRoot); if (!$repositoryAggregateType->equals($arType)) { throw new Exception\InvalidArgumentException(sprintf('aggregate root mismatch between repository type %s and object type %s', $repositoryAggregateType->toString(), $arType->toString())); } $this->eventStore->appendTo($this->buildStreamName($repositoryAggregateType, $aggregateId), $streamEvents); }
/** * @param AggregateType $repositoryAggregateType * @param string $aggregateId * @param Message[] $streamEvents * @param object $aggregateRoot * @return void */ public function appendEvents(AggregateType $repositoryAggregateType, $aggregateId, array $streamEvents, $aggregateRoot) { Assertion::string($aggregateId, 'AggregateId needs to be string'); foreach ($streamEvents as &$streamEvent) { $streamEvent = $streamEvent->withAddedMetadata('aggregate_id', $aggregateId); $streamEvent = $streamEvent->withAddedMetadata('aggregate_type', get_class($aggregateRoot)); } $this->eventStore->appendTo($this->streamName, $streamEvents); }
/** * @param AggregateType $repositoryAggregateType * @param string $aggregateId * @param Message[] $streamEvents * @param object $aggregateRoot * @throws Exception\InvalidArgumentException * @return void */ public function appendEvents(AggregateType $repositoryAggregateType, $aggregateId, array $streamEvents, $aggregateRoot) { $arType = AggregateType::fromAggregateRoot($aggregateRoot); if (!$repositoryAggregateType->equals($arType)) { throw new Exception\InvalidArgumentException(sprintf('aggregate root mismatch between repository type %s and object type %s', $repositoryAggregateType->toString(), $arType->toString())); } $streamName = $this->buildStreamName($repositoryAggregateType); Assertion::string($aggregateId, 'AggregateId needs to be string'); foreach ($streamEvents as &$streamEvent) { $streamEvent = $streamEvent->withAddedMetadata('aggregate_id', $aggregateId); } $this->eventStore->appendTo($streamName, $streamEvents); }
/** * @param object $eventSourcedAggregateRoot * @throws Exception\AggregateTypeException */ public function addAggregateRoot($eventSourcedAggregateRoot) { $this->aggregateType->assert($eventSourcedAggregateRoot); $domainEvents = $this->aggregateTranslator->extractPendingStreamEvents($eventSourcedAggregateRoot); $aggregateId = $this->aggregateTranslator->extractAggregateId($eventSourcedAggregateRoot); $streamName = $this->determineStreamName($aggregateId); $enrichedEvents = []; foreach ($domainEvents as $event) { $enrichedEvents[] = $this->enrichEventMetadata($event, $aggregateId); } if ($this->oneStreamPerAggregate) { $stream = new Stream($streamName, new ArrayIterator($enrichedEvents)); $this->eventStore->create($stream); } else { $this->eventStore->appendTo($streamName, new ArrayIterator($enrichedEvents)); } }
/** * @test */ public function it_publishes_take_snapshot_commands_by_event_name() { $this->eventStore->beginTransaction(); $user = User::create('Alex', '*****@*****.**'); $this->repository->addAggregateRoot($user); $this->eventStore->commit(); $this->eventStore->beginTransaction(); $user = $this->repository->getAggregateRoot($user->getId()->toString()); $user->changeName('Jim'); $this->eventStore->commit(); $this->eventStore->beginTransaction(); $eventWithoutMetadata1 = UsernameChanged::with(['new_name' => 'John Doe'], 5); $this->eventStore->appendTo(new StreamName('event_stream'), new \ArrayIterator([$eventWithoutMetadata1])); $this->eventStore->commit(); $this->assertCount(1, $this->result); $this->assertArrayHasKey('aggregate_type', $this->result[0]); $this->assertArrayHasKey('aggregate_id', $this->result[0]); $this->assertEquals(User::class, $this->result[0]['aggregate_type']); }
*/ require __DIR__ . '/../vendor/autoload.php'; use Prooph\Common\Event\ProophActionEventEmitter; use Prooph\Common\Messaging\FQCNMessageFactory; use Prooph\Common\Messaging\NoOpMessageConverter; use Prooph\EventStore\Adapter\Flywheel\FlywheelEventStoreAdapter; use Prooph\EventStore\EventStore; use Prooph\EventStore\Stream\Stream; use Prooph\EventStore\Stream\StreamName; use ProophTest\EventStore\Mock\UserCreated; use ProophTest\EventStore\Mock\UsernameChanged; $rootDir = __DIR__ . '/event_store'; $adapter = new FlywheelEventStoreAdapter($rootDir, new FQCNMessageFactory(), new NoOpMessageConverter()); $actionEmitter = new ProophActionEventEmitter(); $eventStore = new EventStore($adapter, $actionEmitter); $streamName = new StreamName('event_stream'); $stream = new Stream($streamName, new \ArrayIterator([])); // // Persist some events in the event store // $eventStore->beginTransaction(); $eventStore->create($stream); $eventStore->appendTo($streamName, new \ArrayIterator([UserCreated::with(['name' => 'Max Mustermann'], 1)->withAddedMetadata('tag', 'person'), UsernameChanged::with(['name' => 'John Doe'], 2)->withAddedMetadata('tag', 'person')])); $eventStore->commit(); // // Load all the stored events // $persistedEventStream = $eventStore->load($streamName); foreach ($persistedEventStream->streamEvents() as $event) { echo $event->payload()['name'] . PHP_EOL; }