Completed
Pull Request — master (#476)
by Bruno
14:24
created

DBALConnectionFactory::createService()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4.0119

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 32
ccs 20
cts 22
cp 0.9091
rs 8.5806
cc 4
eloc 19
nc 8
nop 1
crap 4.0119
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 Doctrine\DBAL\DriverManager;
23
use Doctrine\DBAL\Types\Type;
24
use DoctrineModule\Service\AbstractFactory;
25
use Zend\ServiceManager\ServiceLocatorInterface;
26
27
/**
28
 * DBAL Connection ServiceManager factory
29
 *
30
 * @license MIT
31
 * @link    http://www.doctrine-project.org/
32
 * @author  Kyle Spraggs <[email protected]>
33
 */
34
class DBALConnectionFactory extends AbstractFactory
35
{
36
    /**
37
     * {@inheritDoc}
38
     * @return \Doctrine\DBAL\Connection
39
     */
40 58
    public function createService(ServiceLocatorInterface $sl)
41
    {
42
        /** @var $options \DoctrineORMModule\Options\DBALConnection */
43 58
        $options = $this->getOptions($sl, 'connection');
44 58
        $pdo     = $options->getPdo();
45
46 58
        if (is_string($pdo)) {
47
            $pdo = $sl->get($pdo);
48
        }
49
50
        $params = array(
51 58
            'driverClass'  => $options->getDriverClass(),
52 58
            'wrapperClass' => $options->getWrapperClass(),
53 58
            'pdo'          => $pdo,
54 58
        );
55 58
        $params = array_merge($params, $options->getParams());
56
57 58
        $configuration = $sl->get($options->getConfiguration());
58 58
        $eventManager  = $sl->get($options->getEventManager());
59
60 58
        $connection = DriverManager::getConnection($params, $configuration, $eventManager);
0 ignored issues
show
Documentation introduced by
$configuration is of type object|array, but the function expects a null|object<Doctrine\DBAL\Configuration>.

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...
Documentation introduced by
$eventManager is of type object|array, but the function expects a null|object<Doctrine\Common\EventManager>.

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...
61 58
        $platform = $connection->getDatabasePlatform();
62 58
        foreach ($options->getDoctrineTypeMappings() as $dbType => $doctrineType) {
63 2
            $platform->registerDoctrineTypeMapping($dbType, $doctrineType);
64 58
        }
65
66 58
        foreach ($options->getDoctrineCommentedTypes() as $type) {
67 1
            $platform->markDoctrineTypeCommented(Type::getType($type));
68 58
        }
69
70 58
        return $connection;
71
    }
72
73
    /**
74
     * Get the class name of the options associated with this factory.
75
     *
76
     * @return string
77
     */
78 58
    public function getOptionsClass()
79
    {
80 58
        return 'DoctrineORMModule\Options\DBALConnection';
81
    }
82
}
83