Passed
Push — master ( 8d284d...a04c24 )
by Petr
02:31
created

Volume::setModuleLevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
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
    /** @var string */
22
    protected $moduleConfPath = '';
23
    /** @var string */
24
    protected $path = '';
25
26 8
    public function __construct(string $moduleConfPath, ?IMdTranslations $lang = null)
27
    {
28 8
        $this->setMdLang($lang);
29 8
        $this->moduleConfPath = $moduleConfPath;
30 8
    }
31
32 1
    public function setModuleLevel(int $level): void
33
    {
34 1
        $this->path = implode(DIRECTORY_SEPARATOR, [
35 1
            realpath($this->moduleConfPath),
36 1
            sprintf('%s.%d.%s', IPaths::DIR_MODULE, $level, IPaths::DIR_CONF )
37
        ]);
38 1
    }
39
40 2
    public function load(): string
41
    {
42 2
        $data = @ file_get_contents($this->getPath());
43 1
        if (false === $data) {
44
            // @codeCoverageIgnoreStart
45
            throw new ModuleException($this->getMdLang()->mdStorageLoadProblem());
46
        }
47
        // @codeCoverageIgnoreEnd
48 1
        return strval($data);
49
    }
50
51 1
    public function save(string $records): bool
52
    {
53 1
        return boolval(intval(@ file_put_contents($this->getPath(), $records)));
54
    }
55
56
    /**
57
     * @throws ModuleException
58
     * @return string
59
     */
60 2
    protected function getPath(): string
61
    {
62 2
        if (empty($this->path)) {
63 1
            throw new ModuleException($this->getMdLang()->mdStorageTargetNotSet());
64
        }
65 1
        return $this->path;
66
    }
67
}
68