Completed
Pull Request — master (#459)
by Vytautas
13:28 queued 09:14
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 4
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
37
{
38
    /**
39
     * @var string
40
     */
41
    protected $name;
42
43
    /**
44
     * @param string $name
45
     */
46 69
    public function __construct($name)
47
    {
48 69
        $this->name = $name;
49 69
    }
50
51
    /**
52
     * {@inheritDoc}
53
     *
54
     * @return \Doctrine\DBAL\Configuration
55
     */
56
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
0 ignored issues
show
Unused Code introduced by
The parameter $requestedName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58
        $config  = new Configuration();
59
        $this->setupDBALConfiguration($container, $config);
60
61
        return $config;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     * @return \Doctrine\DBAL\Configuration
67
     */
68
    public function createService(ServiceLocatorInterface $container)
69
    {
70
        return $this($container, \Doctrine\DBAL\Configuration::class);
71
    }
72
73
    /**
74
     * @param ContainerInterface $container
75
     * @param Configuration      $config
76
     */
77 68
    public function setupDBALConfiguration(ContainerInterface $container, Configuration $config)
78
    {
79 68
        $options = $this->getOptions($container);
80 68
        $config->setResultCacheImpl($container->get($options->resultCache));
81
82 68
        $sqlLogger = $options->sqlLogger;
83 68
        if (is_string($sqlLogger) and $container->has($sqlLogger)) {
84
            $sqlLogger = $container->get($sqlLogger);
85
        }
86 68
        $config->setSQLLogger($sqlLogger);
87
88 68
        foreach ($options->types as $name => $class) {
89
            if (Type::hasType($name)) {
90
                Type::overrideType($name, $class);
91
            } else {
92
                Type::addType($name, $class);
93
            }
94 68
        }
95 68
    }
96
97
    /**
98
     * @param  ContainerInterface $serviceLocator
99
     * @return mixed
100
     * @throws RuntimeException
101
     */
102 69
    public function getOptions(ContainerInterface $serviceLocator)
103
    {
104 69
        $options = $serviceLocator->get('Config');
105 69
        $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 69
        $optionsClass = $this->getOptionsClass();
118
119 69
        return new $optionsClass($options);
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    protected function getOptionsClass()
126
    {
127
        return 'DoctrineModule\Options\Configuration';
128
    }
129
}
130