Completed
Push — master ( 2c2a55...46eca6 )
by Gianluca
05:40
created

DBALConfigurationFactory::createService()   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 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
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 RuntimeException;
23
use Doctrine\DBAL\Configuration;
24
use Doctrine\DBAL\Types\Type;
25
use Zend\ServiceManager\FactoryInterface;
26
use Zend\ServiceManager\ServiceLocatorInterface;
27
28
/**
29
 * DBAL Configuration ServiceManager factory
30
 *
31
 * @license MIT
32
 * @link    http://www.doctrine-project.org/
33
 * @author  Kyle Spraggs <[email protected]>
34
 */
35
class DBALConfigurationFactory implements FactoryInterface
36
{
37
    /**
38
     * @var string
39
     */
40
    protected $name;
41
42
    /**
43
     * @param string $name
44
     */
45 72
    public function __construct($name)
46
    {
47 72
        $this->name = $name;
48 72
    }
49
50
    /**
51
     * {@inheritDoc}
52
     * @return \Doctrine\DBAL\Configuration
53
     */
54
    public function createService(ServiceLocatorInterface $serviceLocator)
55
    {
56
        $config  = new Configuration();
57
        $this->setupDBALConfiguration($serviceLocator, $config);
58
59
        return $config;
60
    }
61
62
    /**
63
     * @param ServiceLocatorInterface $serviceLocator
64
     * @param Configuration           $config
65
     */
66 70
    public function setupDBALConfiguration(ServiceLocatorInterface $serviceLocator, Configuration $config)
67
    {
68 70
        $options = $this->getOptions($serviceLocator);
69 70
        $config->setResultCacheImpl($serviceLocator->get($options->resultCache));
0 ignored issues
show
Documentation introduced by
$serviceLocator->get($options->resultCache) is of type object|array, but the function expects a object<Doctrine\Common\Cache\Cache>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
71 70
        $sqlLogger = $options->sqlLogger;
72 70
        if (is_string($sqlLogger) and $serviceLocator->has($sqlLogger)) {
73
            $sqlLogger = $serviceLocator->get($sqlLogger);
74
        }
75 70
        $config->setSQLLogger($sqlLogger);
76
77 70
        foreach ($options->types as $name => $class) {
78
            if (Type::hasType($name)) {
79
                Type::overrideType($name, $class);
80
            } else {
81
                Type::addType($name, $class);
82
            }
83 70
        }
84 70
    }
85
86
    /**
87
     * @param  ServiceLocatorInterface $serviceLocator
88
     * @return mixed
89
     * @throws RuntimeException
90
     */
91 72
    public function getOptions(ServiceLocatorInterface $serviceLocator)
92
    {
93 72
        $options = $serviceLocator->get('Config');
94 72
        $options = $options['doctrine'];
95 72
        $options = isset($options['configuration'][$this->name]) ? $options['configuration'][$this->name] : null;
96
97 72
        if (null === $options) {
98
            throw new RuntimeException(
99
                sprintf(
100
                    'Configuration with name "%s" could not be found in "doctrine.configuration".',
101
                    $this->name
102
                )
103
            );
104
        }
105
106 72
        $optionsClass = $this->getOptionsClass();
107
108 72
        return new $optionsClass($options);
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    protected function getOptionsClass()
115
    {
116
        return 'DoctrineModule\Options\Configuration';
117
    }
118
}
119