Passed
Branch master (7599e2)
by Sébastien
12:18 queued 06:01
created

ConnectionFactory   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
eloc 27
dl 0
loc 109
ccs 26
cts 30
cp 0.8667
rs 10
c 2
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A support() 0 3 1
A unregisterDriverMap() 0 3 1
B createConnection() 0 28 7
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 Bdf\Prime\Exception\DBALException;
8
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...
9
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...
10
use Doctrine\Common\EventManager;
11
use Doctrine\DBAL\Configuration;
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 162
    public function create(string $connectionName, array $parameters, Configuration $config): ConnectionInterface
35
    {
36 162
        $connection = $this->createConnection($parameters, $config);
37
38
        // Store connection and return adapter instance
39 162
        if ($connection instanceof ConnectionInterface) {
0 ignored issues
show
introduced by
$connection is always a sub-type of Bdf\Prime\Connection\ConnectionInterface.
Loading history...
40 162
            $connection->setName($connectionName);
41
        }
42
43 162
        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 162
    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 162
        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 162
        if (isset($parameters['adapter'])) {
74 133
            $parameters['driver'] = 'pdo_' . $parameters['adapter'];
75 133
            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 162
        if (!isset($parameters['charset'])) {
80 159
            $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 162
        if (!isset($parameters['wrapperClass'])) {
85 162
            $parameters['wrapperClass'] = SimpleConnection::class;
86
        }
87
88
        try {
89 162
            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...
90
        } catch (DoctrineDBALException $e) {
91
            throw new DBALException('Cannot create the connection : '.$e->getMessage(), $e->getCode(), $e);
92
        }
93
    }
94
95
    /**
96
     * Register a global driver map
97
     *
98
     * @param string $name
99
     * @param string $driver
100
     * @param string|null $wrapper
101
     */
102 1
    public static function registerDriverMap($name, $driver, $wrapper = null)
103
    {
104 1
        self::$driversMap[$name] = [$driver, $wrapper];
105 1
    }
106
107
    /**
108
     * Get a global driver map
109
     *
110
     * @param string $name
111
     *
112
     * @return string|null
113
     */
114 1
    public static function getDriverMap($name)
115
    {
116 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...
117 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...
118 1
            : null;
119
    }
120
121
    /**
122
     * Unregister a global driver map
123
     *
124
     * @param string $name
125
     */
126 1
    public static function unregisterDriverMap($name)
127
    {
128 1
        unset(self::$driversMap[$name]);
129 1
    }
130
}
131