/** * Initializes a new given-when-then style test fixture for the given <code>aggregateType</code>. * * @param string $aggregateType The aggregate to initialize the test fixture for */ public function __construct($aggregateType) { $this->logger = new Logger('fixture'); $this->logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG)); $this->eventBus = new RecordingEventBus($this->publishedEvents); $this->commandBus = new SimpleCommandBus(new DefaultUnitOfWorkFactory()); $this->commandBus->setLogger($this->logger); $this->eventStore = new RecordingEventStore($this->storedEvents, $this->givenEvents, $this->aggregateIdentifier); $this->parameterResolver = new FixtureParameterResolverFactory(); $this->aggregateType = $aggregateType; $this->clearGivenWhenState(); }
} } } class UserEventListener implements EventListenerInterface { public function handle(EventMessageInterface $event) { print_r($event->getPayload()); } } // set up logging $logger = new Logger('governor'); $logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG)); // 1. create a command bus and command gateway $commandBus = new SimpleCommandBus(); $commandBus->setLogger($logger); $commandGateway = new DefaultCommandGateway($commandBus); $rootDirectory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'CommandHandlerExample'; @mkdir($rootDirectory); echo sprintf("Initializing FileSystemEventStore in %s\n", $rootDirectory); // 2. initialize the event store $eventStore = new FilesystemEventStore(new SimpleEventFileResolver($rootDirectory), new JMSSerializer()); // 3. create the event bus $eventBus = new SimpleEventBus(); $eventBus->setLogger($logger); // 4. create an event sourcing repository $repository = new EventSourcingRepository(User::class, $eventBus, new NullLockManager(), $eventStore, new GenericAggregateFactory(User::class)); //5. create and register our commands $commandHandler = new UserCommandHandler($repository); $commandBus->subscribe('CommandHandlerExample\\CreateUserCommand', $commandHandler); $commandBus->subscribe('CommandHandlerExample\\ChangeUserEmailCommand', $commandHandler);