Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface EventInterface
- {
- public function releaseEvents(): array;
- }
- trait EventsTrait
- {
- private $events = [];
- protected function recordEvent($event): void
- {
- $this->events[] = $event;
- }
- public function releaseEvents(): array
- {
- [$events, $this->events] = [$this->events, []];
- return $events;
- }
- }
- class Entity implements EventInterface
- {
- use EventsTrait;
- public function publish()
- {
- ...
- $this->recordEvent(new EntityPublished($this->id));
- }
- }
- class Flusher
- {
- public function flush(EventInterface ...$entities)
- {
- $this->em->flush();
- foreach($entities as $entity) {
- foreach($entity->releaseEvents() as $event) {
- $this->dispatcher->dispatch($event);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement