Completed
Pull Request — master (#25)
by Hugo
04:11
created

PingConnectionMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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