Passed
Push — master ( 5d8af1...e44bd6 )
by Petr
09:24
created

DatabaseSingleton::__clone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Database;
4
5
6
use kalanis\kw_mapper\MapperException;
7
8
9
/**
10
 * Class DatabaseSingleton
11
 * @package kalanis\kw_mapper\Storage\Database
12
 * Singleton to access databases across the mappers
13
 */
14
class DatabaseSingleton
15
{
16
    protected static ?DatabaseSingleton $instance = null;
17
    /** @var ADatabase[] */
18
    private array $database = [];
19
20 56
    public static function getInstance(): self
21
    {
22 56
        if (empty(static::$instance)) {
23 2
            static::$instance = new self();
24
        }
25 56
        return static::$instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::instance could return the type null which is incompatible with the type-hinted return kalanis\kw_mapper\Storag...abase\DatabaseSingleton. Consider adding an additional type-check to rule them out.
Loading history...
26
    }
27
28 2
    protected function __construct()
29
    {
30 2
    }
31
32
    /**
33
     * @codeCoverageIgnore why someone would run that?!
34
     */
35
    private function __clone()
36
    {
37
    }
38
39
    /**
40
     * @param Config $config
41
     * @throws MapperException
42
     * @return ADatabase
43
     */
44 56
    final public function getDatabase(Config $config): ADatabase
45
    {
46 56
        if (empty($this->database[$config->getDriver()])) {
47 5
            $this->database[$config->getDriver()] = $this->getFactory()->getDatabase($config);
48
        }
49 56
        return $this->database[$config->getDriver()];
50
    }
51
52 5
    protected function getFactory(): Factory
53
    {
54 5
        return Factory::getInstance();
55
    }
56
}
57