Passed
Push — master ( af3dcf...3709c1 )
by Petr
02:16
created

Volume::systemDelimiter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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