/** * @return void */ public function run() { $logger = new Logger('default'); $sagaId = Identity::createNew(); $toDoSaga = new ToDoSaga($sagaId, new AssociationValues([])); $eventBus = new SimpleEventBus([ToDoItemCreated::class => [$toDoSaga], ToDoItemDone::class => [$toDoSaga], DeadlineExpired::class => [$toDoSaga]]); $eventBus->setLogger(new EventLogger($logger)); $scheduler = new SimpleEventScheduler($eventBus); $toDoSaga->setEventScheduler($scheduler); $eventStore = new EventStore($eventBus, new MemoryEventStorage(), new JsonSerializer(), new EventClassMap([ToDoItemCreated::class, ToDoItemDone::class, DeadlineExpired::class])); $factory = new GenericAggregateFactory(ToDoItem::class); $repository = new EventSourcingRepository($factory, $eventStore); $commandHandler = new ToDoCommandHandler($repository); $commandBus = new SimpleCommandBus([CreateToDoItem::class => $commandHandler, MarkItemDone::class => $commandHandler]); $loggingCommandInterceptor = new LoggingInterceptor(new CommandLogger($logger)); $commandGateway = new DefaultCommandGateway($commandBus, [$loggingCommandInterceptor]); // Send commands to create two todoitems $todoItemExpireSeconds = 3; $toCompleteId = Identity::createNew(); $toExpireId = Identity::createNew(); $commandGateway->send(new CreateToDoItem($toCompleteId, "Item to complete", $todoItemExpireSeconds)); $commandGateway->send(new CreateToDoItem($toExpireId, "Item to expire", $todoItemExpireSeconds)); // Start an idling process for 5 seconds and wait for the ToDoItem to expire $this->idle(5, $commandGateway, $toCompleteId); }
/** * @return void */ public function run() { $logger = new Logger('default'); $sagaFactory = new GenericSagaFactory(); $sagaStorage = new MemorySagaStorage(); $sagaRepository = new SagaRepository($sagaStorage, new JsonSerializer()); $resolver = new SimpleAssociationValueResolver(); $sagaManager = new SimpleSagaManager([ToDoSaga::class], $sagaRepository, $resolver, $sagaFactory); $eventBus = new SimpleEventBus([]); $eventBus->setLogger(new EventLogger($logger)); $eventBus->addHandler(Event::class, $sagaManager); $eventStore = new EventStore($eventBus, new MemoryEventStorage(), new JsonSerializer(), new EventClassMap([ToDoItemCreated::class, ToDoItemDone::class, DeadlineExpired::class])); $factory = new GenericAggregateFactory(ToDoItem::class); $repository = new EventSourcingRepository($factory, $eventStore); $commandHandler = new ToDoCommandHandler($repository); $commandBus = new SimpleCommandBus([CreateToDoItem::class => $commandHandler, MarkItemDone::class => $commandHandler]); $loggingCommandInterceptor = new LoggingInterceptor(new CommandLogger($logger)); $commandGateway = new DefaultCommandGateway($commandBus, [$loggingCommandInterceptor]); $toCompleteId = Identity::createNew(); $commandGateway->send(new CreateToDoItem($toCompleteId, "Item to complete", PHP_INT_MAX)); $sagaIds = $sagaRepository->find(ToDoSaga::class, new AssociationValue('identity', $toCompleteId->getValue())); $logger->debug("Active sagas found:", ['count' => count($sagaIds), 'saga' => !empty($sagaIds) ? $sagaStorage->findById($sagaIds[0]->getValue()) : '']); $commandGateway->send(new MarkItemDone($toCompleteId)); $sagaIds = $sagaRepository->find(ToDoSaga::class, new AssociationValue('identity', $toCompleteId->getValue())); $logger->debug("Active sagas found:", ['count' => count($sagaIds), 'saga' => !empty($sagaIds) ? $sagaStorage->findById($sagaIds[0]->getValue())['serialized'] : '']); }
/** * @return void */ public function run() { $logger = new Logger('default'); // The storage device to store event data $eventStorage = new MemoryEventStorage(); // A new event bus with a mapping to specify what handlers to call for what event. $eventBus = new SimpleEventBus([UserCreated::class => [new UserCreatedHandler($eventStorage, $logger)]]); $eventBus->setLogger(new EventLogger($logger)); // An event store to store events $eventStore = new EventStore($eventBus, $eventStorage, new JsonSerializer(), new EventClassMap([UserCreated::class])); // A basic aggregate factory to be able to reconstruct the aggregate $factory = new GenericAggregateFactory(User::class); // A repository for objects of type Aggregate $repository = new EventSourcingRepository($factory, $eventStore); // A new command bus with a mapping to specify what handler to call for what command. $commandBus = new SimpleCommandBus([CreateUser::class => new CreateUserHandler($repository, $logger)]); $loggingCommandInterceptor = new LoggingInterceptor(new CommandLogger($logger)); $commandGateway = new DefaultCommandGateway($commandBus, [$loggingCommandInterceptor]); // Send the command $commandGateway->send(new CreateUser(Identity::createNew())); }
/** * @return void */ public function run() { $logger = new Logger('default'); $eventBus = new SimpleEventBus(); $eventBus->setLogger(new EventLogger($logger)); $scheduler = new SimpleEventScheduler($eventBus); $parameterResolver = new DefaultParameterResolver(); $factory = new ToDoSagaFactory($scheduler, $parameterResolver, $logger); $serializer = new JsonSerializer(); $sagaManager = new SimpleSagaManager([ToDoSaga::class], new SagaRepository(new MemorySagaStorage(), new SagaSerializer($serializer, $factory)), new SimpleAssociationValueResolver(), $factory); $eventBus->addHandler(Event::class, $sagaManager); $eventStore = new EventStore($eventBus, new MemoryEventStorage(), $serializer, new EventClassMap([ToDoItemCreated::class, ToDoItemDone::class, DeadlineExpired::class])); $commandHandler = new ToDoCommandHandler(new EventSourcingRepository(new GenericAggregateFactory(ToDoItem::class), $eventStore)); $commandBus = new SimpleCommandBus([CreateToDoItem::class => $commandHandler, MarkItemDone::class => $commandHandler]); $commandGateway = new DefaultCommandGateway($commandBus, [new LoggingInterceptor(new CommandLogger($logger))]); // Send commands to create two todoitems $todoItemExpireSeconds = 3; $toCompleteId = Identity::createNew(); $commandGateway->send(new CreateToDoItem($toCompleteId, "Item to complete", $todoItemExpireSeconds)); $commandGateway->send(new CreateToDoItem(Identity::createNew(), "Item to expire", $todoItemExpireSeconds)); // Start an idling process for 5 seconds and wait for the ToDoItem to expire $this->idle(5, $commandGateway, $toCompleteId); }
/** * @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()))]); }
/** * @test */ public function callLoggerForFailedDispatch() { $event = $this->getMockBuilder(Event::class)->getMock(); $logger = $this->getMockBuilder(EventLogger::class)->disableOriginalConstructor()->getMock(); $logger->expects(self::once())->method('onDispatchFailed')->with($event); $eventBus = new SimpleEventBus([]); $eventBus->setLogger($logger); $eventBus->publish($event); }