Multiton   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 76
ccs 22
cts 22
cp 1
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getContent() 0 4 1
A checkContent() 0 4 2
A getFormatClass() 0 4 1
A known() 0 3 1
A getInstance() 0 6 2
A __construct() 0 2 1
A init() 0 3 1
A setContent() 0 4 1
A __clone() 0 2 1
1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Storage\MultiContent;
4
5
6
use kalanis\kw_mapper\Interfaces\IFileFormat;
7
use kalanis\kw_mapper\MapperException;
8
9
10
/**
11
 * Class Multiton
12
 * @package kalanis\kw_mapper\Storage\Storage\MultiContent
13
 * Content is stored as array of ContentEntities where first key is usually file name
14
 * You also need to specify the format in which is it stored
15
 */
16
class Multiton
17
{
18
    protected static ?Multiton $instance = null;
19
    /** @var Entity[] */
20
    private array $storage = [];
21
22 2
    public static function getInstance(): self
23
    {
24 2
        if (empty(static::$instance)) {
25 1
            static::$instance = new self();
26
        }
27 2
        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...e\MultiContent\Multiton. Consider adding an additional type-check to rule them out.
Loading history...
28
    }
29
30 1
    protected function __construct()
31
    {
32 1
    }
33
34
    /**
35
     * @codeCoverageIgnore why someone would run that?!
36
     */
37
    private function __clone()
38
    {
39
    }
40
41 1
    public function init(string $key, IFileFormat $formatClass): void
42
    {
43 1
        $this->storage[$key] = new Entity($formatClass);
44
    }
45
46 2
    public function known(string $key): bool
47
    {
48 2
        return isset($this->storage[$key]);
49
    }
50
51
    /**
52
     * @param string $key
53
     * @throws MapperException
54
     * @return string[]
55
     */
56 2
    public function getContent(string $key): array
57
    {
58 2
        $this->checkContent($key);
59 1
        return $this->storage[$key]->get();
60
    }
61
62
    /**
63
     * @param string $key
64
     * @throws MapperException
65
     * @return IFileFormat|null
66
     */
67 1
    public function getFormatClass(string $key): ?IFileFormat
68
    {
69 1
        $this->checkContent($key);
70 1
        return $this->storage[$key]->getFormat();
71
    }
72
73
    /**
74
     * @param string $key
75
     * @param string[] $content
76
     * @throws MapperException
77
     */
78 1
    public function setContent(string $key, array $content): void
79
    {
80 1
        $this->checkContent($key);
81 1
        $this->storage[$key]->set($content);
82
    }
83
84
    /**
85
     * @param string $key
86
     * @throws MapperException
87
     */
88 2
    protected function checkContent(string $key): void
89
    {
90 2
        if (!$this->known($key)) {
91 1
            throw new MapperException('Uninitialized content');
92
        }
93
    }
94
}
95