Completed
Pull Request — master (#562)
by Oliver
04:48 queued 02:59
created

DBALConnectionFactory::createService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 76
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
28
    {
29
        /** @var $options DBALConnection */
30 76
        $options = $this->getOptions($container, 'connection');
31 76
        $pdo     = $options->getPdo();
32
33 76
        if (is_string($pdo)) {
34
            $pdo = $container->get($pdo);
35
        }
36
37
        $params = [
38 76
            'driverClass'  => $options->getDriverClass(),
39 76
            'wrapperClass' => $options->getWrapperClass(),
40 76
            'pdo'          => $pdo,
41
        ];
42 76
        $params = array_merge($params, $options->getParams());
43
44 76
        if (array_key_exists('platform', $params)
45 76
            && is_string($params['platform'])
46 76
            && $container->has($params['platform'])
47
        ) {
48 1
            $params['platform'] = $container->get($params['platform']);
49
        }
50
51 76
        $configuration = $container->get($options->getConfiguration());
52 76
        $eventManager  = $container->get($options->getEventManager());
53
54 76
        $connection = DriverManager::getConnection($params, $configuration, $eventManager);
55 76
        foreach ($options->getDoctrineTypeMappings() as $dbType => $doctrineType) {
56 2
            $connection->getDatabasePlatform()->registerDoctrineTypeMapping($dbType, $doctrineType);
57
        }
58
59 76
        foreach ($options->getDoctrineCommentedTypes() as $type) {
60 1
            $connection->getDatabasePlatform()->markDoctrineTypeCommented(Type::getType($type));
61
        }
62
63 76
        return $connection;
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     * @return Connection
69
     */
70 76
    public function createService(ServiceLocatorInterface $container)
71
    {
72 76
        return $this($container, Connection::class);
73
    }
74
75
    /**
76
     * Get the class name of the options associated with this factory.
77
     *
78
     * @return string
79
     */
80 76
    public function getOptionsClass()
81
    {
82 76
        return DBALConnection::class;
83
    }
84
}
85