Completed
Pull Request — master (#479)
by Michał
04:19
created

SQLLoggerCollectorFactory::__invoke()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 25
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 Doctrine\DBAL\Logging\DebugStack;
23
use Doctrine\DBAL\Logging\LoggerChain;
24
use Doctrine\ORM\Configuration;
25
use DoctrineORMModule\Collector\SQLLoggerCollector;
26
use DoctrineORMModule\Options\SQLLoggerCollectorOptions;
27
use Interop\Container\ContainerInterface;
28
use Zend\ServiceManager\Factory\FactoryInterface;
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)
56
    {
57 5
        $options = $this->getOptions($container);
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 = $container->get($options->getSqlLogger());
62 2
        } else {
63 3
            $debugStackLogger = new DebugStack();
64
        }
65
66
        /** @var $configuration Configuration */
67 5
        $configuration = $container->get($options->getConfiguration());
68
69 5
        if (null !== $configuration->getSQLLogger()) {
70 1
            $logger = new LoggerChain();
71 1
            $logger->addLogger($debugStackLogger);
72 1
            $logger->addLogger($configuration->getSQLLogger());
73 1
            $configuration->setSQLLogger($logger);
74 1
        } else {
75 4
            $configuration->setSQLLogger($debugStackLogger);
76
        }
77
78 5
        return new SQLLoggerCollector($debugStackLogger, 'doctrine.sql_logger_collector.' . $options->getName());
79
    }
80
81
    /**
82
     * @param  ContainerInterface $serviceLocator
83
     * @return SQLLoggerCollectorOptions
84
     * @throws \RuntimeException
85
     */
86 5
    protected function getOptions(ContainerInterface $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 SQLLoggerCollectorOptions::class;
114
    }
115
}
116