/**
  * @param Contract $streamContract
  * @param Identifier $streamId
  * @return EventEnvelope[]
  */
 public function fetch(Contract $streamContract, Identifier $streamId)
 {
     $records = $this->connection->fetchAll(Query\Select::from(self::TABLE_NAME), ['streamContract' => $streamContract, 'streamId' => $streamId]);
     $eventEnvelopes = array_map(function (array $record) {
         return EventEnvelope::reconstitute(EventId::fromString($record['eventId']), Contract::with($record['eventContract']), $record['eventPayload']);
     }, $records);
     return $eventEnvelopes;
 }
 /**
  * @test
  * @depends it_should_commit_and_fetch_events
  * @dataProvider providePersistence
  * @param Persistence $persistence
  */
 public function it_should_throw_when_events_have_been_committed_elsewhere(Persistence $persistence)
 {
     $commitId = CommitId::generate();
     $streamContract = Contract::with('My.Contract');
     $streamId = OrderId::generate();
     $eventEnvelope = EventEnvelope::wrap(EventId::generate(), Contract::with("My.SomethingHasHappened"), "My payload2");
     $this->setExpectedException(OptimisticConcurrencyFailed::class);
     $persistence->commit($commitId, $streamContract, $streamId, $expectedStreamVersion = 1, [$eventEnvelope]);
 }
 private function toEventEnvelope()
 {
     return function (InMemoryRecord $record) {
         return EventEnvelope::reconstitute($record->eventId, $record->eventContract, $record->eventPayload);
     };
 }
Exemplo n.º 4
0
 /**
  * @param Aggregate $aggregate
  */
 private function persistAggregate(Aggregate $aggregate)
 {
     $stream = $this->eventStore->createStream($aggregate->getAggregateContract(), $aggregate->getAggregateId());
     $domainEvents = $aggregate->getChanges();
     $wrapInEnvelope = function (DomainEvent $domainEvent) {
         $eventContract = $this->serializer->contractForDomainEvent($domainEvent);
         $payload = $this->serializer->serialize($eventContract, $domainEvent);
         return EventEnvelope::wrap(EventId::generate(), $eventContract, $payload);
     };
     $envelopes = $domainEvents->map($wrapInEnvelope);
     $stream->appendAll($envelopes);
     // @todo share commitId across aggregates
     $stream->commit(CommitId::generate());
     $aggregate->clearChanges();
 }