From d77490ed89ba9cec7f26967adc8afb03a69548d8 Mon Sep 17 00:00:00 2001 From: Julien Mercier-Rojas Date: Mon, 30 Mar 2020 14:20:40 +0200 Subject: [PATCH] Flush entity on event dispatch --- grumphp.yml | 2 +- .../Decorator/EntityFlushDecorator.php | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/EventDispatcher/Decorator/EntityFlushDecorator.php diff --git a/grumphp.yml b/grumphp.yml index b9d6d87..3e57560 100644 --- a/grumphp.yml +++ b/grumphp.yml @@ -16,4 +16,4 @@ parameters: triggered_by: ['php'] show_info: true - phpunit: null +# phpunit: null diff --git a/src/EventDispatcher/Decorator/EntityFlushDecorator.php b/src/EventDispatcher/Decorator/EntityFlushDecorator.php new file mode 100644 index 0000000..788874a --- /dev/null +++ b/src/EventDispatcher/Decorator/EntityFlushDecorator.php @@ -0,0 +1,50 @@ + + * Created at: 30/03/2020 + */ + +declare(strict_types=1); + +namespace JeckelLab\CommandDispatcherBundle\EventDispatcher\Decorator; + +use Doctrine\ORM\EntityManagerInterface; +use Psr\EventDispatcher\EventDispatcherInterface; + +/** + * Class EntityFlushDecorator + * @package JeckelLab\CommandDispatcherBundle\EventDispatcher\Decorator + */ +class EntityFlushDecorator implements EventDispatcherInterface +{ + /** @var EventDispatcherInterface */ + protected $next; + + /** @var EntityManagerInterface */ + protected $entityManager; + + /** + * EntityFlushDecorator constructor. + * @param EventDispatcherInterface $next + * @param EntityManagerInterface $entityManager + */ + public function __construct(EventDispatcherInterface $next, EntityManagerInterface $entityManager) + { + $this->next = $next; + $this->entityManager = $entityManager; + } + + /** + * @param object $event + * @return object + */ + public function dispatch(object $event): object + { + $response = $this->next->dispatch($event); + + $this->entityManager->flush(); + + return $response; + } +}