Completed
Pull Request — master (#35)
by Mathieu
02:03
created

WrapperDriver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 43
wmc 4
lcom 0
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSchemaManager() 0 3 1
A getDatabase() 0 3 1
A callOnWrappedDriver() 0 17 2
1
<?php
2
3
namespace Ez\DbLinker\Driver;
4
5
use Doctrine\DBAL\Connection;
6
use Ez\DbLinker\Driver\Connection\ConnectionWrapper;
7
8
trait WrapperDriver
9
{
10
    /**
11
     * Gets the SchemaManager that can be used to inspect and change the underlying
12
     * database schema of the platform this driver connects to.
13
     *
14
     * @param \Doctrine\DBAL\Connection $connection
15
     *
16
     * @return \Doctrine\DBAL\Schema\AbstractSchemaManager
17
     */
18
    public function getSchemaManager(Connection $connection) {
19
        return $this->callOnWrappedDriver($connection, __FUNCTION__);
20
    }
21
22
    /**
23
     * Gets the name of the database connected to for this driver.
24
     *
25
     * @param \Doctrine\DBAL\Connection $connection
26
     *
27
     * @return string The name of the database.
28
     */
29
    public function getDatabase(Connection $connection) {
30
        return $this->callOnWrappedDriver($connection, __FUNCTION__);
31
    }
32
33
    private function callOnWrappedDriver(Connection $connection, $method)
34
    {
35
        $connection = $connection->getWrappedConnection();
36
        if (!$connection instanceof ConnectionWrapper) {
37
            $class = get_class($connection);
38
            $interface = ConnectionWrapper::class;
39
            throw new WrapperException(
40
                "This driver cannot $method on $class because it does not implements $interface"
41
            );
42
        }
43
        $driver = $connection->wrappedDriver();
44
        $connection = new Connection(
45
            ['pdo' => $connection->wrappedConnection()],
46
            $driver
47
        );
48
        return $driver->$method($connection);
49
    }
50
}
51