Prooph\ServiceBus\Plugin\Router\EventRouter::route PHP Méthode

route() public méthode

public route ( string $eventName )
$eventName string
    public function route($eventName)
    {
        Assertion::string($eventName);
        Assertion::notEmpty($eventName);
        if (null !== $this->tmpEventName && empty($this->eventMap[$this->tmpEventName])) {
            throw new Exception\RuntimeException(sprintf("event %s is not mapped to a listener.", $this->tmpEventName));
        }
        $this->tmpEventName = $eventName;
        if (!isset($this->eventMap[$this->tmpEventName])) {
            $this->eventMap[$this->tmpEventName] = [];
        }
        return $this;
    }

Usage Example

 /**
  * @test
  */
 public function it_confirms_select_one_action_event_after_the_other()
 {
     $actionEvent = $this->prophesize(ActionEvent::class);
     $iterator = new \ArrayIterator(['foo', 'bar']);
     $actionEvent->getParam('recordedEvents', new \ArrayIterator())->willReturn($iterator)->shouldBeCalled();
     $producer = $this->prophesize(Producer::class);
     $producer->startTransaction()->shouldBeCalledTimes(2);
     $producer->commitTransaction()->shouldBeCalledTimes(2);
     $eventBus = new EventBus();
     $plugin = new TransactionalEventPublisher($eventBus, $producer->reveal());
     $eventBusCalls = [];
     $eventRouter = new EventRouter();
     $eventRouter->route('foo')->to(function ($event) use($plugin, &$eventBusCalls) {
         $eventBusCalls[] = $event;
         $actionEvent = new DefaultActionEvent($event, null, ['recordedEvents' => new \ArrayIterator(['baz', 'bam', 'bat'])]);
         $plugin->onEventStoreCommitPost($actionEvent);
     });
     $eventRouter->route('bar')->to(function ($event) use(&$eventBusCalls) {
         $eventBusCalls[] = $event;
     });
     $eventRouter->route('baz')->to(function ($event) use(&$eventBusCalls) {
         $eventBusCalls[] = $event;
     });
     $eventRouter->route('bam')->to(function ($event) use(&$eventBusCalls) {
         $eventBusCalls[] = $event;
     });
     $eventRouter->route('bat')->to(function ($event) use(&$eventBusCalls) {
         $eventBusCalls[] = $event;
     });
     $eventBus->utilize($eventRouter);
     $plugin->onEventStoreCommitPost($actionEvent->reveal());
     $this->assertEquals(['foo', 'bar', 'baz', 'bam', 'bat'], $eventBusCalls);
 }
All Usage Examples Of Prooph\ServiceBus\Plugin\Router\EventRouter::route