Completed
Push — master ( 7a6bdd...324ce1 )
by Sébastien
09:34
created

ConnectionFactory   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 23
c 1
b 0
f 0
dl 0
loc 102
ccs 26
cts 28
cp 0.9286
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A support() 0 3 1
A unregisterDriverMap() 0 3 1
A createConnection() 0 25 6
A getDriverMap() 0 5 2
A registerDriverMap() 0 3 1
A create() 0 10 2
1
<?php
2
3
namespace Bdf\Prime\Connection\Factory;
4
5
use Bdf\Prime\Connection\ConnectionInterface;
6
use Bdf\Prime\Connection\SimpleConnection;
7
use Doctrine\Common\EventManager;
8
use Doctrine\DBAL\Configuration;
9
use Doctrine\DBAL\DriverManager;
10
11
/**
12
 * ConnectionFactory
13
 *
14
 * Create simple connection instance
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
15
 */
16
class ConnectionFactory implements ConnectionFactoryInterface
17
{
18
    /**
19
     * The drivers map
20
     *
21
     * @var array
22
     */
23
    static private $driversMap;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
Coding Style introduced by
The static declaration must come after the visibility declaration
Loading history...
24
25
    /**
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...
26
     * {@inheritDoc}
27
     */
28 141
    public function create(string $connectionName, array $parameters, Configuration $config): ConnectionInterface
29
    {
30 141
        $connection = $this->createConnection($parameters, $config);
31
32
        // Store connection and return adapter instance
33 141
        if ($connection instanceof ConnectionInterface) {
0 ignored issues
show
introduced by
$connection is always a sub-type of Bdf\Prime\Connection\ConnectionInterface.
Loading history...
34 141
            $connection->setName($connectionName);
35
        }
36
37 141
        return $connection;
38
    }
39
40
    /**
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...
41
     * {@inheritDoc}
42
     */
43 54
    public function support(string $connectionName, array $parameters): bool
44
    {
45 54
        return true;
46
    }
47
48
    /**
49
     * Create the instance of the connection
50
     *
51
     * @param array              $parameters
52
     * @param Configuration|null $config
53
     * @param EventManager|null  $eventManager The event manager, optional.
54
     *
55
     * @return ConnectionInterface
56
     */
57 141
    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...
58
    {
59
        // Set the custom driver class + wrapper
60 141
        if (isset($parameters['driver']) && isset(self::$driversMap[$parameters['driver']])) {
61
            list($parameters['driverClass'], $parameters['wrapperClass']) = self::$driversMap[$parameters['driver']];
62
            unset($parameters['driver']);
63
        }
64
65
        // Replace 'adapter' with 'driver' and add 'pdo_'
66 141
        if (isset($parameters['adapter'])) {
67 111
            $parameters['driver'] = 'pdo_' . $parameters['adapter'];
68 111
            unset($parameters['adapter']);
69
        }
70
71
        // default charset
0 ignored issues
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
72 141
        if (!isset($parameters['charset'])) {
73 138
            $parameters['charset'] = 'utf8';
74
        }
75
76
        // default wrapper
0 ignored issues
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
77 141
        if (!isset($parameters['wrapperClass'])) {
78 141
            $parameters['wrapperClass'] = SimpleConnection::class;
79
        }
80
81 141
        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...
82
    }
83
84
    /**
85
     * Register a global driver map
86
     *
87
     * @param string $name
88
     * @param string $driver
89
     * @param string|null $wrapper
90
     */
91 1
    public static function registerDriverMap($name, $driver, $wrapper = null)
92
    {
93 1
        self::$driversMap[$name] = [$driver, $wrapper];
94 1
    }
95
96
    /**
97
     * Get a global driver map
98
     *
99
     * @param string $name
100
     *
101
     * @return string|null
102
     */
103 1
    public static function getDriverMap($name)
104
    {
105 1
        return isset(self::$driversMap[$name])
106 1
            ? self::$driversMap[$name]
0 ignored issues
show
Coding Style introduced by
Inline IF statements are not allowed
Loading history...
Coding Style introduced by
Inline shorthand IF statement must be declared on a single line
Loading history...
107 1
            : null;
108
    }
109
110
    /**
111
     * Unregister a global driver map
112
     *
113
     * @param string $name
114
     */
115 1
    public static function unregisterDriverMap($name)
116
    {
117 1
        unset(self::$driversMap[$name]);
118 1
    }
119
}
120