DatabaseConfig   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 106
ccs 24
cts 24
cp 1
rs 10
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getDriver() 0 22 4
A getDatabases() 0 8 2
A getDatabase() 0 13 2
A hasDatabase() 0 3 1
A getDrivers() 0 8 2
A getDefaultDatabase() 0 3 1
A hasDriver() 0 3 2
A __construct() 0 8 1
1
<?php
2
3
/**
4
 * This file is part of Cycle ORM package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Database\Config;
13
14
use Cycle\Database\Driver\DriverInterface;
15
use Cycle\Database\Exception\ConfigException;
16
use Cycle\Database\NamedInterface;
17
use Spiral\Core\InjectableConfig;
18
use Spiral\Core\Traits\Config\AliasTrait;
19
20
final class DatabaseConfig extends InjectableConfig
21
{
22
    use AliasTrait;
0 ignored issues
show
Deprecated Code introduced by
The trait Spiral\Core\Traits\Config\AliasTrait has been deprecated: to be removed in future releases. ( Ignorable by Annotation )

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

22
    use /** @scrutinizer ignore-deprecated */ AliasTrait;

This trait has been deprecated. The supplier of the trait has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the trait will be removed and what other trait to use instead.

Loading history...
23
24
    public const CONFIG = 'database';
25
    public const DEFAULT_DATABASE = 'default';
26
27
    public function __construct(array $config = [])
28
    {
29
        parent::__construct(\array_merge([
30
            'default' => self::DEFAULT_DATABASE,
31
            'aliases' => [],
32
            'databases' => [],
33
            'connections' => [],
34
        ], $config));
35
    }
36
37
    public function getDefaultDatabase(): string
38
    {
39
        return $this->config['default'] ?? 'default';
40
    }
41
42 2
    /**
43
     * Get named list of all databases.
44 2
     *
45
     * @return DatabasePartial[]
46
     */
47
    public function getDatabases(): array
48
    {
49
        $result = [];
50
        foreach (\array_keys($this->config['databases'] ?? []) as $database) {
51
            $result[$database] = $this->getDatabase($database);
52 6
        }
53
54 6
        return $result;
55 6
    }
56 6
57
    /**
58
     * Get names list of all driver connections.
59 6
     *
60
     * @return DriverInterface[]
61
     */
62
    public function getDrivers(): array
63
    {
64
        $result = [];
65
        foreach (\array_keys($this->config['connections'] ?? $this->config['drivers'] ?? []) as $driver) {
66
            $result[$driver] = $this->getDriver($driver);
67 6
        }
68
69 6
        return $result;
70 6
    }
71 4
72
    public function hasDatabase(string $database): bool
73
    {
74 6
        return isset($this->config['databases'][$database]);
75
    }
76
77
    /**
78
     * @throws ConfigException
79
     */
80
    public function getDatabase(string $database): DatabasePartial
81
    {
82 26
        if (!$this->hasDatabase($database)) {
83
            throw new ConfigException("Undefined database `{$database}`");
84 26
        }
85
86
        $config = $this->config['databases'][$database];
87
88
        return new DatabasePartial(
89
            $database,
90
            $config['tablePrefix'] ?? $config['prefix'] ?? '',
91
            $config['connection'] ?? $config['write'] ?? $config['driver'],
92
            $config['readConnection'] ?? $config['read'] ?? $config['readDriver'] ?? null,
93
        );
94 20
    }
95
96 20
    public function hasDriver(string $driver): bool
97 2
    {
98
        return isset($this->config['connections'][$driver]) || isset($this->config['drivers'][$driver]);
99
    }
100 18
101
    /**
102 18
     * @throws ConfigException
103
     */
104 18
    public function getDriver(string $driver): DriverInterface
105 18
    {
106 18
        if (!$this->hasDriver($driver)) {
107
            throw new ConfigException("Undefined driver `{$driver}`");
108
        }
109
110
        $config = $this->config['connections'][$driver] ?? $this->config['drivers'][$driver];
111
112
        if ($config instanceof DriverConfig) {
113
            $driverObject = $config->driver::create($config);
114
115 28
            if ($driverObject instanceof NamedInterface) {
116
                return $driverObject->withName($driver);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $driverObject->withName($driver) returns the type Cycle\Database\NamedInterface which is incompatible with the type-hinted return Cycle\Database\Driver\DriverInterface.
Loading history...
117 28
            }
118
119
            return $driverObject;
120
        }
121
122
        throw new \InvalidArgumentException(
123
            \vsprintf('Driver config must be an instance of %s, but %s passed', [
124
                DriverConfig::class,
125
                \get_debug_type($config),
126
            ]),
127 26
        );
128
    }
129
}
130