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

DoctrineCloseConnectionMiddleware   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A handleForManager() 0 12 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 Symfony\Component\Messenger\Envelope;
9
use Symfony\Component\Messenger\Middleware\StackInterface;
10
use Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp;
11
12
/**
13
 * @final
14
 */
15
class DoctrineCloseConnectionMiddleware extends AbstractDoctrineMiddleware
16
{
17
    protected function handleForManager(
18
        EntityManagerInterface $entityManager,
19
        Envelope $envelope,
20
        StackInterface $stack
21
    ): Envelope {
22
        try {
23
            $connection = $entityManager->getConnection();
24
25
            return $stack->next()->handle($envelope, $stack);
26
        } finally {
27
            if (null !== $envelope->last(ConsumedByWorkerStamp::class)) {
28
                $connection->close();
29
            }
30
        }
31
    }
32
}
33