Passed
Push — master ( 5e9dce...cb103f )
by Aleksei
02:54
created

DbalFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 65
ccs 27
cts 27
cp 1
rs 10
c 2
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareLogger() 0 11 3
A __construct() 0 7 3
A __invoke() 0 15 2
A prepareConfig() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Factory;
6
7
use Cycle\Database\Driver\Driver;
8
use Exception;
9
use Cycle\Database\Config\DatabaseConfig;
10
use Cycle\Database\DatabaseManager;
11
use Psr\Container\ContainerInterface;
12
use Psr\Log\LoggerInterface;
13
use RuntimeException;
14
15
final class DbalFactory
16
{
17
    private array|DatabaseConfig $dbalConfig;
18
19
    /** @var LoggerInterface|string|null */
20
    private mixed $logger = null;
21
22 34
    public function __construct(array|DatabaseConfig $config)
23
    {
24 34
        if (is_array($config) && array_key_exists('query-logger', $config)) {
25 33
            $this->logger = $config['query-logger'];
26 33
            unset($config['query-logger']);
27
        }
28 34
        $this->dbalConfig = $config;
29
    }
30
31 33
    public function __invoke(ContainerInterface $container): DatabaseManager
32
    {
33 33
        $dbal = new DatabaseManager(
34 33
            $this->prepareConfig($this->dbalConfig)
35 33
        );
36
37 33
        if ($this->logger !== null) {
38 5
            $logger = $this->prepareLogger($container, $this->logger);
39 2
            $dbal->setLogger($logger);
40
            /** Remove when issue is resolved {@link https://github.com/cycle/orm/issues/60} */
41 2
            $drivers = $dbal->getDrivers();
42 2
            array_walk($drivers, static fn (Driver $driver) => $driver->setLogger($logger));
43
        }
44
45 30
        return $dbal;
46
    }
47
48
    /**
49
     * @param LoggerInterface|string $logger
50
     *
51
     * @throws Exception
52
     *
53
     * @return LoggerInterface
54
     */
55 5
    private function prepareLogger(ContainerInterface $container, mixed $logger): LoggerInterface
56
    {
57 5
        if (is_string($logger)) {
58 3
            $logger = $container->get($logger);
59
        }
60 4
        if (!$logger instanceof LoggerInterface) {
61 2
            throw new RuntimeException(
62 2
                sprintf('Logger definition should be subclass of %s.', LoggerInterface::class)
63 2
            );
64
        }
65 2
        return $logger;
66
    }
67
68
    /**
69
     * @param array|DatabaseConfig $config
70
     *
71
     * @return DatabaseConfig
72
     */
73 34
    private function prepareConfig(array|DatabaseConfig $config): DatabaseConfig
74
    {
75 34
        if ($config instanceof DatabaseConfig) {
0 ignored issues
show
introduced by
$config is never a sub-type of Cycle\Database\Config\DatabaseConfig.
Loading history...
76 1
            return $config;
77
        }
78
79 34
        return new DatabaseConfig($config);
80
    }
81
}
82