Ejemplo n.º 1
0
 /**
  * @return JsonModel
  */
 public function getList()
 {
     $previousEventId = $this->params()->fromQuery('previousEventId');
     $count = $this->params()->fromQuery('count', 10);
     if ($previousEventId) {
         $previousEventId = Uuid::fromString($previousEventId);
     }
     $iterator = $this->eventStore->iterate($previousEventId);
     $selfUrl = $this->url()->fromRoute(null, [], ['force_canonical' => true], true);
     $nextUrl = false;
     $lastEventId = $previousEventId;
     $events = [];
     $i = 0;
     /** @var EventMessageInterface $event */
     foreach ($iterator as $event) {
         if ($i >= $count) {
             break;
         }
         $events[] = $event;
         $i++;
         $lastEventId = $event->getId()->toString();
     }
     $nextUrl = $this->url()->fromRoute(null, [], ['force_canonical' => true, 'query' => ['previousEventId' => (string) $lastEventId]], true);
     $data = ['_links' => ['self' => $selfUrl, 'next' => $nextUrl], 'count' => count($events), '_embedded' => ['event' => array_values($events)]];
     return new JsonModel($data);
 }
Ejemplo n.º 2
0
 /**
  * @return Generator
  */
 public function getIterator()
 {
     $eventIterator = $this->eventStore->iterate($this->lastEventId);
     /** @var EventMessageInterface $event */
     foreach ($eventIterator as $event) {
         $this->lastEventId = $event->getId();
         (yield $event);
     }
 }
Ejemplo n.º 3
0
 /**
  * @param EventMessageInterface[] $events
  */
 protected function dispatchEvents(array $events)
 {
     foreach ($events as $event) {
         if ($this->eventStore) {
             $this->eventStore->store($event);
         }
         $this->eventBus->publish($event);
     }
 }
Ejemplo n.º 4
0
 /**
  * @param  AggregateRootInterface $aggregate
  * @return void
  */
 public function save(AggregateRootInterface $aggregate)
 {
     /** @var Uuid $id */
     $id = $aggregate->getId();
     /** @var array $events */
     $events = $aggregate->pullUncommittedEvents();
     try {
         /** @var EventStream $stream */
         $stream = $this->eventStore->get($id);
         $stream->addEvents($events);
     } catch (EventStreamNotFoundException $e) {
         $stream = new EventStream($aggregate->getId(), get_class($aggregate), $events, 1);
     }
     $this->eventStore->commit($stream);
     /** @var EventInterface $event */
     foreach ($events as $event) {
         $this->eventBus->handle($event);
     }
 }
Ejemplo n.º 5
0
 /**
  * @param null|UuidInterface $previousEventId
  * @return Traversable
  */
 public function iterate(UuidInterface $previousEventId = null)
 {
     return $this->eventStore->iterate($previousEventId);
 }