/** * @command Malocher\Cqrs\Command\PublishEventCommand * @param PublishEventCommand $command */ public function publishEventHandler(PublishEventCommand $command) { print "---- monitoring start -----\n"; print sprintf("PublishEventCommand invoked by %s on %s \n", $command->getMessageClass(), $command->getBusName()); print sprintf("id:%s, edited:%s, ts:%s, version:%s, payload:%s \n", $command->getMessageVars()['id'], $command->getMessageVars()['edited'], $command->getMessageVars()['timestamp'], $command->getMessageVars()['version'], $command->getMessageVars()['payload']); print "---- monitoring ends ------\n"; }
public function testClosurePublishEvent() { $gate = new Gate(); $gate->enableSystemBus(); $gate->attach($this->bus); $this->bus->registerEventListener('Malocher\\CqrsTest\\Coverage\\Mock\\Event\\MockEvent', function (MockEvent $event) { $event->edit(); }); $gate->getSystemBus()->mapCommand('Malocher\\Cqrs\\Command\\PublishEventCommand', function (PublishEventCommand $command) { $this->publishEventCommand = $command; }); $gate->getSystemBus()->registerEventListener('Malocher\\Cqrs\\Event\\EventPublishedEvent', function (EventPublishedEvent $event) { $this->eventPublishedEvent = $event; }); $mockEvent = new MockEvent(); $gate->getBus($this->bus->getName())->publishEvent($mockEvent); $this->assertEquals(true, $mockEvent->isEdited()); $this->assertInstanceOf('Malocher\\Cqrs\\Command\\PublishEventCommand', $this->publishEventCommand); $this->assertInstanceOf('Malocher\\Cqrs\\Event\\EventPublishedEvent', $this->eventPublishedEvent); $this->assertEquals('Malocher\\CqrsTest\\Coverage\\Mock\\Event\\MockEvent', $this->publishEventCommand->getMessageClass()); $this->assertEquals('Malocher\\CqrsTest\\Coverage\\Mock\\Event\\MockEvent', $this->eventPublishedEvent->getMessageClass()); }
/** * {@inheritDoc} */ public function publishEvent(EventInterface $event) { // Check if event exists after invoking the PublishEventCommand because // the PublishEventCommand tells that a event is dispatched but does not care // if it succeeded. Later the EventPublishedEvent can be used to check if a // event succeeded. if (!is_null($this->gate->getSystemBus())) { $publishEventCommand = new PublishEventCommand(); $publishEventCommand->setMessageClass(get_class($event)); $publishEventCommand->setMessageVars($event->getMessageVars()); $publishEventCommand->setBusName($this->getName()); $this->gate->getSystemBus()->invokeCommand($publishEventCommand); } try { $response = $this->bus->publishEvent($event); if ($response === false) { return false; } } catch (BusException $ex) { //throw it again throw $ex; } catch (\Exception $ex) { throw BusException::defaultBusError($ex->getMessage(), null, $ex); } // Dispatch the EventPublishedEvent here! If for example a event could not be dispatched // because it does not exist in the eventListenerMap[<empty>] this Event would never // be dispatched! if (!is_null($this->gate->getSystemBus())) { $eventPublishedEvent = new EventPublishedEvent(); $eventPublishedEvent->setMessageClass(get_class($event)); $eventPublishedEvent->setMessageVars($event->getMessageVars()); $eventPublishedEvent->setBusName($this->getName()); $this->gate->getSystemBus()->publishEvent($eventPublishedEvent); } return $response; }