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

handleForManager()   A

Complexity

Conditions 2
Paths 6

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 6
nop 3
dl 0
loc 12
rs 10
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