Completed
Push — master ( d1f8a2...f6c5c7 )
by Andreas
13s queued 10s
created

ConnectionFactory::getDatabasePlatform()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\DBAL\Configuration;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\DBALException;
9
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
10
use Doctrine\DBAL\DriverManager;
11
use Doctrine\DBAL\Exception\DriverException;
12
use Doctrine\DBAL\Platforms\AbstractPlatform;
13
use Doctrine\DBAL\Types\Type;
14
15
class ConnectionFactory
16
{
17
    /** @var mixed[][] */
18
    private $typesConfig = [];
19
20
    /** @var bool */
21
    private $initialized = false;
22
23
    /**
24
     * @param mixed[][] $typesConfig
25
     */
26
    public function __construct(array $typesConfig)
27
    {
28
        $this->typesConfig = $typesConfig;
29
    }
30
31
    /**
32
     * Create a connection by name.
33
     *
34
     * @param mixed[]         $params
35
     * @param string[]|Type[] $mappingTypes
36
     *
37
     * @return Connection
38
     */
39
    public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = [])
40
    {
41
        if (! $this->initialized) {
42
            $this->initializeTypes();
43
        }
44
45
        $connection = DriverManager::getConnection($params, $config, $eventManager);
46
47
        if (! isset($params['pdo']) && ! isset($params['charset'])) {
48
            $params            = $connection->getParams();
49
            $params['charset'] = 'utf8';
50
            $driver            = $connection->getDriver();
51
52
            if ($driver instanceof AbstractMySQLDriver) {
53
                $params['charset'] = 'utf8mb4';
54
55
                if (! isset($params['defaultTableOptions']['collate'])) {
56
                    $params['defaultTableOptions']['collate'] = 'utf8mb4_unicode_ci';
57
                }
58
            }
59
60
            $connection = new $connection($params, $driver, $connection->getConfiguration(), $connection->getEventManager());
61
        }
62
63
        if (! empty($mappingTypes)) {
64
            $platform = $this->getDatabasePlatform($connection);
65
            foreach ($mappingTypes as $dbType => $doctrineType) {
66
                $platform->registerDoctrineTypeMapping($dbType, $doctrineType);
0 ignored issues
show
Bug introduced by
It seems like $doctrineType defined by $doctrineType on line 65 can also be of type object<Doctrine\DBAL\Types\Type>; however, Doctrine\DBAL\Platforms\...erDoctrineTypeMapping() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
67
            }
68
        }
69
70
        return $connection;
71
    }
72
73
    /**
74
     * Try to get the database platform.
75
     *
76
     * This could fail if types should be registered to an predefined/unused connection
77
     * and the platform version is unknown.
78
     * For details have a look at DoctrineBundle issue #673.
79
     *
80
     * @return AbstractPlatform
81
     *
82
     * @throws DBALException
83
     */
84
    private function getDatabasePlatform(Connection $connection)
85
    {
86
        try {
87
            return $connection->getDatabasePlatform();
88
        } catch (DriverException $driverException) {
89
            throw new DBALException(
90
                'An exception occured while establishing a connection to figure out your platform version.' . PHP_EOL .
91
                "You can circumvent this by setting a 'server_version' configuration value" . PHP_EOL . PHP_EOL .
92
                'For further information have a look at:' . PHP_EOL .
93
                'https://github.com/doctrine/DoctrineBundle/issues/673',
94
                0,
95
                $driverException
96
            );
97
        }
98
    }
99
100
    /**
101
     * initialize the types
102
     */
103
    private function initializeTypes()
104
    {
105
        foreach ($this->typesConfig as $typeName => $typeConfig) {
106
            if (Type::hasType($typeName)) {
107
                Type::overrideType($typeName, $typeConfig['class']);
108
            } else {
109
                Type::addType($typeName, $typeConfig['class']);
110
            }
111
        }
112
113
        $this->initialized = true;
114
    }
115
}
116