Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Symfony Messenger - Subscribing multiple Handlers to Messages
<?php
namespace Acme\TemporaryWork\Infrastructure\ProcessManager;
use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface;
use Prooph\EventSourcing\AggregateChanged;
use Acme\TemporaryWork\Domain\Model\Agency\Event\AgencyHired;
use Acme\TemporaryWork\Domain\Model\Agency\Event\AgencyModified;
use Acme\PersonnelManagement\Infrastructure\RabbitMq\Producer;
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
final class AgencyPublisher extends Producer
implements MessageSubscriberInterface
{
public function __construct(ProducerInterface $producer)
{
$this->producer = $producer;
}
public function __invoke(AggregateChanged $event): void
{
$data = array_merge(
['agencyId' => $event->agencyId()->toString()],
$event->payload()
);
switch (get_class($event)) {
case AgencyHired::class:
$this->publish($data, 'acme.personnel.agency.hired');
break;
case AgencyModified::class:
$this->publish($data, 'acme.personnel.agency.modified');
break;
}
}
public static function getHandledMessages(): iterable
{
yield AgencyHired::class;
yield AgencyModified::class;
}
}
@webdevilopers

This comment has been minimized.

Copy link
Owner Author

@webdevilopers webdevilopers commented Jun 6, 2020

Is there a way to subscribe a symfony messenger message to multiple handlers without adding code to the class via MessageSubscriberInterface - maybe via config?

Came from:

/cc @weaverryan

Tutorial by @knpuniversity:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment