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

StorageSingleton::__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 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
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