Passed
Push — master ( 705ab8...a10add )
by Rustam
13:49 queued 11:19
created

DbalFactory::prepareConnection()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
ccs 3
cts 6
cp 0.5
rs 10
cc 3
nc 2
nop 1
crap 4.125
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 5
    public function __construct(array|DatabaseConfig $config)
23
    {
24 5
        if (is_array($config) && array_key_exists('query-logger', $config)) {
25 5
            $this->logger = $config['query-logger'];
26 5
            unset($config['query-logger']);
27
        }
28 5
        $this->dbalConfig = $config;
29
    }
30
31 5
    public function __invoke(ContainerInterface $container): DatabaseManager
32
    {
33 5
        $dbal = new DatabaseManager(
34 5
            $this->prepareConfig($this->dbalConfig)
35 5
        );
36
37 5
        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 2
        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 5
    private function prepareConfig(array|DatabaseConfig $config): DatabaseConfig
74
    {
75 5
        if ($config instanceof DatabaseConfig) {
0 ignored issues
show
introduced by
$config is never a sub-type of Cycle\Database\Config\DatabaseConfig.
Loading history...
76
            return $config;
77
        }
78
79 5
        return new DatabaseConfig($config);
80
    }
81
}
82