Completed
Pull Request — master (#602)
by Tom
10:09
created

DBALConfigurationFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 52.94%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 94
ccs 18
cts 34
cp 0.5294
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineORMModule\Service;
6
7
use Doctrine\DBAL\Configuration;
8
use Doctrine\DBAL\Types\Type;
9
use DoctrineORMModule\Options\Configuration as DoctrineORMModuleConfiguration;
10
use Interop\Container\ContainerInterface;
11
use Laminas\ServiceManager\FactoryInterface;
12
use Laminas\ServiceManager\ServiceLocatorInterface;
13
use RuntimeException;
14
use function is_string;
15
use function sprintf;
16
17
/**
18
 * DBAL Configuration ServiceManager factory
19
 *
20
 * @link    http://www.doctrine-project.org/
21
 */
22
class DBALConfigurationFactory implements FactoryInterface
23
{
24
    protected string $name;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
25
26
    public function __construct(string $name)
27
    {
28
        $this->name = $name;
29
    }
30 88
31
    /**
32 88
     * {@inheritDoc}
33 88
     *
34
     * @return Configuration
35
     */
36
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
37
    {
38
        $config = new Configuration();
39
        $this->setupDBALConfiguration($container, $config);
40
41
        return $config;
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     *
47
     * @return Configuration
48
     */
49
    public function createService(ServiceLocatorInterface $container)
50
    {
51
        return $this($container, Configuration::class);
52
    }
53
54
    public function setupDBALConfiguration(ContainerInterface $container, Configuration $config) : void
55
    {
56
        $options = $this->getOptions($container);
57
        $config->setResultCacheImpl($container->get($options->resultCache));
58
59
        $sqlLogger = $options->sqlLogger;
60
        if (is_string($sqlLogger) && $container->has($sqlLogger)) {
61 86
            $sqlLogger = $container->get($sqlLogger);
62
        }
63 86
64 86
        $config->setSQLLogger($sqlLogger);
65
66 86
        foreach ($options->types as $name => $class) {
67 86
            if (Type::hasType($name)) {
68
                Type::overrideType($name, $class);
69
            } else {
70 86
                Type::addType($name, $class);
71
            }
72 86
        }
73
    }
74
75
    /**
76
     * @return mixed
77
     *
78
     * @throws RuntimeException
79 86
     */
80
    public function getOptions(ContainerInterface $serviceLocator)
81
    {
82
        $options = $serviceLocator->get('config');
83
        $options = $options['doctrine'];
84
        $options = $options['configuration'][$this->name] ?? null;
85
86 88
        if ($options === null) {
87
            throw new RuntimeException(
88 88
                sprintf(
89 88
                    'Configuration with name "%s" could not be found in "doctrine.configuration".',
90 88
                    $this->name
91
                )
92 88
            );
93
        }
94
95
        $optionsClass = $this->getOptionsClass();
96
97
        return new $optionsClass($options);
98
    }
99
100
    protected function getOptionsClass() : string
101 88
    {
102
        return DoctrineORMModuleConfiguration::class;
103 88
    }
104
}
105