Advertisement
Guest User

Untitled

a guest
May 28th, 2020
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. interface EventInterface
  2. {
  3. public function releaseEvents(): array;
  4. }
  5.  
  6. trait EventsTrait
  7. {
  8. private $events = [];
  9.  
  10. protected function recordEvent($event): void
  11. {
  12. $this->events[] = $event;
  13. }
  14.  
  15. public function releaseEvents(): array
  16. {
  17. [$events, $this->events] = [$this->events, []];
  18.  
  19. return $events;
  20. }
  21. }
  22.  
  23. class Entity implements EventInterface
  24. {
  25. use EventsTrait;
  26.  
  27. public function publish()
  28. {
  29. ...
  30. $this->recordEvent(new EntityPublished($this->id));
  31. }
  32. }
  33.  
  34. class Flusher
  35. {
  36. public function flush(EventInterface ...$entities)
  37. {
  38. $this->em->flush();
  39.  
  40. foreach($entities as $entity) {
  41. foreach($entity->releaseEvents() as $event) {
  42. $this->dispatcher->dispatch($event);
  43. }
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement