Completed
Push — master ( c00473...569398 )
by Tom
01:40 queued 10s
created

DBALConnectionFactory::__invoke()   B

Complexity

Conditions 8
Paths 32

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 8.0052

Importance

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