Passed
Pull Request — master (#5)
by
unknown
01:21
created

DbalConfig   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 37
ccs 0
cts 22
cp 0
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A prepareConfig() 0 3 1
A getAlias() 0 3 1
A setConnections() 0 9 4
1
<?php
2
3
namespace Yiisoft\Yii\Cycle;
4
5
use Spiral\Database\Config\DatabaseConfig;
6
use Yiisoft\Aliases\Aliases;
7
use Yiisoft\Yii\Cycle\Config\BaseConfig;
8
9
/**
10
 * @property-read string $default
11
 * @property-read array  $aliases
12
 * @property-read array  $databases
13
 * @property-read array  $connections
14
 */
15
class DbalConfig extends BaseConfig
16
{
17
    protected $default     = '';
18
    protected $aliases     = [];
19
    protected $databases   = [];
20
    protected $connections = [];
21
22
    // private property will be ignored in toArray() method
23
    /** @var Aliases */
24
    private $objAliases;
25
26
    public function __construct(Aliases $aliases)
27
    {
28
        $this->objAliases = $aliases;
29
    }
30
31
    public function prepareConfig(): DatabaseConfig
32
    {
33
        return new DatabaseConfig($this->toArray());
34
    }
35
36
    protected function setConnections($data): void
37
    {
38
        $this->connections = $data;
0 ignored issues
show
Bug introduced by
The property connections is declared read-only in Yiisoft\Yii\Cycle\DbalConfig.
Loading history...
39
        foreach ($this->connections as &$connection) {
40
            // if connection option contain alias in path
41
            if (isset($connection['connection']) && preg_match('/^(?<proto>\w+:)?@/', $connection['connection'], $m)) {
42
                $proto = $m['proto'];
43
                $path = $this->getAlias(substr($connection['connection'], strlen($proto)));
44
                $connection['connection'] = $proto . $path;
45
            }
46
        }
47
    }
48
49
    protected function getAlias(string $alias): string
50
    {
51
        return $this->objAliases->get($alias, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->objAliases->get($alias, true) could return the type boolean which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
52
    }
53
}
54