Files   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

5 Methods

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