Passed
Push — 2.x ( b50642...2496d7 )
by Aleksei
17:51
created

DatabaseConfig::offsetUnset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 1
b 0
f 0
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
    /**
28
     * @internal
29
     *
30
     * @var array
31
     */
32
    protected $config = [
33
        'default'     => self::DEFAULT_DATABASE,
34
        'aliases'     => [],
35
        'databases'   => [],
36
        'connections' => [],
37
    ];
38
39
    /**
40
     * @return string
41
     */
42 2
    public function getDefaultDatabase(): string
43
    {
44 2
        return $this->config['default'] ?? 'default';
45
    }
46
47
    /**
48
     * Get named list of all databases.
49
     *
50
     * @return DatabasePartial[]
51
     */
52 6
    public function getDatabases(): array
53
    {
54 6
        $result = [];
55 6
        foreach (array_keys($this->config['databases'] ?? []) as $database) {
56 6
            $result[$database] = $this->getDatabase($database);
57
        }
58
59 6
        return $result;
60
    }
61
62
    /**
63
     * Get names list of all driver connections.
64
     *
65
     * @return DriverInterface[]
66
     */
67 6
    public function getDrivers(): array
68
    {
69 6
        $result = [];
70 6
        foreach (array_keys($this->config['connections'] ?? $this->config['drivers'] ?? []) as $driver) {
71 4
            $result[$driver] = $this->getDriver($driver);
72
        }
73
74 6
        return $result;
75
    }
76
77
    /**
78
     * @param string $database
79
     *
80
     * @return bool
81
     */
82 26
    public function hasDatabase(string $database): bool
83
    {
84 26
        return isset($this->config['databases'][$database]);
85
    }
86
87
    /**
88
     * @param string $database
89
     *
90
     * @throws ConfigException
91
     *
92
     * @return DatabasePartial
93
     */
94 20
    public function getDatabase(string $database): DatabasePartial
95
    {
96 20
        if (!$this->hasDatabase($database)) {
97 2
            throw new ConfigException("Undefined database `{$database}`");
98
        }
99
100 18
        $config = $this->config['databases'][$database];
101
102 18
        return new DatabasePartial(
103
            $database,
104 18
            $config['tablePrefix'] ?? $config['prefix'] ?? '',
105 18
            $config['connection'] ?? $config['write'] ?? $config['driver'],
106 18
            $config['readConnection'] ?? $config['read'] ?? $config['readDriver'] ?? null
107
        );
108
    }
109
110
    /**
111
     * @param string $driver
112
     *
113
     * @return bool
114
     */
115 28
    public function hasDriver(string $driver): bool
116
    {
117 28
        return isset($this->config['connections'][$driver]) || isset($this->config['drivers'][$driver]);
118
    }
119
120
    /**
121
     * @param string $driver
122
     *
123
     * @throws ConfigException
124
     *
125
     * @return DriverInterface
126
     */
127 26
    public function getDriver(string $driver): DriverInterface
128
    {
129 26
        if (!$this->hasDriver($driver)) {
130 6
            throw new ConfigException("Undefined driver `{$driver}`");
131
        }
132
133 20
        $config = $this->config['connections'][$driver] ?? $this->config['drivers'][$driver];
134
135 20
        if ($config instanceof DriverConfig) {
136 18
            $driverObject = $config->driver::create($config);
137
138 18
            if ($driverObject instanceof NamedInterface) {
139 18
                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...
140
            }
141
142
            return $driverObject;
143
        }
144
145 2
        throw new \InvalidArgumentException(
146 2
            \vsprintf('Driver config must be an instance of %s, but %s passed', [
147 2
                DriverConfig::class,
148 2
                \get_debug_type($config),
149
            ])
150
        );
151
    }
152
}
153