Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Created June 6, 2020 09:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/23479b053f40ab2db79bf8be627f2b11 to your computer and use it in GitHub Desktop.
Save webdevilopers/23479b053f40ab2db79bf8be627f2b11 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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