AbstractDoctrineMiddleware.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\Messenger;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Symfony\Component\Messenger\Envelope;
  14. use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
  15. use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
  16. use Symfony\Component\Messenger\Middleware\StackInterface;
  17. /**
  18. * @author Konstantin Myakshin <molodchick@gmail.com>
  19. *
  20. * @internal
  21. */
  22. abstract class AbstractDoctrineMiddleware implements MiddlewareInterface
  23. {
  24. protected $managerRegistry;
  25. protected $entityManagerName;
  26. public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null)
  27. {
  28. $this->managerRegistry = $managerRegistry;
  29. $this->entityManagerName = $entityManagerName;
  30. }
  31. final public function handle(Envelope $envelope, StackInterface $stack): Envelope
  32. {
  33. try {
  34. $entityManager = $this->managerRegistry->getManager($this->entityManagerName);
  35. } catch (\InvalidArgumentException $e) {
  36. throw new UnrecoverableMessageHandlingException($e->getMessage(), 0, $e);
  37. }
  38. return $this->handleForManager($entityManager, $envelope, $stack);
  39. }
  40. abstract protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope;
  41. }