Completed
Pull Request — master (#459)
by Vytautas
14:01
created

DBALConnectionFactory::__invoke()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4.0138

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
ccs 19
cts 21
cp 0.9048
rs 8.5806
cc 4
eloc 19
nc 8
nop 3
crap 4.0138
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\DriverManager;
23
use Doctrine\DBAL\Types\Type;
24
use DoctrineModule\Service\AbstractFactory;
25
use Interop\Container\ContainerInterface;
26
use Zend\ServiceManager\ServiceLocatorInterface;
27
28
/**
29
 * DBAL Connection ServiceManager factory
30
 *
31
 * @license MIT
32
 * @link    http://www.doctrine-project.org/
33
 * @author  Kyle Spraggs <[email protected]>
34
 */
35
class DBALConnectionFactory extends AbstractFactory
36
{
37
    /**
38
     * {@inheritDoc}
39
     *
40 58
     * @return \Doctrine\DBAL\Connection
41
     */
42
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
43 58
    {
44 58
        /** @var $options \DoctrineORMModule\Options\DBALConnection */
45
        $options = $this->getOptions($container, 'connection');
46 58
        $pdo     = $options->getPdo();
47
48
        if (is_string($pdo)) {
49
            $pdo = $container->get($pdo);
50
        }
51 58
52 58
        $params = array(
53 58
            'driverClass'  => $options->getDriverClass(),
54 58
            'wrapperClass' => $options->getWrapperClass(),
55 58
            'pdo'          => $pdo,
56
        );
57 58
        $params = array_merge($params, $options->getParams());
58 58
59
        $configuration = $container->get($options->getConfiguration());
60 58
        $eventManager  = $container->get($options->getEventManager());
61 58
62 58
        $connection = DriverManager::getConnection($params, $configuration, $eventManager);
63 2
        $platform = $connection->getDatabasePlatform();
64 58
        foreach ($options->getDoctrineTypeMappings() as $dbType => $doctrineType) {
65
            $platform->registerDoctrineTypeMapping($dbType, $doctrineType);
66 58
        }
67 1
68 58
        foreach ($options->getDoctrineCommentedTypes() as $type) {
69
            $platform->markDoctrineTypeCommented(Type::getType($type));
70 58
        }
71
72
        return $connection;
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     * @return \Doctrine\DBAL\Connection
78 58
     */
79
    public function createService(ServiceLocatorInterface $container)
80 58
    {
81
        return $this($container, \Doctrine\DBAL\Connection::class);
82
    }
83
84
    /**
85
     * Get the class name of the options associated with this factory.
86
     *
87
     * @return string
88
     */
89
    public function getOptionsClass()
90
    {
91
        return 'DoctrineORMModule\Options\DBALConnection';
92
    }
93
}
94