Created
June 6, 2020 09:16
-
-
Save webdevilopers/23479b053f40ab2db79bf8be627f2b11 to your computer and use it in GitHub Desktop.
Symfony Messenger - Subscribing multiple Handlers to Messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: