AbstractDoctrineMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 27
ccs 9
cts 9
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 10 2
A __construct() 0 4 1
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