1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace TMV\Messenger\Middleware; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use Symfony\Component\Messenger\Envelope; |
11
|
|
|
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; |
12
|
|
|
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; |
13
|
|
|
use Symfony\Component\Messenger\Middleware\StackInterface; |
14
|
|
|
|
15
|
|
|
abstract class AbstractDoctrineMiddleware implements MiddlewareInterface |
16
|
|
|
{ |
17
|
|
|
/** @var ManagerRegistry $managerRegistry */ |
18
|
|
|
protected $managerRegistry; |
19
|
|
|
|
20
|
|
|
/** @var string|null $entityManagerName */ |
21
|
|
|
protected $entityManagerName; |
22
|
|
|
|
23
|
17 |
|
public function __construct(ManagerRegistry $managerRegistry, ?string $entityManagerName = null) |
24
|
|
|
{ |
25
|
17 |
|
$this->managerRegistry = $managerRegistry; |
26
|
17 |
|
$this->entityManagerName = $entityManagerName; |
27
|
17 |
|
} |
28
|
|
|
|
29
|
13 |
|
final public function handle(Envelope $envelope, StackInterface $stack): Envelope |
30
|
|
|
{ |
31
|
|
|
try { |
32
|
|
|
/** @var EntityManagerInterface $entityManager */ |
33
|
13 |
|
$entityManager = $this->managerRegistry->getManager($this->entityManagerName); |
34
|
4 |
|
} catch (InvalidArgumentException $e) { |
35
|
4 |
|
throw new UnrecoverableMessageHandlingException($e->getMessage(), 0, $e); |
36
|
|
|
} |
37
|
|
|
|
38
|
9 |
|
return $this->handleForManager($entityManager, $envelope, $stack); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
abstract protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope; |
42
|
|
|
} |
43
|
|
|
|