Completed
Pull Request — master (#479)
by Michał
03:49
created

DBALConnectionFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 91.67%

Importance

Changes 9
Bugs 2 Features 0
Metric Value
wmc 5
c 9
b 2
f 0
lcom 0
cbo 6
dl 0
loc 50
ccs 22
cts 24
cp 0.9167
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 32 4
A getOptionsClass() 0 4 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineORMModule\Service;
21
22
use Doctrine\DBAL\Connection;
23
use Doctrine\DBAL\DriverManager;
24
use Doctrine\DBAL\Types\Type;
25
use DoctrineModule\Service\AbstractFactory;
26
use DoctrineORMModule\Options\DBALConnection as OptionsDBALConnection;
27
use Interop\Container\ContainerInterface;
28
29
/**
30
 * DBAL Connection ServiceManager factory
31
 *
32
 * @license MIT
33
 * @link    http://www.doctrine-project.org/
34
 * @author  Kyle Spraggs <[email protected]>
35
 */
36
class DBALConnectionFactory extends AbstractFactory
37
{
38
    /**
39
     * {@inheritDoc}
40
     *
41
     * @return Connection
42
     */
43 58
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
44
    {
45
        /** @var $options OptionsDBALConnection */
46 58
        $options = $this->getOptions($container, 'connection');
47 58
        $pdo     = $options->getPdo();
48
49 58
        if (is_string($pdo)) {
50
            $pdo = $container->get($pdo);
51
        }
52
53
        $params = [
54 58
            'driverClass'  => $options->getDriverClass(),
55 58
            'wrapperClass' => $options->getWrapperClass(),
56 58
            'pdo'          => $pdo,
57 58
        ];
58 58
        $params = array_merge($params, $options->getParams());
59
60 58
        $configuration = $container->get($options->getConfiguration());
61 58
        $eventManager  = $container->get($options->getEventManager());
62
63 58
        $connection = DriverManager::getConnection($params, $configuration, $eventManager);
64 58
        $platform = $connection->getDatabasePlatform();
65 58
        foreach ($options->getDoctrineTypeMappings() as $dbType => $doctrineType) {
66 2
            $platform->registerDoctrineTypeMapping($dbType, $doctrineType);
67 58
        }
68
69 58
        foreach ($options->getDoctrineCommentedTypes() as $type) {
70 1
            $platform->markDoctrineTypeCommented(Type::getType($type));
71 58
        }
72
73 58
        return $connection;
74
    }
75
76
    /**
77
     * Get the class name of the options associated with this factory.
78
     *
79
     * @return string
80
     */
81 58
    public function getOptionsClass()
82
    {
83 58
        return OptionsDBALConnection::class;
84
    }
85
}
86