Total Complexity | 6 |
Total Lines | 37 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
17 | class DoctrinePingConnectionMiddleware extends AbstractDoctrineMiddleware |
||
18 | { |
||
19 | protected function handleForManager( |
||
20 | EntityManagerInterface $entityManager, |
||
21 | Envelope $envelope, |
||
22 | StackInterface $stack |
||
23 | ): Envelope { |
||
24 | if (null !== $envelope->last(ConsumedByWorkerStamp::class)) { |
||
25 | $this->pingConnection($entityManager); |
||
26 | } |
||
27 | |||
28 | return $stack->next()->handle($envelope, $stack); |
||
29 | } |
||
30 | |||
31 | private function pingConnection(EntityManagerInterface $entityManager): void |
||
32 | { |
||
33 | $connection = $entityManager->getConnection(); |
||
34 | |||
35 | try { |
||
36 | $this->executeDummySql($connection); |
||
37 | } catch (Exception) { |
||
38 | $connection->close(); |
||
39 | // Attempt to reestablish the lazy connection by sending another query. |
||
40 | $this->executeDummySql($connection); |
||
41 | } |
||
42 | |||
43 | if (! $entityManager->isOpen()) { |
||
44 | $this->managerRegistry->resetManager($this->entityManagerName); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @throws Exception |
||
50 | */ |
||
51 | private function executeDummySql(Connection $connection): void |
||
54 | } |
||
55 | } |
||
56 |