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

SQLLoggerCollectorFactory::getOptions()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.576

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 9
cts 15
cp 0.6
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 1
crap 3.576
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 Zend\ServiceManager\FactoryInterface;
23
use Zend\ServiceManager\ServiceLocatorInterface;
24
use RuntimeException;
25
use DoctrineORMModule\Collector\SQLLoggerCollector;
26
use Doctrine\DBAL\Logging\DebugStack;
27
use Doctrine\DBAL\Logging\LoggerChain;
28
29
/**
30
 * DBAL Configuration ServiceManager factory
31
 *
32
 * @license MIT
33
 * @link    http://www.doctrine-project.org/
34
 * @author  Marco Pivetta <[email protected]>
35
 */
36
class SQLLoggerCollectorFactory implements FactoryInterface
37
{
38
    /**
39
     * @var string
40
     */
41
    protected $name;
42
43
    /**
44
     * @param string $name
45
     */
46 5
    public function __construct($name)
47
    {
48 5
        $this->name = $name;
49 5
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54 5
    public function createService(ServiceLocatorInterface $serviceLocator)
55
    {
56
        /** @var $options \DoctrineORMModule\Options\SQLLoggerCollectorOptions */
57 5
        $options = $this->getOptions($serviceLocator);
58
59
        // @todo always ask the serviceLocator instead? (add a factory?)
60 5
        if ($options->getSqlLogger()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $options->getSqlLogger() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
61 2
            $debugStackLogger = $serviceLocator->get($options->getSqlLogger());
62 2
        } else {
63 3
            $debugStackLogger = new DebugStack();
64
        }
65
66
        /* @var $configuration \Doctrine\ORM\Configuration */
67 5
        $configuration = $serviceLocator->get($options->getConfiguration());
68
69 5
        if (null !== $configuration->getSQLLogger()) {
70 1
            $logger = new LoggerChain();
71 1
            $logger->addLogger($debugStackLogger);
0 ignored issues
show
Documentation introduced by
$debugStackLogger is of type object|array, but the function expects a object<Doctrine\DBAL\Logging\SQLLogger>.

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...
72 1
            $logger->addLogger($configuration->getSQLLogger());
73 1
            $configuration->setSQLLogger($logger);
74 1
        } else {
75 4
            $configuration->setSQLLogger($debugStackLogger);
0 ignored issues
show
Documentation introduced by
$debugStackLogger is of type object|array, but the function expects a null|object<Doctrine\DBAL\Logging\SQLLogger>.

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...
76
        }
77
78 5
        return new SQLLoggerCollector($debugStackLogger, 'doctrine.sql_logger_collector.' . $options->getName());
0 ignored issues
show
Documentation introduced by
$debugStackLogger is of type object|array, but the function expects a object<Doctrine\DBAL\Logging\DebugStack>.

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...
79
    }
80
81
    /**
82
     * @param  ServiceLocatorInterface $serviceLocator
83
     * @return mixed
84
     * @throws RuntimeException
85
     */
86 5
    protected function getOptions(ServiceLocatorInterface $serviceLocator)
87
    {
88 5
        $options = $serviceLocator->get('Config');
89 5
        $options = $options['doctrine'];
90 5
        $options = isset($options['sql_logger_collector'][$this->name])
91 5
            ? $options['sql_logger_collector'][$this->name]
92 5
            : null;
93
94 5
        if (null === $options) {
95
            throw new RuntimeException(
96
                sprintf(
97
                    'Configuration with name "%s" could not be found in "doctrine.sql_logger_collector".',
98
                    $this->name
99
                )
100
            );
101
        }
102
103 5
        $optionsClass = $this->getOptionsClass();
104
105 5
        return new $optionsClass($options);
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111 5
    protected function getOptionsClass()
112
    {
113 5
        return 'DoctrineORMModule\Options\SQLLoggerCollectorOptions';
114
    }
115
}
116