Files::load()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
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