Passed
Pull Request — master (#137)
by
unknown
37:49 queued 25:37
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 Exception;
8
use Psr\Container\ContainerInterface;
9
use Psr\Log\LoggerInterface;
10
use RuntimeException;
11
use Spiral\Core\FactoryInterface;
12
use Spiral\Database\Config\DatabaseConfig;
13
use Spiral\Database\DatabaseManager;
14
use Spiral\Database\Driver\Driver;
15
use Yiisoft\Aliases\Aliases;
16
17
final class DbalFactory
18
{
19
    /** @var array|DatabaseConfig */
20
    private $dbalConfig;
21
    /** @var LoggerInterface|string|null */
22
    private $logger = null;
23
    private ?ContainerInterface $container = null;
24
25
    /**
26
     * @param array|DatabaseConfig $config
27
     */
28 5
    public function __construct($config)
29
    {
30 5
        if (is_array($config) && array_key_exists('query-logger', $config)) {
31 5
            $this->logger = $config['query-logger'];
32 5
            unset($config['query-logger']);
33
        }
34 5
        $this->dbalConfig = $config;
35
    }
36
37 5
    public function __invoke(ContainerInterface $container)
38
    {
39 5
        $this->container = $container;
40
41 5
        $dbal = new DatabaseManager(
42 5
            $this->prepareConfig($this->dbalConfig),
43 5
            $this->container->get(FactoryInterface::class)
44
        );
45
46 5
        if ($this->logger !== null) {
47 5
            $logger = $this->prepareLogger($this->logger);
48 2
            $dbal->setLogger($logger);
49
            /** Remove when issue is resolved {@link https://github.com/cycle/orm/issues/60} */
50 2
            $drivers = $dbal->getDrivers();
51 2
            array_walk($drivers, static fn (Driver $driver) => $driver->setLogger($logger));
52
        }
53
54 2
        return $dbal;
55
    }
56
57
    /**
58
     * @param LoggerInterface|string $logger
59
     *
60
     * @throws Exception
61
     *
62
     * @return LoggerInterface
63
     */
64 5
    private function prepareLogger($logger): LoggerInterface
65
    {
66 5
        if (is_string($logger)) {
67
            /** @psalm-suppress PossiblyNullReference */
68 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

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