DBALConnectionFactory::__invoke()   B
last analyzed

Complexity

Conditions 8
Paths 32

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 8.0052

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 22
cts 23
cp 0.9565
rs 8.0195
c 0
b 0
f 0
cc 8
nc 32
nop 3
crap 8.0052
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 is_string;
17
18
/**
19
 * DBAL Connection ServiceManager factory
20
 */
21
class DBALConnectionFactory extends AbstractFactory
22
{
23
    /**
24
     * {@inheritDoc}
25
     *
26
     * @return Connection
27
     */
28 78
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
29
    {
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
     *
73
     * @return Connection
74
     */
75 78
    public function createService(ServiceLocatorInterface $container)
76
    {
77 78
        return $this($container, Connection::class);
78
    }
79
80
    /**
81
     * Get the class name of the options associated with this factory.
82
     */
83 78
    public function getOptionsClass() : string
84
    {
85 78
        return DBALConnection::class;
86
    }
87
}
88