Completed
Pull Request — master (#459)
by Vytautas
14:01
created

DBALConfigurationFactory::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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 Interop\Container\ContainerInterface;
23
use RuntimeException;
24
use Doctrine\DBAL\Configuration;
25
use Doctrine\DBAL\Types\Type;
26
use Zend\ServiceManager\FactoryInterface;
27
use Zend\ServiceManager\ServiceLocatorInterface;
28
29
/**
30
 * DBAL Configuration ServiceManager factory
31
 *
32
 * @license MIT
33
 * @link    http://www.doctrine-project.org/
34
 * @author  Kyle Spraggs <[email protected]>
35
 */
36
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...
37
{
38
    /**
39
     * @var string
40
     */
41
    protected $name;
42
43
    /**
44
     * @param string $name
45 69
     */
46
    public function __construct($name)
47 69
    {
48 69
        $this->name = $name;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     *
54
     * @return \Doctrine\DBAL\Configuration
55
     */
56
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
57
    {
58
        $config  = new Configuration();
59
        $this->setupDBALConfiguration($container, $config);
60
61
        return $config;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66 68
     * @return \Doctrine\DBAL\Configuration
67
     */
68 68
    public function createService(ServiceLocatorInterface $container)
69 68
    {
70
        return $this($container, \Doctrine\DBAL\Configuration::class);
71 68
    }
72 68
73
    /**
74
     * @param ContainerInterface $container
75 68
     * @param Configuration      $config
76
     */
77 68
    public function setupDBALConfiguration(ContainerInterface $container, Configuration $config)
78
    {
79
        $options = $this->getOptions($container);
80
        $config->setResultCacheImpl($container->get($options->resultCache));
81
82
        $sqlLogger = $options->sqlLogger;
83 68
        if (is_string($sqlLogger) and $container->has($sqlLogger)) {
84 68
            $sqlLogger = $container->get($sqlLogger);
85
        }
86
        $config->setSQLLogger($sqlLogger);
87
88
        foreach ($options->types as $name => $class) {
89
            if (Type::hasType($name)) {
90
                Type::overrideType($name, $class);
91 69
            } else {
92
                Type::addType($name, $class);
93 69
            }
94 69
        }
95 69
    }
96
97 69
    /**
98
     * @param  ContainerInterface $serviceLocator
99
     * @return mixed
100
     * @throws RuntimeException
101
     */
102
    public function getOptions(ContainerInterface $serviceLocator)
103
    {
104
        $options = $serviceLocator->get('Config');
105
        $options = $options['doctrine'];
106 69
        $options = isset($options['configuration'][$this->name]) ? $options['configuration'][$this->name] : null;
107
108 69
        if (null === $options) {
109
            throw new RuntimeException(
110
                sprintf(
111
                    'Configuration with name "%s" could not be found in "doctrine.configuration".',
112
                    $this->name
113
                )
114
            );
115
        }
116
117
        $optionsClass = $this->getOptionsClass();
118
119
        return new $optionsClass($options);
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    protected function getOptionsClass()
126
    {
127
        return 'DoctrineModule\Options\Configuration';
128
    }
129
}
130