Completed
Pull Request — master (#527)
by
unknown
14:10 queued 08:39
created

DBALConfigurationFactory::getOptionsClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineORMModule\Service;
21
22
use DoctrineORMModule\Options\Configuration as DoctrineORMModuleConfiguration;
23
use Interop\Container\ContainerInterface;
24
use RuntimeException;
25
use Doctrine\DBAL\Configuration;
26
use Doctrine\DBAL\Types\Type;
27
use Zend\ServiceManager\FactoryInterface;
28
use Zend\ServiceManager\ServiceLocatorInterface;
29
30
/**
31
 * DBAL Configuration ServiceManager factory
32
 *
33
 * @license MIT
34
 * @link    http://www.doctrine-project.org/
35
 * @author  Kyle Spraggs <[email protected]>
36
 */
37
class DBALConfigurationFactory implements FactoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Zend\ServiceManager\FactoryInterface has been deprecated with message: Use Zend\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...
38
{
39
    /**
40
     * @var string
41
     */
42
    protected $name;
43
44
    /**
45
     * @param string $name
46
     */
47 88
    public function __construct($name)
48
    {
49 88
        $this->name = $name;
50 88
    }
51
52
    /**
53
     * {@inheritDoc}
54
     *
55
     * @return \Doctrine\DBAL\Configuration
56
     */
57
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
58
    {
59
        $config  = new Configuration();
60
        $this->setupDBALConfiguration($container, $config);
61
62
        return $config;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     * @return \Doctrine\DBAL\Configuration
68
     */
69
    public function createService(ServiceLocatorInterface $container)
70
    {
71
        return $this($container, \Doctrine\DBAL\Configuration::class);
72
    }
73
74
    /**
75
     * @param ContainerInterface $container
76
     * @param Configuration      $config
77
     */
78 86
    public function setupDBALConfiguration(ContainerInterface $container, Configuration $config)
79
    {
80 86
        $options = $this->getOptions($container);
81 86
        $config->setResultCacheImpl($container->get($options->resultCache));
82
83 86
        $sqlLogger = $options->sqlLogger;
84 86
        if (is_string($sqlLogger) and $container->has($sqlLogger)) {
85
            $sqlLogger = $container->get($sqlLogger);
86
        }
87 86
        $config->setSQLLogger($sqlLogger);
88
89 86
        foreach ($options->types as $name => $class) {
90
            if (Type::hasType($name)) {
91
                Type::overrideType($name, $class);
92
            } else {
93
                Type::addType($name, $class);
94
            }
95 86
        }
96 86
    }
97
98
    /**
99
     * @param  ContainerInterface $serviceLocator
100
     * @return mixed
101
     * @throws RuntimeException
102
     */
103 88
    public function getOptions(ContainerInterface $serviceLocator)
104
    {
105 88
        $options = $serviceLocator->get('config');
106 88
        $options = $options['doctrine'];
107 88
        $options = isset($options['configuration'][$this->name]) ? $options['configuration'][$this->name] : null;
108
109 88
        if (null === $options) {
110
            throw new RuntimeException(
111
                sprintf(
112
                    'Configuration with name "%s" could not be found in "doctrine.configuration".',
113
                    $this->name
114
                )
115
            );
116
        }
117
118 88
        $optionsClass = $this->getOptionsClass();
119
120 88
        return new $optionsClass($options);
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    protected function getOptionsClass()
127
    {
128
        return DoctrineORMModuleConfiguration::class;
129
    }
130
}
131