DBALConfigurationFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
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 84
ccs 18
cts 34
cp 0.5294
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createService() 0 4 1
A __construct() 0 4 1
A __invoke() 0 7 1
A setupDBALConfiguration() 0 20 5
A getOptions() 0 19 2
A getOptionsClass() 0 4 1
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
class DBALConfigurationFactory implements FactoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Laminas\ServiceManager\FactoryInterface has been deprecated with message: Use Laminas\ServiceManager\Factory\FactoryInterface instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

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