Passed
Pull Request — master (#368)
by Sergei
02:42
created

ConnectionProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 7
c 2
b 0
f 0
dl 0
loc 39
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A set() 0 3 1
A all() 0 3 1
A has() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord;
6
7
use Yiisoft\Db\Connection\ConnectionInterface;
8
9
/**
10
 * ConnectionProvider is used to manage DB connections.
11
 */
12
final class ConnectionProvider
13
{
14
    public const DEFAULT = 'default';
15
16
    /** @var ConnectionInterface[] $connections */
17
    private static array $connections = [];
18
19
    /**
20
     * Returns all connections.
21
     *
22
     * @return ConnectionInterface[]
23
     */
24
    public static function all(): array
25
    {
26
        return self::$connections;
27
    }
28
29
    /**
30
     * Returns a connection by name.
31
     */
32
    public static function get(string $name = self::DEFAULT): ConnectionInterface
33
    {
34
        return self::$connections[$name];
35
    }
36
37
    /**
38
     * Checks if a connection name exists.
39
     */
40
    public static function has(string $name = self::DEFAULT): bool
41
    {
42
        return isset(self::$connections[$name]);
43
    }
44
45
    /**
46
     * Sets a connection by name.
47
     */
48
    public static function set(ConnectionInterface $connection, string $name = self::DEFAULT): void
49
    {
50
        self::$connections[$name] = $connection;
51
    }
52
}
53