Completed
Pull Request — master (#527)
by
unknown
14:10 queued 08:39
created

SQLLoggerCollectorFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 83.78%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 88
ccs 31
cts 37
cp 0.8378
rs 10
c 0
b 0
f 0

5 Methods

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