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

Files   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 50
ccs 20
cts 20
cp 1
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 6 2
A __construct() 0 5 1
A setSource() 0 3 1
A exists() 0 6 2
A load() 0 6 2
1
<?php
2
3
namespace kalanis\kw_menu\MetaSource;
4
5
6
use kalanis\kw_files\Access\CompositeAdapter;
7
use kalanis\kw_files\FilesException;
8
use kalanis\kw_menu\Interfaces\IMetaFileParser;
9
use kalanis\kw_menu\Interfaces\IMetaSource;
10
use kalanis\kw_menu\Menu\Menu;
11
use kalanis\kw_menu\MenuException;
12
use kalanis\kw_paths\PathsException;
13
14
15
/**
16
 * Class Files
17
 * @package kalanis\kw_menu\MetaSource
18
 * Data source is in passed Files package
19
 */
20
class Files implements IMetaSource
21
{
22
    /** @var string[] */
23
    protected $key = [];
24
    /** @var CompositeAdapter */
25
    protected $files = null;
26
    /** @var IMetaFileParser */
27
    protected $parser = null;
28
29
    /**
30
     * @param CompositeAdapter $files
31
     * @param IMetaFileParser $parser
32
     * @param string[] $metaKey
33
     */
34 5
    public function __construct(CompositeAdapter $files, IMetaFileParser $parser, array $metaKey = [])
35
    {
36 5
        $this->files = $files;
37 5
        $this->parser = $parser;
38 5
        $this->key = $metaKey;
39 5
    }
40
41 1
    public function setSource(array $metaPath): void
42
    {
43 1
        $this->key = $metaPath;
44 1
    }
45
46 2
    public function exists(): bool
47
    {
48
        try {
49 2
            return $this->files->exists($this->key);
50 1
        } catch (FilesException | PathsException $ex) {
51 1
            throw new MenuException($ex->getMessage(), $ex->getCode(), $ex);
52
        }
53
    }
54
55 2
    public function load(): Menu
56
    {
57
        try {
58 2
            return $this->parser->unpack($this->files->readFile($this->key));
0 ignored issues
show
Bug introduced by
It seems like $this->files->readFile($this->key) can also be of type resource; however, parameter $content of kalanis\kw_menu\Interfac...etaFileParser::unpack() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
            return $this->parser->unpack(/** @scrutinizer ignore-type */ $this->files->readFile($this->key));
Loading history...
59 1
        } catch (FilesException | PathsException $ex) {
60 1
            throw new MenuException($ex->getMessage(), $ex->getCode(), $ex);
61
        }
62
    }
63
64 2
    public function save(Menu $content): bool
65
    {
66
        try {
67 2
            return $this->files->saveFile($this->key, $this->parser->pack($content));
68 1
        } catch (FilesException | PathsException $ex) {
69 1
            throw new MenuException($ex->getMessage(), $ex->getCode(), $ex);
70
        }
71
    }
72
}
73