Passed
Pull Request — master (#41)
by
unknown
11:38
created

DbalFactory::prepareLogger()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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

64
            return $this->container->/** @scrutinizer ignore-call */ 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...
65
        }
66
        if (is_object($logger) && method_exists($logger, '__invoke')) {
67
            return $logger($this->container);
68
        }
69
        throw new InvalidArgumentException('Invalid logger.');
70
    }
71
72
    /**
73
     * @param array|DatabaseConfig $config
74
     * @return DatabaseConfig
75
     */
76
    private function prepareConfig($config): DatabaseConfig
77
    {
78
        if ($config instanceof DatabaseConfig) {
79
            return $config;
80
        }
81
        if (isset($config['connections'])) {
82
            // prepare connections
83
            foreach ($config['connections'] as &$connection) {
84
                $connection = $this->prepareConnection($connection);
85
            }
86
        }
87
88
        return new DatabaseConfig($config);
89
    }
90
91
    private function prepareConnection(array $connection): array
92
    {
93
        // if connection option contain alias in path
94
        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
        return $connection;
100
    }
101
102
    private function getAlias(string $alias): string
103
    {
104
        return $this->container->get(Aliases::class)->get($alias);
105
    }
106
}
107