Exemplo n.º 1
0
 /**
  * @return void
  */
 public function run()
 {
     $logger = new Logger('default');
     $serializer = new JsonSerializer();
     $stateStorage = new MemoryStateStorage();
     $toDoItemProjections = new ToDoItemProjections($stateStorage);
     $eventStore = new EventStore(new SimpleEventBus([ToDoItemCreated::class => [$toDoItemProjections], ToDoItemDone::class => [$toDoItemProjections], DeadlineExpired::class => [$toDoItemProjections]]), new MemoryEventStorage(), $serializer, new EventClassMap([ToDoItemCreated::class, ToDoItemDone::class, DeadlineExpired::class]));
     $repository = new EventSourcingRepository(new GenericAggregateFactory(ToDoItem::class), $eventStore);
     $toDoItemCommandHandler = new ToDoCommandHandler($repository);
     $commandGateWay = new DefaultCommandGateway(new SimpleCommandBus([CreateToDoItem::class => $toDoItemCommandHandler, MarkItemDone::class => $toDoItemCommandHandler]), [new LoggingInterceptor(new CommandLogger($logger))]);
     $toDoId = Identity::createNew();
     $commandGateWay->send(new CreateToDoItem($toDoId, "Wash the dishes", 5));
     $logger->debug("Current state of ToDoItem", ['item' => $serializer->serialize($stateStorage->find($toDoId->getValue()))]);
     $commandGateWay->send(new MarkItemDone($toDoId));
     $logger->debug("Current state of ToDoItem", ['item' => $serializer->serialize($stateStorage->find($toDoId->getValue()))]);
 }
Exemplo n.º 2
0
 /**
  * @return void
  */
 public function run()
 {
     // A logger
     $logger = new Logger('default');
     // A serializer
     $serializer = new JsonSerializer();
     // An event storage
     $eventStorage = new MemoryEventStorage();
     // A state storage
     $stateStorage = new MemoryStateStorage();
     // An event bus with an event handler bound to the DemonstratedEvent
     $eventBus = new SimpleEventBus([CreatedEvent::class => [new DemonstratedEventHandler($stateStorage)], DemonstratedEvent::class => [new DemonstratedEventHandler($stateStorage)]]);
     $eventBus->setLogger(new EventLogger($logger));
     // The event store
     $eventStore = new EventStore($eventBus, $eventStorage, $serializer, new EventClassMap([CreatedEvent::class, DemonstratedEvent::class]));
     $identity = Identity::createNew();
     // Build a series of events for an aggregate to mock initial state.
     $this->prepareSomeEvents($identity, $eventStorage, $serializer);
     // Use the EventPlayer to replay the events for an Aggregate.
     $eventPlayer = new AggregateEventPlayer($eventBus, $eventStore);
     $eventPlayer->replayEventsByAggregateId($identity);
     // Inspect the current aggregate state
     $logger->debug('Current aggregate state', ['aggregate' => (new ArrayConverter())->objectToArray($stateStorage->find($identity->getValue()))]);
 }
Exemplo n.º 3
0
 /**
  * @test
  */
 public function findAllRetrievesEmptyArrayIfNoneFound()
 {
     $storage = new MemoryStateStorage();
     self::assertEmpty($storage->findAll());
 }