Completed
Pull Request — develop (#607)
by Tom
03:12 queued 01:40
created

DBALConnectionFactory::__invoke()   B

Complexity

Conditions 8
Paths 32

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 8.0046

Importance

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