Passed
Push — master ( 6c900b...b2b78e )
by Vincent
09:01 queued 13s
created

ConnectionFactory::createConnection()   B

Complexity

Conditions 8
Paths 64

Size

Total Lines 37
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 37
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\Exception 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
     * @psalm-suppress UndefinedClass
0 ignored issues
show
Coding Style Documentation introduced by
@psalm-suppress tag is not allowed in member variable comment
Loading history...
28
     * @psalm-suppress InvalidPropertyAssignmentValue
0 ignored issues
show
Coding Style Documentation introduced by
@psalm-suppress tag is not allowed in member variable comment
Loading history...
29
     */
30
    static private $driversMap = [
0 ignored issues
show
Coding Style introduced by
The static declaration must come after the visibility declaration
Loading history...
31
        'mongodb' => [MongoDriver::class, MongoConnection::class],
32
    ];
33
34
    /**
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...
35
     * {@inheritDoc}
36
     */
37 169
    public function create(string $connectionName, array $parameters, ?Configuration $config = null): ConnectionInterface
38
    {
39 169
        $connection = $this->createConnection($parameters, $config);
40
41
        // Store connection and return adapter instance
42 169
        $connection->setName($connectionName);
43
44 169
        return $connection;
45
    }
46
47
    /**
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...
48
     * {@inheritDoc}
49
     */
50 68
    public function support(string $connectionName, array $parameters): bool
51
    {
52 68
        return true;
53
    }
54
55
    /**
56
     * Create the instance of the connection
57
     *
58
     * @param array{wrapperClass?: class-string<T>} $parameters
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{wrapperClass?: class-string<T>} at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array{wrapperClass?: class-string<T>}.
Loading history...
introduced by
Parameter type "arraywrapperClass?: class-string<T>" must not contain spaces
Loading history...
59
     * @param Configuration|null $config
60
     * @param EventManager|null $eventManager The event manager, optional.
61
     *
62
     * @return ConnectionInterface
63
     * @throws DBALException
64
     *
65
     * @template T as ConnectionInterface
66
     */
67 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...
68
    {
69
        // Set the custom driver class + wrapper
70 169
        if (isset($parameters['driver']) && isset(self::$driversMap[$parameters['driver']])) {
71
            list($parameters['driverClass'], $parameters['wrapperClass']) = self::$driversMap[$parameters['driver']];
72
            unset($parameters['driver']);
73
        }
74
75
        // Replace 'adapter' with 'driver' and add 'pdo_'
76 169
        if (isset($parameters['adapter'])) {
77 135
            $parameters['driver'] = 'pdo_' . $parameters['adapter'];
78 135
            unset($parameters['adapter']);
79
        }
80
81
        // default charset
0 ignored issues
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
82 169
        if (!isset($parameters['charset'])) {
83 163
            $parameters['charset'] = 'utf8';
84
        }
85
86
        // default wrapper
0 ignored issues
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
87 169
        if (!isset($parameters['wrapperClass'])) {
88 169
            $parameters['wrapperClass'] = SimpleConnection::class;
89
        }
90
91 169
        if ($config === null) {
92 75
            $config = new Configuration();
93
        }
94
95
        try {
96
            /**
0 ignored issues
show
Coding Style introduced by
Block comments must be started with /*
Loading history...
Coding Style introduced by
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
97
             * @var T
98
             * @psalm-suppress InvalidArgument
99
             */
100 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...
101
        } catch (DoctrineDBALException $e) {
102
            /** @psalm-suppress InvalidScalarArgument */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Block comments must be started with /*
Loading history...
Coding Style introduced by
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
103
            throw new DBALException('Cannot create the connection : '.$e->getMessage(), $e->getCode(), $e);
104
        }
105
    }
106
107
    /**
108
     * Register a global driver map
109
     *
110
     * @param string $name
111
     * @param class-string<\Doctrine\DBAL\Driver> $driver
0 ignored issues
show
introduced by
Expected "classstring\Doctrine\DBAL\Driver" but found "class-string<\Doctrine\DBAL\Driver>" for parameter type
Loading history...
Documentation Bug introduced by
The doc comment class-string<\Doctrine\DBAL\Driver> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Doctrine\DBAL\Driver>.
Loading history...
112
     * @param class-string<\Doctrine\DBAL\Driver\Connection>|null $wrapper
0 ignored issues
show
introduced by
Expected "classstring\Doctrine\DBAL\Driver\Connection|null" but found "class-string<\Doctrine\DBAL\Driver\Connection>|null" for parameter type
Loading history...
113
     */
114 1
    public static function registerDriverMap(string $name, string $driver, ?string $wrapper = null): void
115
    {
116 1
        self::$driversMap[$name] = [$driver, $wrapper];
117 1
    }
118
119
    /**
120
     * Get a global driver map
121
     *
122
     * @param string $name
123
     *
124
     * @return array{0: class-string<\Doctrine\DBAL\Driver>, 1: class-string<\Doctrine\DBAL\Driver\Connection>}|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{0: class-string<\D...river\Connection>}|null at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array{0: class-string<\Doctrine\DBAL\Driver>, 1: class-string<\Doctrine\DBAL\Driver\Connection>}|null.
Loading history...
125
     */
126 1
    public static function getDriverMap(string $name): ?array
127
    {
128 1
        return self::$driversMap[$name] ?? null;
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
129
    }
130
131
    /**
132
     * Unregister a global driver map
133
     *
134
     * @param string $name
135
     */
136 1
    public static function unregisterDriverMap(string $name): void
137
    {
138 1
        unset(self::$driversMap[$name]);
139 1
    }
140
}
141