Completed
Pull Request — master (#562)
by Oliver
04:48 queued 02:59
created

SQLLoggerCollectorFactory::__invoke()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

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