DoctrinePingConnectionMiddleware::pingConnection()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\Messenger\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\ReceivedStamp;
11
12
class DoctrinePingConnectionMiddleware extends AbstractDoctrineMiddleware
13
{
14 3
    protected function handleForManager(
15
        EntityManagerInterface $entityManager,
16
        Envelope $envelope,
17
        StackInterface $stack
18
    ): Envelope {
19 3
        if (null !== $envelope->last(ReceivedStamp::class)) {
20 2
            $this->pingConnection($entityManager);
21
        }
22
23 3
        return $stack->next()->handle($envelope, $stack);
24
    }
25
26 2
    private function pingConnection(EntityManagerInterface $entityManager): void
27
    {
28 2
        $connection = $entityManager->getConnection();
29
30 2
        if (! $connection->ping()) {
31 2
            $connection->close();
32 2
            $connection->connect();
33
        }
34
35 2
        if (! $entityManager->isOpen()) {
36 2
            $this->managerRegistry->resetManager($this->entityManagerName);
37
        }
38 2
    }
39
}
40