Completed
Push — master ( 7c7b47...8cb461 )
by Tom
11s
created

DBALConnectionFactory::__invoke()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7.0199

Importance

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