Volume::__construct()   A
last analyzed

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 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace kalanis\kw_menu\MetaSource;
4
5
6
use kalanis\kw_menu\Interfaces;
7
use kalanis\kw_menu\Menu\Menu;
8
use kalanis\kw_menu\MenuException;
9
use kalanis\kw_menu\Traits\TLang;
10
use kalanis\kw_paths\PathsException;
11
use kalanis\kw_paths\Stuff;
12
13
14
/**
15
 * Class Volume
16
 * @package kalanis\kw_menu\MetaSource
17
 * Data source is processed directly over volume
18
 */
19
class Volume implements Interfaces\IMetaSource
20
{
21
    use TLang;
22
23
    /** @var string path to menu dir */
24
    protected string $metaDir = '';
25
    /** @var string[] name of the file */
26
    protected array $metaFile = [];
27
    protected Interfaces\IMetaFileParser $parser;
28
29 16
    public function __construct(string $metaPath, Interfaces\IMetaFileParser $parser, ?Interfaces\IMNTranslations $lang = null)
30
    {
31 16
        $this->metaDir = $metaPath;
32 16
        $this->parser = $parser;
33 16
        $this->setMnLang($lang);
34 16
    }
35
36 14
    public function setSource(array $metaSource): void
37
    {
38 14
        $this->metaFile = $metaSource;
39 14
    }
40
41 10
    public function exists(): bool
42
    {
43
        try {
44 10
            $path = $this->metaDir . Stuff::arrayToPath($this->metaFile, $this->systemDelimiter());
45 9
            return is_file($path);
46 1
        } catch (PathsException $ex) {
47 1
            throw new MenuException($ex->getMessage(), $ex->getCode(), $ex);
48
        }
49
    }
50
51 9
    public function load(): Menu
52
    {
53
        try {
54 9
            $path = $this->metaDir . Stuff::arrayToPath($this->metaFile, $this->systemDelimiter());
55 8
            $content = @file_get_contents($path);
56 8
            if (false === $content) {
57 1
                throw new MenuException($this->getMnLang()->mnCannotOpen());
58
            }
59 7
            return $this->parser->unpack($content);
60 2
        } catch (PathsException $ex) {
61 1
            throw new MenuException($ex->getMessage(), $ex->getCode(), $ex);
62
        }
63
    }
64
65 5
    public function save(Menu $content): bool
66
    {
67
        try {
68 5
            $path = $this->metaDir . Stuff::arrayToPath($this->metaFile, $this->systemDelimiter());
69 4
            if (false === @file_put_contents($path, $this->parser->pack($content))) {
70 1
                throw new MenuException($this->getMnLang()->mnCannotSave());
71
            }
72 3
            return true;
73 2
        } catch (PathsException $ex) {
74 1
            throw new MenuException($ex->getMessage(), $ex->getCode(), $ex);
75
        }
76
    }
77
78
    /**
79
     * Because tests - fail Stuff class
80
     * @return string
81
     */
82 11
    protected function systemDelimiter(): string
83
    {
84 11
        return DIRECTORY_SEPARATOR;
85
    }
86
}
87