DatabaseSingleton   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 6 2
A getFactory() 0 3 1
A __construct() 0 2 1
A getDatabase() 0 6 2
A __clone() 0 2 1
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