/** * Handle an event. * * @param EventInterface $event * * @return void */ public function handle(EventInterface $event) { if (!$event instanceof SerializableEvent) { throw new \InvalidArgumentException(sprintf('Cannot serialize %s event.', $event->getName())); } $this->publisher->publish(json_encode($event->toArray()), $event->getName()); }
/** * @param object $command * @param callable $next * * @return mixed */ public function execute($command, callable $next) { if (!$command instanceof NamedCommand) { throw new \InvalidArgumentException('Command must be a NamedCommand'); } $this->queuePublisher->publish($this->serializer->serialize($command), $command->getCommandName()); }
/** * @test */ public function it_publishes_the_event_in_the_QueuePublisher() { $event = new Event('SomethingHappened'); $this->serializer->shouldReceive('serialize')->with($event)->andReturn('serialized'); $this->queuePublisher->shouldReceive('publish')->with('serialized', 'SomethingHappened')->once(); $this->listener->handle($event); }
function it_should_not_publish_message_when_rollbacking(QueuePublisher $publisher) { $this->beginTransaction(); $publisher->publish('', '')->shouldNotBeCalled(); $this->publish('', ''); $this->rollback(); }
/** * {@inheritdoc} */ public function commit() { foreach ($this->messages as $message) { $this->publisher->publish($message['data'], $message['routingKey']); } $this->messages = []; $this->running = false; }
/** * @test */ public function it_should_throw_the_exception_if_it_catches_one() { $stream = new DomainEventStream([new DomainMessage('a', 0, new Metadata(), [], DateTime::now())]); $this->serializer->shouldReceive('serialize')->with($stream)->andReturn('serialized'); $this->queuePublisher->shouldReceive('publish')->andThrow(\Exception::class); $this->setExpectedException(\Exception::class); $eventBus = new QueuePublishingEventBus($this->serializer, $this->queuePublisher); $eventBus->publish($stream); }
/** * Publishes the events from the domain event stream to the listeners. * * @param DomainEventStreamInterface $domainMessages * @throws \Exception */ public function publish(DomainEventStreamInterface $domainMessages) { $this->queue[] = $domainMessages; if (!$this->isPublishing) { $this->isPublishing = true; try { parent::publish($domainMessages); while ($domainMessageStream = array_shift($this->queue)) { $serializedDomainMessages = $this->serializer->serialize($domainMessages); $this->queuePublisher->publish($serializedDomainMessages); } } catch (\Exception $e) { throw $e; } finally { $this->isPublishing = false; } } }
/** * Handle an event. * * @param EventInterface $event * * @return void */ public function handle(EventInterface $event) { $this->publisher->publish($this->serializer->serialize($event), $event->getName()); }