/** * @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'] : '']); }
/** * @test */ public function sendNotifiesInterceptorsOfFailedDispatch() { $interceptor = $this->getMockBuilder(CommandDispatchInterceptor::class)->getMock(); $commandBus = $this->getMockBuilder(CommandBus::class)->getMock(); $command = $this->getMockBuilder(Command::class)->getMock(); $exception = new NoCommandHandlerException($command); $commandBus->expects(self::once())->method('send')->with($command)->willThrowException($exception); $interceptor->expects(self::once())->method('onDispatchFailed')->with($command, $exception); $gateway = new DefaultCommandGateway($commandBus, [$interceptor]); $gateway->send($command); }
/** * @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()))]); }
/** * @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); }