Failed Conditions
Pull Request — master (#32)
by Mathieu
02:59
created

RetryDriver::retryConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Ez\DbLinker\Driver;
4
5
use \Doctrine\DBAL\Connection;
6
use \Doctrine\DBAL\DriverManager;
7
use Ez\DbLinker\Driver\Connection\CallAndRetry;
8
use Ez\DbLinker\Driver\Connection\RetryConnection;
9
10
trait RetryDriver
11
{
12
    use CallAndRetry;
13
14
    /**
15
     * Attempts to create a connection with the database.
16
     *
17
     * @param array       $params        All connection parameters passed by the user.
18
     * @param string|null $username      The username to use when connecting.
19
     * @param string|null $password      The password to use when connecting.
20
     * @param array       $driverOptions The driver options to use when connecting.
21
     *
22
     * @return \Doctrine\DBAL\Driver\Connection The database connection.
23
     */
24
    public function connect(Array $params, $username = null, $password = null, Array $driverOptions = []) {
25
        return $this->callAndRetry(function () use ($params) {
26
            return new RetryConnection($params['connectionParams'], $params['retryStrategy']);
27
        }, $params['retryStrategy']);
28
    }
29
30
    /**
31
     * Gets the SchemaManager that can be used to inspect and change the underlying
32
     * database schema of the platform this driver connects to.
33
     *
34
     * @param \Doctrine\DBAL\Connection $conn
35
     *
36
     * @return \Doctrine\DBAL\Schema\AbstractSchemaManager
37
     */
38
    public function getSchemaManager(Connection $conn) {
39
        return $this->wrappedDriver($conn)->getSchemaManager($conn);
40
    }
41
42
    /**
43
     * Gets the name of the database connected to for this driver.
44
     *
45
     * @param \Doctrine\DBAL\Connection $conn
46
     *
47
     * @return string The name of the database.
48
     */
49
    public function getDatabase(Connection $conn) {
50
        return $this->wrappedDriver($conn)->getDatabase($conn);
51
    }
52
53
    private function wrappedDriver(Connection $connection)
54
    {
55
        if ($connection instanceof RetryConnection) {
56
            return $connection->wrappedConnection()->getDriver();
57
        }
58
    }
59
60
    protected function retryConnection()
61
    {
62
        throw new \Exception("Error Processing Request", 1);
63
    }
64
}
65