Symfony workflow events: reacting to a status change
Symfony workflow events announce each step of a transition: you react to a status change, whether it comes from a page or a command.
final class ParcheminsDuRegistre { public function __construct(private readonly Parcheminier $parcheminier) { } // workflow.reservation.entered.confirmee: arriving at "confirmee", whatever the transition. #[AsEnteredListener(workflow: 'reservation', place: 'confirmee')] public function confirmee(EnteredEvent $event): void { $this->parcheminier->envoyerLaConfirmation($event->getSubject()); } }
On every transition, the workflow emits, in order, guard, leave, transition, enter, entered, completed, then announce for each transition that has become possible. Each comes in three scopes, workflow.entered, workflow.reservation.entered and workflow.reservation.entered.confirmee, the last one targeting a place or a transition; #[AsEnteredListener], #[AsLeaveListener] or #[AsCompletedListener] build that name. The side effect then lives with the workflow, not in a controller. The trap: putting a side effect in a guard listener, called on every can() and every time the buttons are displayed, or in enter, emitted before the new marking is written on the object, whereas entered comes once the place is reached. debug:event-dispatcher workflow.reservation.entered.confirmee lists who is really listening.
The exercises have you send the confirmation scroll when a booking reaches confirmee, and a cancellation scroll when it reaches annulee, the form no longer sending anything; then, in a "simplified" ledger, find out why a cancellation sends two contradictory scrolls, or the wrong one.
2 exercises work on this concept, by writing code in the browser.
In the tracks
- Scrolls from the reservation book Symfony for PHP developers · chapter 10: The life of a reservation
- 🐉 Boss: the misplaced scrolls Symfony for PHP developers · chapter 10: The life of a reservation