Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 58e0480

Browse filesBrowse files
committed
[BridgeDoctrineMessenger] Doctrine ping connection middleware
1 parent 2243bf5 commit 58e0480
Copy full SHA for 58e0480

File tree

2 files changed

+162
-0
lines changed
Filter options

2 files changed

+162
-0
lines changed
+64Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Messenger;
13+
14+
use Doctrine\Common\Persistence\ManagerRegistry;
15+
use Doctrine\ORM\EntityManagerInterface;
16+
use Symfony\Component\Messenger\Envelope;
17+
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
18+
use Symfony\Component\Messenger\Middleware\StackInterface;
19+
20+
class DoctrinePingConnectionMiddleware implements MiddlewareInterface
21+
{
22+
private $managerRegistry;
23+
private $entityManagerName;
24+
private $forceCloseConnection = false;
25+
26+
public function __construct(ManagerRegistry $managerRegistry, bool $forceCloseConnection = false, string $entityManagerName = null)
27+
{
28+
$this->managerRegistry = $managerRegistry;
29+
$this->forceCloseConnection = $forceCloseConnection;
30+
$this->entityManagerName = $entityManagerName;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
37+
{
38+
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
39+
40+
if (!$entityManager instanceof EntityManagerInterface) {
41+
throw new \InvalidArgumentException(sprintf('The ObjectManager with name "%s" must be an instance of EntityManagerInterface', $this->entityManagerName));
42+
}
43+
44+
$connection = $entityManager->getConnection();
45+
try {
46+
if (!$connection->ping()) {
47+
$connection->close();
48+
$connection->connect();
49+
}
50+
51+
$envelope = $stack->next()->handle($envelope, $stack);
52+
53+
if (!$entityManager->isOpen()) {
54+
$this->managerRegistry->resetManager($this->entityManagerName);
55+
}
56+
57+
return $envelope;
58+
} finally {
59+
if ($this->forceCloseConnection) {
60+
$connection->close();
61+
}
62+
}
63+
}
64+
}
+98Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Messenger;
13+
14+
use Doctrine\Common\Persistence\ManagerRegistry;
15+
use Doctrine\DBAL\Connection;
16+
use Doctrine\ORM\EntityManagerInterface;
17+
use Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware;
18+
use Symfony\Component\Messenger\Envelope;
19+
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
20+
21+
class DoctrinePingConnectionMiddlewareTest extends MiddlewareTestCase
22+
{
23+
private $connection;
24+
private $entityManager;
25+
private $managerRegistry;
26+
private $middleware;
27+
private $middlewareForceCloseConnection;
28+
private $entityManagerName = 'default';
29+
30+
protected function setUp()
31+
{
32+
$this->connection = $this->createMock(Connection::class);
33+
34+
$this->entityManager = $this->createMock(EntityManagerInterface::class);
35+
$this->entityManager->method('getConnection')->willReturn($this->connection);
36+
37+
$this->managerRegistry = $this->createMock(ManagerRegistry::class);
38+
$this->managerRegistry->method('getManager')->willReturn($this->entityManager);
39+
40+
$this->middleware = new DoctrinePingConnectionMiddleware(
41+
$this->managerRegistry,
42+
false,
43+
$this->entityManagerName
44+
);
45+
46+
$this->middlewareForceCloseConnection = new DoctrinePingConnectionMiddleware(
47+
$this->managerRegistry,
48+
true,
49+
$this->entityManagerName
50+
);
51+
}
52+
53+
public function testMiddlewarePingOk()
54+
{
55+
$this->connection->expects($this->once())
56+
->method('ping')
57+
->willReturn(false);
58+
59+
$this->connection->expects($this->once())
60+
->method('close')
61+
;
62+
$this->connection->expects($this->once())
63+
->method('connect')
64+
;
65+
66+
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
67+
}
68+
69+
public function testMiddlewarePingResetEntityManager()
70+
{
71+
$this->entityManager->expects($this->once())
72+
->method('isOpen')
73+
->willReturn(false)
74+
;
75+
$this->managerRegistry->expects($this->once())
76+
->method('resetManager')
77+
->with($this->entityManagerName)
78+
;
79+
80+
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
81+
}
82+
83+
public function testMiddlewarePingForceCloseConnection()
84+
{
85+
$this->connection->expects($this->once())
86+
->method('ping')
87+
->willReturn(true);
88+
89+
$this->connection->expects($this->never())
90+
->method('connect')
91+
;
92+
$this->connection->expects($this->once())
93+
->method('close')
94+
;
95+
96+
$this->middlewareForceCloseConnection->handle(new Envelope(new \stdClass()), $this->getStackMock());
97+
}
98+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.