Passed
Push — master ( c85748...2fd0ab )
by Petr
08:11
created

Multiton   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 2 1
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
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
    /** @var self|null */
19
    protected static $instance = null;
20
    /** @var Entity[] */
21
    private $storage = [];
22
23 2
    public static function getInstance(): self
24
    {
25 2
        if (empty(static::$instance)) {
26 1
            static::$instance = new self();
27
        }
28 2
        return static::$instance;
29
    }
30
31 1
    protected function __construct()
32
    {
33 1
    }
34
35
    /**
36
     * @codeCoverageIgnore why someone would run that?!
37
     */
38
    private function __clone()
39
    {
40
    }
41
42 1
    public function init(string $key, IFileFormat $formatClass): void
43
    {
44 1
        $this->storage[$key] = new Entity($formatClass);
45
    }
46
47 2
    public function known(string $key): bool
48
    {
49 2
        return isset($this->storage[$key]);
50
    }
51
52
    /**
53
     * @param string $key
54
     * @throws MapperException
55
     * @return string[]
56
     */
57 2
    public function getContent(string $key): array
58
    {
59 2
        $this->checkContent($key);
60 1
        return $this->storage[$key]->get();
61
    }
62
63
    /**
64
     * @param string $key
65
     * @throws MapperException
66
     * @return IFileFormat|null
67
     */
68 1
    public function getFormatClass(string $key): ?IFileFormat
69
    {
70 1
        $this->checkContent($key);
71 1
        return $this->storage[$key]->getFormat();
72
    }
73
74
    /**
75
     * @param string $key
76
     * @param string[] $content
77
     * @throws MapperException
78
     */
79 1
    public function setContent(string $key, array $content): void
80
    {
81 1
        $this->checkContent($key);
82 1
        $this->storage[$key]->set($content);
83
    }
84
85
    /**
86
     * @param string $key
87
     * @throws MapperException
88
     */
89 2
    protected function checkContent(string $key): void
90
    {
91 2
        if (!$this->known($key)) {
92 1
            throw new MapperException('Uninitialized content');
93
        }
94
    }
95
}
96