Passed
Push — master ( e473b2...f755b5 )
by Sébastien
02:42 queued 13s
created

ConnectionFactory::createConnection()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.1426

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 28
ccs 10
cts 14
cp 0.7143
rs 8.8333
cc 7
nc 32
nop 3
crap 8.1426
1
<?php
2
3
namespace Bdf\Prime\Connection\Factory;
4
5
use Bdf\Prime\Connection\ConnectionInterface;
6
use Bdf\Prime\Connection\SimpleConnection;
7
use Bdf\Prime\Exception\DBALException;
8
use Bdf\Prime\Exception\PrimeException;
9
use Doctrine\Common\EventManager;
10
use Doctrine\DBAL\Configuration;
11
use Doctrine\DBAL\DBALException as DoctrineDBALException;
12
use Doctrine\DBAL\DriverManager;
13
14
/**
15
 * ConnectionFactory
16
 *
17
 * Create simple connection instance
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
18
 */
19
class ConnectionFactory implements ConnectionFactoryInterface
20
{
21
    /**
22
     * The drivers map
23
     *
24
     * @var array
25
     */
26
    static private $driversMap;
0 ignored issues
show
Coding Style introduced by
The static declaration must come after the visibility declaration
Loading history...
27
28
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $connectionName should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $parameters should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
29
     * {@inheritDoc}
30
     */
31 149
    public function create(string $connectionName, array $parameters, Configuration $config): ConnectionInterface
32
    {
33 149
        $connection = $this->createConnection($parameters, $config);
34
35
        // Store connection and return adapter instance
36 149
        if ($connection instanceof ConnectionInterface) {
0 ignored issues
show
introduced by
$connection is always a sub-type of Bdf\Prime\Connection\ConnectionInterface.
Loading history...
37 149
            $connection->setName($connectionName);
38
        }
39
40 149
        return $connection;
41
    }
42
43
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $connectionName should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $parameters should have a doc-comment as per coding-style.
Loading history...
44
     * {@inheritDoc}
45
     */
46 55
    public function support(string $connectionName, array $parameters): bool
47
    {
48 55
        return true;
49
    }
50
51
    /**
52
     * Create the instance of the connection
53
     *
54
     * @param array $parameters
55
     * @param Configuration|null $config
56
     * @param EventManager|null $eventManager The event manager, optional.
57
     *
58
     * @return ConnectionInterface
59
     * @throws DBALException
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
60
     */
61 149
    private function createConnection(array $parameters, Configuration $config = null, EventManager $eventManager = null): ConnectionInterface
0 ignored issues
show
Coding Style introduced by
Private method name "ConnectionFactory::createConnection" must be prefixed with an underscore
Loading history...
62
    {
63
        // Set the custom driver class + wrapper
64 149
        if (isset($parameters['driver']) && isset(self::$driversMap[$parameters['driver']])) {
65
            list($parameters['driverClass'], $parameters['wrapperClass']) = self::$driversMap[$parameters['driver']];
66
            unset($parameters['driver']);
67
        }
68
69
        // Replace 'adapter' with 'driver' and add 'pdo_'
70 149
        if (isset($parameters['adapter'])) {
71 119
            $parameters['driver'] = 'pdo_' . $parameters['adapter'];
72 119
            unset($parameters['adapter']);
73
        }
74
75
        // default charset
0 ignored issues
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
76 149
        if (!isset($parameters['charset'])) {
77 146
            $parameters['charset'] = 'utf8';
78
        }
79
80
        // default wrapper
0 ignored issues
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
81 149
        if (!isset($parameters['wrapperClass'])) {
82 149
            $parameters['wrapperClass'] = SimpleConnection::class;
83
        }
84
85
        try {
86 149
            return DriverManager::getConnection($parameters, $config, $eventManager);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Doctrine\DBAL\Dri...$config, $eventManager) returns the type Doctrine\DBAL\Connection which is incompatible with the type-hinted return Bdf\Prime\Connection\ConnectionInterface.
Loading history...
87
        } catch (DoctrineDBALException $e) {
88
            throw new DBALException('Cannot create the connection : '.$e->getMessage(), $e->getCode(), $e);
89
        }
90
    }
91
92
    /**
93
     * Register a global driver map
94
     *
95
     * @param string $name
96
     * @param string $driver
97
     * @param string|null $wrapper
98
     */
99 1
    public static function registerDriverMap($name, $driver, $wrapper = null)
100
    {
101 1
        self::$driversMap[$name] = [$driver, $wrapper];
102 1
    }
103
104
    /**
105
     * Get a global driver map
106
     *
107
     * @param string $name
108
     *
109
     * @return string|null
110
     */
111 1
    public static function getDriverMap($name)
112
    {
113 1
        return isset(self::$driversMap[$name])
114 1
            ? self::$driversMap[$name]
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement must be declared on a single line
Loading history...
115 1
            : null;
116
    }
117
118
    /**
119
     * Unregister a global driver map
120
     *
121
     * @param string $name
122
     */
123 1
    public static function unregisterDriverMap($name)
124
    {
125 1
        unset(self::$driversMap[$name]);
126 1
    }
127
}
128