Completed
Pull Request — master (#562)
by Oliver
04:48 queued 02:59
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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 7 1
A createService() 0 4 1
A getOptionsClass() 0 4 1
B setupDBALConfiguration() 0 19 5
A getOptions() 0 19 2
1
<?php
2
3
namespace DoctrineORMModule\Service;
4
5
use DoctrineORMModule\Options\Configuration as DoctrineORMModuleConfiguration;
6
use Interop\Container\ContainerInterface;
7
use RuntimeException;
8
use Doctrine\DBAL\Configuration;
9
use Doctrine\DBAL\Types\Type;
10
use Zend\ServiceManager\FactoryInterface;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
13
/**
14
 * DBAL Configuration ServiceManager factory
15
 *
16
 * @license MIT
17
 * @link    http://www.doctrine-project.org/
18
 * @author  Kyle Spraggs <[email protected]>
19
 */
20
class DBALConfigurationFactory implements FactoryInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $name;
26
27
    /**
28
     * @param string $name
29
     */
30 88
    public function __construct($name)
31
    {
32 88
        $this->name = $name;
33 88
    }
34
35
    /**
36
     * {@inheritDoc}
37
     *
38
     * @return \Doctrine\DBAL\Configuration
39
     */
40
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
41
    {
42
        $config  = new Configuration();
43
        $this->setupDBALConfiguration($container, $config);
44
45
        return $config;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     * @return \Doctrine\DBAL\Configuration
51
     */
52
    public function createService(ServiceLocatorInterface $container)
53
    {
54
        return $this($container, \Doctrine\DBAL\Configuration::class);
55
    }
56
57
    /**
58
     * @param ContainerInterface $container
59
     * @param Configuration      $config
60
     */
61 86
    public function setupDBALConfiguration(ContainerInterface $container, Configuration $config)
62
    {
63 86
        $options = $this->getOptions($container);
64 86
        $config->setResultCacheImpl($container->get($options->resultCache));
65
66 86
        $sqlLogger = $options->sqlLogger;
67 86
        if (is_string($sqlLogger) and $container->has($sqlLogger)) {
68
            $sqlLogger = $container->get($sqlLogger);
69
        }
70 86
        $config->setSQLLogger($sqlLogger);
71
72 86
        foreach ($options->types as $name => $class) {
73
            if (Type::hasType($name)) {
74
                Type::overrideType($name, $class);
75
            } else {
76
                Type::addType($name, $class);
77
            }
78
        }
79 86
    }
80
81
    /**
82
     * @param  ContainerInterface $serviceLocator
83
     * @return mixed
84
     * @throws RuntimeException
85
     */
86 88
    public function getOptions(ContainerInterface $serviceLocator)
87
    {
88 88
        $options = $serviceLocator->get('config');
89 88
        $options = $options['doctrine'];
90 88
        $options = $options['configuration'][$this->name] ?? null;
91
92 88
        if (null === $options) {
93
            throw new RuntimeException(
94
                sprintf(
95
                    'Configuration with name "%s" could not be found in "doctrine.configuration".',
96
                    $this->name
97
                )
98
            );
99
        }
100
101 88
        $optionsClass = $this->getOptionsClass();
102
103 88
        return new $optionsClass($options);
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    protected function getOptionsClass()
110
    {
111
        return DoctrineORMModuleConfiguration::class;
112
    }
113
}
114