Passed
Pull Request — master (#31)
by Sébastien
12:54 queued 06:01
created

ConnectionFactory::createConnection()   B

Complexity

Conditions 8
Paths 64

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 32
ccs 12
cts 16
cp 0.75
rs 8.4444
cc 8
nc 64
nop 3
crap 9
1
<?php
2
3
namespace Bdf\Prime\Connection\Factory;
4
5
use Bdf\Prime\Configuration;
6
use Bdf\Prime\Connection\ConnectionInterface;
7
use Bdf\Prime\Connection\SimpleConnection;
8
use Bdf\Prime\Exception\DBALException;
9
use Bdf\Prime\MongoDB\Driver\MongoConnection;
0 ignored issues
show
Bug introduced by
The type Bdf\Prime\MongoDB\Driver\MongoConnection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Bdf\Prime\MongoDB\Driver\MongoDriver;
0 ignored issues
show
Bug introduced by
The type Bdf\Prime\MongoDB\Driver\MongoDriver was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Doctrine\Common\EventManager;
12
use Doctrine\DBAL\DBALException as DoctrineDBALException;
13
use Doctrine\DBAL\DriverManager;
14
15
/**
16
 * ConnectionFactory
17
 *
18
 * Create simple connection instance
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
19
 */
20
class ConnectionFactory implements ConnectionFactoryInterface
21
{
22
    /**
23
     * The drivers map
24
     *
25
     * @var array<string, array{0: class-string<\Doctrine\DBAL\Driver>, 1: class-string<\Doctrine\DBAL\Driver\Connection>}>
0 ignored issues
show
introduced by
Expected "arraystringarray0classstring\Doctrine\DBAL\Driver1classstring\Doctrine\DBAL\Driver\Connection" but found "array<string, array0: class-string<\Doctrine\DBAL\Driver>, 1: class-string<\Doctrine\DBAL\Driver\Connection>>" for @var tag in member variable comment
Loading history...
Documentation Bug introduced by
The doc comment array<string, array{0: c...AL\Driver\Connection>}> at position 8 could not be parsed: Unknown type name 'class-string' at position 8 in array<string, array{0: class-string<\Doctrine\DBAL\Driver>, 1: class-string<\Doctrine\DBAL\Driver\Connection>}>.
Loading history...
26
     */
27
    static private $driversMap = [
0 ignored issues
show
Coding Style introduced by
The static declaration must come after the visibility declaration
Loading history...
28
        'mongodb' => [MongoDriver::class, MongoConnection::class],
29
    ];
30
31
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $parameters should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $connectionName 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...
32
     * {@inheritDoc}
33
     */
34 169
    public function create(string $connectionName, array $parameters, ?Configuration $config = null): ConnectionInterface
35
    {
36 169
        $connection = $this->createConnection($parameters, $config);
37
38
        // Store connection and return adapter instance
39 169
        if ($connection instanceof ConnectionInterface) {
0 ignored issues
show
introduced by
$connection is always a sub-type of Bdf\Prime\Connection\ConnectionInterface.
Loading history...
40 169
            $connection->setName($connectionName);
41
        }
42
43 169
        return $connection;
44
    }
45
46
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $parameters should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $connectionName should have a doc-comment as per coding-style.
Loading history...
47
     * {@inheritDoc}
48
     */
49 68
    public function support(string $connectionName, array $parameters): bool
50
    {
51 68
        return true;
52
    }
53
54
    /**
55
     * Create the instance of the connection
56
     *
57
     * @param array $parameters
58
     * @param Configuration|null $config
59
     * @param EventManager|null $eventManager The event manager, optional.
60
     *
61
     * @return ConnectionInterface
62
     * @throws DBALException
63
     */
64 169
    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...
65
    {
66
        // Set the custom driver class + wrapper
67 169
        if (isset($parameters['driver']) && isset(self::$driversMap[$parameters['driver']])) {
68
            list($parameters['driverClass'], $parameters['wrapperClass']) = self::$driversMap[$parameters['driver']];
69
            unset($parameters['driver']);
70
        }
71
72
        // Replace 'adapter' with 'driver' and add 'pdo_'
73 169
        if (isset($parameters['adapter'])) {
74 135
            $parameters['driver'] = 'pdo_' . $parameters['adapter'];
75 135
            unset($parameters['adapter']);
76
        }
77
78
        // default charset
0 ignored issues
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
79 169
        if (!isset($parameters['charset'])) {
80 163
            $parameters['charset'] = 'utf8';
81
        }
82
83
        // default wrapper
0 ignored issues
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
84 169
        if (!isset($parameters['wrapperClass'])) {
85 169
            $parameters['wrapperClass'] = SimpleConnection::class;
86
        }
87
88 169
        if ($config === null) {
89 75
            $config = new Configuration();
90
        }
91
92
        try {
93 169
            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...
94
        } catch (DoctrineDBALException $e) {
95
            throw new DBALException('Cannot create the connection : '.$e->getMessage(), $e->getCode(), $e);
96
        }
97
    }
98
99
    /**
100
     * Register a global driver map
101
     *
102
     * @param string $name
103
     * @param string $driver
104
     * @param string|null $wrapper
105
     */
106 1
    public static function registerDriverMap($name, $driver, $wrapper = null)
107
    {
108 1
        self::$driversMap[$name] = [$driver, $wrapper];
109 1
    }
110
111
    /**
112
     * Get a global driver map
113
     *
114
     * @param string $name
115
     *
116
     * @return string|null
117
     */
118 1
    public static function getDriverMap($name)
119
    {
120 1
        return isset(self::$driversMap[$name])
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode ? self::driversMap[$name] : null also could return the type array which is incompatible with the documented return type null|string.
Loading history...
121 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...
122 1
            : null;
123
    }
124
125
    /**
126
     * Unregister a global driver map
127
     *
128
     * @param string $name
129
     */
130 1
    public static function unregisterDriverMap($name)
131
    {
132 1
        unset(self::$driversMap[$name]);
133 1
    }
134
}
135