Completed
Push — master ( c318a1...1bc1b4 )
by Ross
20s queued 10s
created

PingConnectionMiddleware::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace League\Tactician\Doctrine\DBAL;
4
5
use Doctrine\DBAL\Connection;
6
use League\Tactician\Middleware;
7
8
/**
9
 * Verifies if there is a connection established with the database. If not it will reconnect.
10
 */
11
final class PingConnectionMiddleware implements Middleware
12
{
13
    /**
14
     * @var Connection
15
     */
16
    private $connection;
17
18
    public function __construct(Connection $connection)
19
    {
20
        $this->connection = $connection;
21
    }
22
23
    /**
24
     * Reconnects to the database if the connection is expired.
25
     *
26
     * @param object $command
27
     * @param callable $next
28
     * @return mixed
29
     */
30
    public function execute($command, callable $next)
31
    {
32
        if (!$this->connection->ping()) {
33
            $this->connection->close();
34
            $this->connection->connect();
35
        }
36
        
37
        return $next($command);
38
    }
39
}
40