Volume::getPath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
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_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
12
13
/**
14
 * Class Volume
15
 * @package kalanis\kw_modules\ModulesLists\File
16
 */
17
class Volume implements IFile
18
{
19
    use TMdLang;
20
21
    protected string $moduleConfPath = '';
22
    protected string $path = '';
23
24 8
    public function __construct(string $moduleConfPath, ?IMdTranslations $lang = null)
25
    {
26 8
        $this->setMdLang($lang);
27 8
        $this->moduleConfPath = $moduleConfPath;
28 8
    }
29
30 1
    public function setModuleLevel(int $level): void
31
    {
32 1
        $this->path = implode(DIRECTORY_SEPARATOR, [
33 1
            realpath($this->moduleConfPath),
34 1
            sprintf('%s.%d.%s', IPaths::DIR_MODULE, $level, IPaths::DIR_CONF )
35
        ]);
36 1
    }
37
38 2
    public function load(): string
39
    {
40 2
        $data = @ file_get_contents($this->getPath());
41 1
        if (false === $data) {
42
            // @codeCoverageIgnoreStart
43
            throw new ModuleException($this->getMdLang()->mdStorageLoadProblem());
44
        }
45
        // @codeCoverageIgnoreEnd
46 1
        return strval($data);
47
    }
48
49 1
    public function save(string $records): bool
50
    {
51 1
        return boolval(intval(@ file_put_contents($this->getPath(), $records)));
52
    }
53
54
    /**
55
     * @throws ModuleException
56
     * @return string
57
     */
58 2
    protected function getPath(): string
59
    {
60 2
        if (empty($this->path)) {
61 1
            throw new ModuleException($this->getMdLang()->mdStorageTargetNotSet());
62
        }
63 1
        return $this->path;
64
    }
65
}
66