Storage   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 51
ccs 22
cts 22
cp 1
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setModuleLevel() 0 5 1
A getPath() 0 6 2
A load() 0 6 2
A save() 0 6 2
1
<?php
2
3
namespace kalanis\kw_modules\ModulesLists\File;
4
5
6
use kalanis\kw_modules\Interfaces\IMdTranslations;
7
use kalanis\kw_modules\Interfaces\Lists\IFile;
8
use kalanis\kw_modules\ModuleException;
9
use kalanis\kw_modules\Traits\TMdLang;
10
use kalanis\kw_paths\Interfaces\IPaths;
11
use kalanis\kw_paths\Stuff;
12
use kalanis\kw_storage\Interfaces\IStorage as IKwStorage;
13
use kalanis\kw_storage\StorageException;
14
15
16
/**
17
 * Class Storage
18
 * @package kalanis\kw_modules\ModulesLists\File
19
 */
20
class Storage implements IFile
21
{
22
    use TMdLang;
23
24
    protected IKwStorage $storage;
25
    protected string $moduleConfPath = '';
26
    protected string $path = '';
27
28 12
    public function __construct(IKwStorage $storage, string $moduleConfPath, ?IMdTranslations $lang = null)
29
    {
30 12
        $this->setMdLang($lang);
31 12
        $this->moduleConfPath = $moduleConfPath;
32 12
        $this->storage = $storage;
33 12
    }
34
35 7
    public function setModuleLevel(int $level): void
36
    {
37 7
        $this->path = Stuff::sanitize(implode(DIRECTORY_SEPARATOR, [
38 7
            $this->moduleConfPath,
39 7
            sprintf('%s.%d.%s', IPaths::DIR_MODULE, $level, IPaths::DIR_CONF )
40
        ]));
41 7
    }
42
43 7
    public function load(): string
44
    {
45
        try {
46 7
            return $this->storage->read($this->getPath());
47 2
        } catch (StorageException $ex) {
48 1
            throw new ModuleException($this->getMdLang()->mdStorageLoadProblem(), 0, $ex);
49
        }
50
    }
51
52 4
    public function save(string $records): bool
53
    {
54
        try {
55 4
            return $this->storage->write($this->getPath(), $records);
56 1
        } catch (StorageException $ex) {
57 1
            throw new ModuleException($this->getMdLang()->mdStorageSaveProblem(), 0, $ex);
58
        }
59
    }
60
61
    /**
62
     * @throws ModuleException
63
     * @return string
64
     */
65 8
    protected function getPath(): string
66
    {
67 8
        if (empty($this->path)) {
68 1
            throw new ModuleException($this->getMdLang()->mdStorageTargetNotSet());
69
        }
70 7
        return $this->path;
71
    }
72
}
73