/** * @param EventSubscriberInterface $subscriber * * @return $this */ public function addEventSubscriber(EventSubscriberInterface $subscriber) { if ($this->eventBus === null) { throw new \RuntimeException('The event bus should not be null'); } $this->eventBus->addSubscriber($subscriber); return $this; }
/** * @param DomainEventSubscriberInterface $subscriber */ protected function addSubscriber(DomainEventSubscriberInterface $subscriber) { $this->eventBus->addSubscriber($subscriber); }
/** * @param callable|ApplicationConfig $config * * @return ConsoleApplication */ protected function createApplication($config) { $commandBus = CommandBus::create(); $eventBus = EventBus::create(); $postService = new PostService($eventBus); $commandBus->addHandler(CreateBlogCommand::class, new BlogService($eventBus)); $commandBus->addHandler(CreatePostCommand::class, $postService); $commandBus->addHandler(ChangePostTitleCommand::class, $postService); return new ConsoleApplication($config, $commandBus, $eventBus); }
/** * @param ChangePostTitleCommand $command */ public function changePostTitle(ChangePostTitleCommand $command) { $this->eventBus->dispatch(new PostTitleWasChanged($command->title())); }
/** * Test RemoveSubscriber method. */ public function testRemoveSubscriber() { $this->given($eventBus = EventBus::create())->and($subscriber = new UserEventSubscriber())->and($eventBus->addSubscriber($subscriber))->then()->boolean($eventBus->hasEventListeners(UserEventSubscriber::FOO_EVENT))->isTrue()->boolean($eventBus->hasEventListeners(UserEventSubscriber::BAR_EVENT))->isTrue()->boolean($eventBus->hasEventListeners(UserEventSubscriber::USER_LOGIN))->isTrue()->and()->when($eventBus->removeSubscriber($subscriber))->then()->boolean($eventBus->hasEventListeners(UserEventSubscriber::FOO_EVENT))->isFalse()->boolean($eventBus->hasEventListeners(UserEventSubscriber::BAR_EVENT))->isFalse()->boolean($eventBus->hasEventListeners(UserEventSubscriber::USER_LOGIN))->isFalse(); }
/** * Test eventBus method. */ public function testEventBus() { $this->given($eventBus = EventBus::create())->when(DomainEventPublisher::set($eventBus))->then()->object(DomainEventPublisher::eventBus())->isEqualTo($eventBus); }