DoctrinePingConnectionMiddleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
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 25
ccs 11
cts 11
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handleForManager() 0 10 2
A pingConnection() 0 11 3
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