Completed
Pull Request — master (#459)
by Vytautas
14:01
created

SQLLoggerCollectorFactory::createService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
0 ignored issues
show
Deprecated Code introduced by
The interface Zend\ServiceManager\FactoryInterface has been deprecated with message: Use Zend\ServiceManager\Factory\FactoryInterface instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
38
{
39
    /**
40
     * @var string
41
     */
42
    protected $name;
43
44
    /**
45
     * @param string $name
46 5
     */
47
    public function __construct($name)
48 5
    {
49 5
        $this->name = $name;
50
    }
51
52
    /**
53
     * {@inheritDoc}
54 5
     */
55
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
56
    {
57 5
        /** @var $options \DoctrineORMModule\Options\SQLLoggerCollectorOptions */
58
        $options = $this->getOptions($container);
59
60 5
        // @todo always ask the serviceLocator instead? (add a factory?)
61 2
        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 3
        } else {
64
            $debugStackLogger = new DebugStack();
65
        }
66
67 5
        /* @var $configuration \Doctrine\ORM\Configuration */
68
        $configuration = $container->get($options->getConfiguration());
69 5
70 1
        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 4
        } else {
76
            $configuration->setSQLLogger($debugStackLogger);
77
        }
78 5
79
        return new SQLLoggerCollector($debugStackLogger, 'doctrine.sql_logger_collector.' . $options->getName());
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85
    public function createService(ServiceLocatorInterface $container)
86 5
    {
87
        return $this($container, SQLLoggerCollector::class);
88 5
    }
89 5
90 5
    /**
91 5
     * @param  ContainerInterface $serviceLocator
92 5
     * @return mixed
93
     * @throws RuntimeException
94 5
     */
95
    protected function getOptions(ContainerInterface $serviceLocator)
96
    {
97
        $options = $serviceLocator->get('Config');
98
        $options = $options['doctrine'];
99
        $options = isset($options['sql_logger_collector'][$this->name])
100
            ? $options['sql_logger_collector'][$this->name]
101
            : null;
102
103 5
        if (null === $options) {
104
            throw new RuntimeException(
105 5
                sprintf(
106
                    'Configuration with name "%s" could not be found in "doctrine.sql_logger_collector".',
107
                    $this->name
108
                )
109
            );
110
        }
111 5
112
        $optionsClass = $this->getOptionsClass();
113 5
114
        return new $optionsClass($options);
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120
    protected function getOptionsClass()
121
    {
122
        return 'DoctrineORMModule\Options\SQLLoggerCollectorOptions';
123
    }
124
}
125