Completed
Pull Request — master (#677)
by Sebastian
02:04
created

ConnectionFactory::createConnection()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 13
nc 8
nop 4
1
<?php
2
3
/*
4
 * This file is part of the Doctrine Bundle
5
 *
6
 * The code was originally distributed inside the Symfony framework.
7
 *
8
 * (c) Fabien Potencier <[email protected]>
9
 * (c) Doctrine Project, Benjamin Eberlei <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Doctrine\Bundle\DoctrineBundle;
16
17
use Doctrine\Common\EventManager;
18
use Doctrine\DBAL\Configuration;
19
use Doctrine\DBAL\Connection;
20
use Doctrine\DBAL\DriverManager;
21
use Doctrine\DBAL\DBALException;
22
use Doctrine\DBAL\Exception\DriverException;
23
use Doctrine\DBAL\Types\Type;
24
25
/**
26
 * Connection
27
 */
28
class ConnectionFactory
29
{
30
    private $typesConfig = array();
31
    private $commentedTypes = array();
32
    private $initialized = false;
33
34
    /**
35
     * Construct.
36
     *
37
     * @param array $typesConfig
38
     */
39
    public function __construct(array $typesConfig)
40
    {
41
        $this->typesConfig = $typesConfig;
42
    }
43
44
    /**
45
     * Create a connection by name.
46
     *
47
     * @param array         $params
48
     * @param Configuration $config
0 ignored issues
show
Documentation introduced by
Should the type for parameter $config not be null|Configuration?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
49
     * @param EventManager  $eventManager
0 ignored issues
show
Documentation introduced by
Should the type for parameter $eventManager not be null|EventManager?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
50
     * @param array         $mappingTypes
51
     *
52
     * @return \Doctrine\DBAL\Connection
53
     */
54
    public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = array())
55
    {
56
        if (!$this->initialized) {
57
            $this->initializeTypes();
58
        }
59
60
        $connection = DriverManager::getConnection($params, $config, $eventManager);
61
62
        if (!empty($mappingTypes)) {
63
            $platform = $this->getDatabasePlatform($connection);
64
            foreach ($mappingTypes as $dbType => $doctrineType) {
65
                $platform->registerDoctrineTypeMapping($dbType, $doctrineType);
66
            }
67
        }
68
        if (!empty($this->commentedTypes)) {
69
            $platform = $this->getDatabasePlatform($connection);
70
            foreach ($this->commentedTypes as $type) {
71
                $platform->markDoctrineTypeCommented(Type::getType($type));
72
            }
73
        }
74
75
        return $connection;
76
    }
77
78
    /**
79
     * Try to get the database platform.
80
     *
81
     * This could fail if types should be registered to an predefined/unused connection
82
     * and the platform version is unknown.
83
     * For details have a look at DoctrineBundle issue #673.
84
     *
85
     * @param  \Doctrine\DBAL\Connection $connection
86
     *
87
     * @return \Doctrine\DBAL\Platforms\AbstractPlatform
88
     * @throws \Doctrine\DBAL\DBALException
89
     */
90
    private function getDatabasePlatform(Connection $connection)
91
    {
92
        try {
93
            return $connection->getDatabasePlatform();
94
        } catch (DriverException $driverException) {
95
            throw new DBALException(
96
                "An exception occured while establishing a connection to figure out your platform version." . PHP_EOL .
97
                "You can circumvent this by setting a 'server_version' configuration value" . PHP_EOL . PHP_EOL .
98
                "For further information have a look at:" . PHP_EOL .
99
                "https://github.com/doctrine/DoctrineBundle/issues/673",
100
                0,
101
                $driverException
102
            );
103
        }
104
    }
105
106
    /**
107
     * initialize the types
108
     */
109
    private function initializeTypes()
110
    {
111
        foreach ($this->typesConfig as $type => $typeConfig) {
112
            if (Type::hasType($type)) {
113
                Type::overrideType($type, $typeConfig['class']);
114
            } else {
115
                Type::addType($type, $typeConfig['class']);
116
            }
117
            if ($typeConfig['commented']) {
118
                $this->commentedTypes[] = $type;
119
            }
120
        }
121
        $this->initialized = true;
122
    }
123
}
124