Completed
Pull Request — master (#479)
by Michał
04:19
created

DBALConnectionFactory::__invoke()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4.0119

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 32
ccs 20
cts 22
cp 0.9091
rs 8.5806
cc 4
eloc 19
nc 8
nop 3
crap 4.0119
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