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

ConfigStorage::addConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Database;
4
5
6
use kalanis\kw_mapper\MapperException;
7
8
9
/**
10
 * Class ConfigStorage
11
 * @package kalanis\kw_mapper\Storage\Database
12
 * Singleton to access configs across the mapper system
13
 */
14
class ConfigStorage
15
{
16
    protected static ?ConfigStorage $instance = null;
17
    /** @var Config[] */
18
    private array $configs = [];
19
20 67
    public static function getInstance(): self
21
    {
22 67
        if (empty(static::$instance)) {
23 1
            static::$instance = new self();
24
        }
25 67
        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\Storage\Database\ConfigStorage. Consider adding an additional type-check to rule them out.
Loading history...
26
    }
27
28 1
    protected function __construct()
29
    {
30 1
    }
31
32
    /**
33
     * @codeCoverageIgnore why someone would run that?!
34
     */
35
    private function __clone()
36
    {
37
    }
38
39 64
    final public function addConfig(Config $config): void
40
    {
41 64
        $this->configs[$config->getSourceName()] = $config;
42
    }
43
44
    /**
45
     * @param string $sourceName
46
     * @throws MapperException
47
     * @return Config
48
     */
49 27
    final public function getConfig(string $sourceName): Config
50
    {
51 27
        if (empty($this->configs[$sourceName])) {
52 1
            throw new MapperException(sprintf('Unknown source *%s*', $sourceName));
53
        }
54 26
        return $this->configs[$sourceName];
55
    }
56
}
57