Completed
Pull Request — master (#602)
by Tom
10:09
created

SQLLoggerCollectorFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 87.1%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 86
ccs 27
cts 31
cp 0.871
rs 10
c 0
b 0
f 0
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 Doctrine\ORM\Configuration;
10
use DoctrineORMModule\Collector\SQLLoggerCollector;
11
use DoctrineORMModule\Options\SQLLoggerCollectorOptions;
12
use Interop\Container\ContainerInterface;
13
use Laminas\ServiceManager\FactoryInterface;
14
use Laminas\ServiceManager\ServiceLocatorInterface;
15
use RuntimeException;
16
use function assert;
17
use function sprintf;
18
19
/**
20
 * DBAL Configuration ServiceManager factory
21
 *
22
 * @link    http://www.doctrine-project.org/
23
 */
24
class SQLLoggerCollectorFactory implements FactoryInterface
25
{
26
    protected string $name;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
27
28
    public function __construct(string $name)
29
    {
30
        $this->name = $name;
31 5
    }
32
33 5
    /**
34 5
     * {@inheritDoc}
35
     */
36
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
37
    {
38
        $options = $this->getOptions($container);
39 5
        assert($options instanceof SQLLoggerCollectorOptions);
40
41
        // @todo always ask the serviceLocator instead? (add a factory?)
42 5
        if ($options->getSqlLogger()) {
43
            $debugStackLogger = $container->get($options->getSqlLogger());
44
        } else {
45 5
            $debugStackLogger = new DebugStack();
46 2
        }
47
48 3
        $configuration = $container->get($options->getConfiguration());
49
        assert($configuration instanceof Configuration);
50
51
        if ($configuration->getSQLLogger() !== null) {
52 5
            $logger = new LoggerChain();
53
            $logger->addLogger($debugStackLogger);
54 5
            $logger->addLogger($configuration->getSQLLogger());
55 1
            $configuration->setSQLLogger($logger);
56 1
        } else {
57 1
            $configuration->setSQLLogger($debugStackLogger);
58 1
        }
59
60 4
        return new SQLLoggerCollector($debugStackLogger, 'doctrine.sql_logger_collector.' . $options->getName());
61
    }
62
63 5
    /**
64
     * {@inheritDoc}
65
     */
66
    public function createService(ServiceLocatorInterface $container)
67
    {
68
        return $this($container, SQLLoggerCollector::class);
69 5
    }
70
71 5
    /**
72
     * @return mixed
73
     *
74
     * @throws RuntimeException
75
     */
76
    protected function getOptions(ContainerInterface $serviceLocator)
77
    {
78
        $options = $serviceLocator->get('config');
79 5
        $options = $options['doctrine'];
80
        $options = $options['sql_logger_collector'][$this->name] ?? null;
81 5
82 5
        if ($options === null) {
83 5
            throw new RuntimeException(
84
                sprintf(
85 5
                    'Configuration with name "%s" could not be found in "doctrine.sql_logger_collector".',
86
                    $this->name
87
                )
88
            );
89
        }
90
91
        $optionsClass = $this->getOptionsClass();
92
93
        return new $optionsClass($options);
94 5
    }
95
96 5
    /**
97
     * {@inheritDoc}
98
     */
99
    protected function getOptionsClass()
100
    {
101
        return SQLLoggerCollectorOptions::class;
102 5
    }
103
}
104