The tavern bell

Exercise 2 of 6 in chapter 9 · 270 XP · 5 objectives checked by tests

Topics covered: Domain eventEventDispatcherInterfacedispatchListener priority

When Bruna invents a dish, the whole tavern must know. Pippin has already written the code to invite the guilds to taste it (InviterLesGuildes), and Gorm insists on being told first. But the kitchen controller tells nobody.

We could write everything in the controller: send the raven to Gorm, loop over the guilds… then tomorrow add the update of the chalkboard, and a statistic the day after. The controller would become the crossroads of the whole tavern.

A domain event

The other way: the controller announces what happened (“a dish was added”), and whoever is interested listens. The PlatAjoute (dish added) event already exists: a simple class that carries the dish and the cook. Adding a reaction means adding a listener, without touching the controller.

Your mission

  1. In CuisineController::nouveauPlat(), dispatch PlatAjoute once the dish is saved, and only then.
  2. Complete PrevenirLeTavernier (notify the innkeeper): for every PlatAjoute, a raven flies to Gorm, with the name of the dish and the name of the cook.
  3. Gorm must be notified before the guilds.

To try it: log in with bruna@drunken-dragon.me / cauldron, invent a dish, then open /aviary. In the console, debug:event-dispatcher "App\Event\PlatAjoute" lists the listeners of the event, in order.