Completed
Pull Request — master (#459)
by Vytautas
13:28 queued 09:14
created

SQLLoggerCollectorFactory::__invoke()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
ccs 15
cts 15
cp 1
rs 8.8571
cc 3
eloc 15
nc 4
nop 3
crap 3
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 Zend\ServiceManager\FactoryInterface;
24
use Zend\ServiceManager\ServiceLocatorInterface;
25
use RuntimeException;
26
use DoctrineORMModule\Collector\SQLLoggerCollector;
27
use Doctrine\DBAL\Logging\DebugStack;
28
use Doctrine\DBAL\Logging\LoggerChain;
29
30
/**
31
 * DBAL Configuration ServiceManager factory
32
 *
33
 * @license MIT
34
 * @link    http://www.doctrine-project.org/
35
 * @author  Marco Pivetta <[email protected]>
36
 */
37
class SQLLoggerCollectorFactory implements FactoryInterface
38
{
39
    /**
40
     * @var string
41
     */
42
    protected $name;
43
44
    /**
45
     * @param string $name
46
     */
47 5
    public function __construct($name)
48
    {
49 5
        $this->name = $name;
50 5
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55 5
    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...
56
    {
57
        /** @var $options \DoctrineORMModule\Options\SQLLoggerCollectorOptions */
58 5
        $options = $this->getOptions($container);
59
60
        // @todo always ask the serviceLocator instead? (add a factory?)
61 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...
62 2
            $debugStackLogger = $container->get($options->getSqlLogger());
63 2
        } else {
64 3
            $debugStackLogger = new DebugStack();
65
        }
66
67
        /* @var $configuration \Doctrine\ORM\Configuration */
68 5
        $configuration = $container->get($options->getConfiguration());
69
70 5
        if (null !== $configuration->getSQLLogger()) {
71 1
            $logger = new LoggerChain();
72 1
            $logger->addLogger($debugStackLogger);
73 1
            $logger->addLogger($configuration->getSQLLogger());
74 1
            $configuration->setSQLLogger($logger);
75 1
        } else {
76 4
            $configuration->setSQLLogger($debugStackLogger);
77
        }
78
79 5
        return new SQLLoggerCollector($debugStackLogger, 'doctrine.sql_logger_collector.' . $options->getName());
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85 5
    public function createService(ServiceLocatorInterface $container)
86
    {
87 5
        return $this($container, SQLLoggerCollector::class);
88
    }
89
90
    /**
91
     * @param  ContainerInterface $serviceLocator
92
     * @return mixed
93
     * @throws RuntimeException
94
     */
95 5
    protected function getOptions(ContainerInterface $serviceLocator)
96
    {
97 5
        $options = $serviceLocator->get('Config');
98 5
        $options = $options['doctrine'];
99 5
        $options = isset($options['sql_logger_collector'][$this->name])
100 5
            ? $options['sql_logger_collector'][$this->name]
101 5
            : null;
102
103 5
        if (null === $options) {
104
            throw new RuntimeException(
105
                sprintf(
106
                    'Configuration with name "%s" could not be found in "doctrine.sql_logger_collector".',
107
                    $this->name
108
                )
109
            );
110
        }
111
112 5
        $optionsClass = $this->getOptionsClass();
113
114 5
        return new $optionsClass($options);
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120 5
    protected function getOptionsClass()
121
    {
122 5
        return 'DoctrineORMModule\Options\SQLLoggerCollectorOptions';
123
    }
124
}
125