SQLLoggerCollectorFactory::getOptions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.1922

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 7
cts 11
cp 0.6364
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.1922
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineORMModule\Service;
6
7
use Doctrine\DBAL\Logging\DebugStack;
8
use Doctrine\DBAL\Logging\LoggerChain;
9
use DoctrineORMModule\Collector\SQLLoggerCollector;
10
use DoctrineORMModule\Options\SQLLoggerCollectorOptions;
11
use Interop\Container\ContainerInterface;
12
use Laminas\ServiceManager\FactoryInterface;
13
use Laminas\ServiceManager\ServiceLocatorInterface;
14
use RuntimeException;
15
use function sprintf;
16
17
/**
18
 * DBAL Configuration ServiceManager factory
19
 */
20
class SQLLoggerCollectorFactory implements FactoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Laminas\ServiceManager\FactoryInterface has been deprecated with message: Use Laminas\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...
21
{
22
    /** @var string */
23
    protected $name;
24
25 5
    public function __construct(string $name)
26
    {
27 5
        $this->name = $name;
28 5
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33 5
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
34
    {
35 5
        $options = $this->getOptions($container);
36
37
        // @todo always ask the serviceLocator instead? (add a factory?)
38 5
        if ($options->getSqlLogger()) {
39 2
            $debugStackLogger = $container->get($options->getSqlLogger());
40
        } else {
41 3
            $debugStackLogger = new DebugStack();
42
        }
43
44 5
        $configuration = $container->get($options->getConfiguration());
45
46 5
        if ($configuration->getSQLLogger() !== null) {
47 1
            $logger = new LoggerChain();
48 1
            $logger->addLogger($debugStackLogger);
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\DBAL\Logging\LoggerChain::addLogger() has been deprecated with message: Inject list of loggers via constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
49 1
            $logger->addLogger($configuration->getSQLLogger());
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\DBAL\Logging\LoggerChain::addLogger() has been deprecated with message: Inject list of loggers via constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
50 1
            $configuration->setSQLLogger($logger);
51
        } else {
52 4
            $configuration->setSQLLogger($debugStackLogger);
53
        }
54
55 5
        return new SQLLoggerCollector($debugStackLogger, 'doctrine.sql_logger_collector.' . $options->getName());
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61 5
    public function createService(ServiceLocatorInterface $container)
62
    {
63 5
        return $this($container, SQLLoggerCollector::class);
64
    }
65
66
    /**
67
     * @return mixed
68
     *
69
     * @throws RuntimeException
70
     */
71 5
    protected function getOptions(ContainerInterface $serviceLocator)
72
    {
73 5
        $options = $serviceLocator->get('config');
74 5
        $options = $options['doctrine'];
75 5
        $options = $options['sql_logger_collector'][$this->name] ?? null;
76
77 5
        if ($options === null) {
78
            throw new RuntimeException(
79
                sprintf(
80
                    'Configuration with name "%s" could not be found in "doctrine.sql_logger_collector".',
81
                    $this->name
82
                )
83
            );
84
        }
85
86 5
        $optionsClass = $this->getOptionsClass();
87
88 5
        return new $optionsClass($options);
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94 5
    protected function getOptionsClass()
95
    {
96 5
        return SQLLoggerCollectorOptions::class;
97
    }
98
}
99