Passed
Branch master (d300ec)
by Petr
08:24
created

StorageSingleton   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 48
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInstance() 0 6 2
A __clone() 0 2 1
A clearStorage() 0 3 1
A getStorage() 0 9 3
1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Storage;
4
5
6
use kalanis\kw_storage\Interfaces\IStorage;
7
use kalanis\kw_storage\Storage as Store;
8
use kalanis\kw_storage\StorageException;
9
10
11
/**
12
 * Class StorageSingleton
13
 * @package kalanis\kw_mapper\Storage\Storage
14
 * Singleton to access storage across the mappers
15
 */
16
class StorageSingleton
17
{
18
    /** @var self|null */
19
    protected static $instance = null;
20
    /** @var Store\Factory */
21
    private $factory = null;
22
    /** @var IStorage|null */
23
    private $storage = null;
24
25 15
    public static function getInstance(): self
26
    {
27 15
        if (empty(static::$instance)) {
28 1
            static::$instance = new self();
29
        }
30 15
        return static::$instance;
31
    }
32
33 1
    protected function __construct()
34
    {
35 1
        $this->factory = new Store\Factory(new Store\Key\Factory(), new Store\Target\Factory());
36 1
    }
37
38
    /**
39
     * @codeCoverageIgnore why someone would run that?!
40
     */
41
    private function __clone()
42
    {
43
    }
44
45
    /**
46
     * @param object|array<string, string|object>|string $storageParams
47
     * @throws StorageException
48
     * @return IStorage
49
     */
50 15
    public function getStorage($storageParams): IStorage
51
    {
52 15
        if (empty($this->storage)) {
53 4
            if (empty($storageParams)) {
54 1
                throw new StorageException('Storage cannot be empty!');
55
            }
56 3
            $this->storage = $this->factory->getStorage($storageParams);
57
        }
58 14
        return $this->storage;
59
    }
60
61 2
    public function clearStorage(): void
62
    {
63 2
        $this->storage = null;
64 2
    }
65
}
66