Passed
Push — master ( 6139d1...678a4c )
by Petr
13:22 queued 10:27
created

DatabaseSingleton   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 6 2
A getFactory() 0 3 1
A getDatabase() 0 6 2
A __construct() 0 2 1
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
    /** @var self|null */
17
    protected static $instance = null;
18
    /** @var ADatabase[] */
19
    private $database = [];
20
21 52
    public static function getInstance(): self
22
    {
23 52
        if (empty(static::$instance)) {
24 1
            static::$instance = new self();
25
        }
26 52
        return static::$instance;
27
    }
28
29 1
    protected function __construct()
30
    {
31 1
    }
32
33
    /**
34
     * @codeCoverageIgnore why someone would run that?!
35
     */
36
    private function __clone()
37
    {
38
    }
39
40
    /**
41
     * @param Config $config
42
     * @throws MapperException
43
     * @return ADatabase
44
     */
45 52
    final public function getDatabase(Config $config): ADatabase
46
    {
47 52
        if (empty($this->database[$config->getDriver()])) {
48 4
            $this->database[$config->getDriver()] = $this->getFactory()->getDatabase($config);
49
        }
50 52
        return $this->database[$config->getDriver()];
51
    }
52
53 4
    protected function getFactory(): Factory
54
    {
55 4
        return Factory::getInstance();
56
    }
57
}
58