The processor
is a kind of event bus listener that can execute actions on certain events.
A process can be for example used to send an email when a guest is checked in:
namespace App\Domain\Hotel\Listener;
use App\Domain\Hotel\Event\GuestIsCheckedIn;
use Patchlevel\EventSourcing\Aggregate\AggregateChanged;
use Patchlevel\EventSourcing\EventBus\Listener;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
final class SendCheckInEmailListener implements Listener
{
private MailerInterface $mailer;
private function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
public function __invoke(AggregateChanged $event): void
{
if (!$event instanceof GuestIsCheckedIn) {
return;
}
$email = (new Email())
->from('noreply@patchlevel.de')
->to('hq@patchlevel.de')
->subject('Guest is checked in')
->text(sprintf('A new guest named "%s" is checked in', $event->guestName()));
$this->mailer->send($email);
}
}
If you have the symfony default service setting with autowire
and autoconfiger
enabled,
the processor is automatically recognized and registered at the Listener
interface.
Otherwise you have to define the processor in the symfony service file:
services:
App\Domain\Hotel\Listener\SendCheckInEmailListener:
tags:
- event_sourcing.processor
You can also determine the priority
in which the processors are executed.
The higher the priority, the earlier the processor is executed.
You have to add the tag manually and specify the priority.
services:
App\Domain\Hotel\Listener\SendCheckInEmailListener:
autoconfigure: false
tags:
- name: event_sourcing.processor
priority: 16
⚠️ You have to deactivate theautoconfigure
for this service, otherwise the service will be added twice.
📖 The
projection
listener has a priority of-32
, to do things after the projection, you have to be lower.