Passed
Push — master ( fe2662...9d01c9 )
by Aleksei
06:53
created

DbalFactory::prepareConfig()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 10
cc 4
nc 3
nop 1
crap 4.0466
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Factory;
6
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\Container\ContainerInterface;
9
use Psr\Log\LoggerInterface;
10
use RuntimeException;
11
use Spiral\Database\Config\DatabaseConfig;
12
use Spiral\Database\DatabaseManager;
13
use Spiral\Database\Driver\Driver;
14
use Yiisoft\Aliases\Aliases;
15
16
final class DbalFactory
17
{
18
    /** @var array|DatabaseConfig */
19
    private $dbalConfig;
20
    /** @var null|string|LoggerInterface */
21
    private $logger = null;
22
    private ?ContainerInterface $container = null;
23
24
    /**
25
     * @param array|DatabaseConfig $config
26
     */
27 5
    public function __construct($config)
28
    {
29 5
        if (is_array($config) && array_key_exists('query-logger', $config)) {
30 5
            $this->logger = $config['query-logger'];
31 5
            unset($config['query-logger']);
32
        }
33 5
        $this->dbalConfig = $config;
34 5
    }
35
36 5
    public function __invoke(ContainerInterface $container)
37
    {
38 5
        $this->container = $container;
39 5
        $conf = $this->prepareConfig($this->dbalConfig);
40 5
        $dbal = new DatabaseManager($conf);
41
42 5
        if ($this->logger !== null) {
43 5
            $logger = $this->prepareLogger($this->logger);
44 2
            $dbal->setLogger($logger);
45
            /** Remove when issue is resolved {@link https://github.com/cycle/orm/issues/60} */
46 2
            $drivers = $dbal->getDrivers();
47 2
            array_walk($drivers, static fn (Driver $driver) => $driver->setLogger($logger));
48
        }
49
50 2
        return $dbal;
51
    }
52
53
    /**
54
     * @param string|LoggerInterface $logger
55
     * @return LoggerInterface
56
     * @throws RuntimeException
57
     * @throws ContainerExceptionInterface
58
     */
59 5
    private function prepareLogger($logger): LoggerInterface
60
    {
61 5
        if (is_string($logger)) {
62 3
            $logger = $this->container->get($logger);
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            /** @scrutinizer ignore-call */ 
63
            $logger = $this->container->get($logger);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
        }
64 4
        if (!$logger instanceof LoggerInterface) {
65 2
            throw new RuntimeException(
66 2
                sprintf('Logger definition should be subclass of %s.', LoggerInterface::class)
67
            );
68
        }
69 2
        return $logger;
70
    }
71
72
    /**
73
     * @param array|DatabaseConfig $config
74
     * @return DatabaseConfig
75
     */
76 5
    private function prepareConfig($config): DatabaseConfig
77
    {
78 5
        if ($config instanceof DatabaseConfig) {
79
            return $config;
80
        }
81 5
        if (isset($config['connections'])) {
82
            // prepare connections
83 5
            foreach ($config['connections'] as &$connection) {
84 5
                $connection = $this->prepareConnection($connection);
85
            }
86
        }
87
88 5
        return new DatabaseConfig($config);
89
    }
90
91 5
    private function prepareConnection(array $connection): array
92
    {
93
        // if connection option contain alias in path
94 5
        if (isset($connection['connection']) && preg_match('/^(?<proto>\w+:)?@/', $connection['connection'], $m)) {
95
            $proto = $m['proto'];
96
            $path = $this->getAlias(substr($connection['connection'], strlen($proto)));
97
            $connection['connection'] = $proto . $path;
98
        }
99 5
        return $connection;
100
    }
101
102
    private function getAlias(string $alias): string
103
    {
104
        return $this->container->get(Aliases::class)->get($alias);
105
    }
106
}
107