Passed
Push — master ( 79a79c...9c574d )
by Thomas Mauro
02:27
created

AbstractDoctrineMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 24
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\Laminas\Messenger\Bridge\Doctrine\Middleware;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\Persistence\ManagerRegistry;
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
    protected ManagerRegistry $managerRegistry;
18
19
    protected ?string $entityManagerName;
20
21
    public function __construct(ManagerRegistry $managerRegistry, ?string $entityManagerName = null)
22
    {
23
        $this->managerRegistry = $managerRegistry;
24
        $this->entityManagerName = $entityManagerName;
25
    }
26
27
    final public function handle(Envelope $envelope, StackInterface $stack): Envelope
28
    {
29
        try {
30
            $connection = $this->managerRegistry->getManager($this->entityManagerName);
31
        } catch (InvalidArgumentException $e) {
32
            throw new UnrecoverableMessageHandlingException($e->getMessage(), 0, $e);
33
        }
34
35
        return $this->handleForManager($connection, $envelope, $stack);
36
    }
37
38
    abstract protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope;
39
}
40