Passed
Push — master ( 23ee75...fcbf2b )
by Alexander
69:22 queued 67:57
created

DbalFactory::prepareConnection()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 12
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Cycle\Factory;
4
5
use Psr\Container\ContainerInterface;
6
use Spiral\Database\Config\DatabaseConfig;
7
use Spiral\Database\DatabaseManager;
8
use Yiisoft\Aliases\Aliases;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Aliases\Aliases was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class DbalFactory
11
{
12
    private $params;
13
14
    /** @var ContainerInterface */
15
    protected $container;
16
17
    public function __construct($params)
18
    {
19
        $this->params = $params;
20
    }
21
22
    public function __invoke(ContainerInterface $container)
23
    {
24
        $this->container = $container;
25
        $conf = $this->prepareConfig($this->params);
26
        return new DatabaseManager($conf);
27
    }
28
29
    protected function prepareConfig($params): DatabaseConfig
30
    {
31
        if ($params instanceof DatabaseConfig) {
32
            return $params;
33
        }
34
        if (isset($params['connections'])) {
35
            // prepare connections
36
            foreach ($params['connections'] as &$connection) {
37
                $connection = $this->prepareConnection($connection);
38
            }
39
        }
40
41
        return new DatabaseConfig($params);
42
    }
43
44
    protected function prepareConnection(array $connection): array
45
    {
46
        // if connection option contain alias in path
47
        if (isset($connection['connection']) && preg_match('/^(?<proto>\w+:)?@/', $connection['connection'], $m)) {
48
            $proto = $m['proto'];
49
            $path = $this->getAlias(substr($connection['connection'], strlen($proto)));
50
            $connection['connection'] = $proto . $path;
51
        }
52
        return $connection;
53
    }
54
55
    protected function getAlias(string $alias): string
56
    {
57
        return $this->container->get(Aliases::class)->get($alias, true);
58
    }
59
}
60