Passed
Push — master ( a477cf...533b2b )
by Petr
08:26
created

AFileSource::getReadPath()   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
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_mapper\Mappers\File;
4
5
6
use kalanis\kw_files\FilesException;
7
use kalanis\kw_files\Traits\TToString;
8
use kalanis\kw_mapper\Interfaces\IFileFormat;
9
use kalanis\kw_mapper\MapperException;
10
use kalanis\kw_mapper\Mappers\AMapper;
11
use kalanis\kw_mapper\Storage\File;
12
use kalanis\kw_mapper\Storage\Shared;
13
use kalanis\kw_paths\PathsException;
14
15
16
/**
17
 * Class AFileSource
18
 * @package kalanis\kw_mapper\Mappers\File
19
 * The path is separated
20
 * - files (sometimes even with the storage underneath) has first half which is usually static
21
 * - content has second half which can be changed by circumstances
22
 */
23
abstract class AFileSource extends AMapper
24
{
25
    use File\TFile;
26
    use Shared\TFormat;
27
    use TToString;
28
29
    /**
30
     * @param string[] $path
31
     */
32 1
    public function setCombinedPath(array $path): void
33
    {
34 1
        $this->setPath($path);
35 1
        $this->setSource(implode($this->getCombinedSourceSeparator(), $path));
36 1
    }
37
38 1
    protected function getCombinedSourceSeparator(): string
39
    {
40 1
        return "//\e\e//";
41
    }
42
43 2
    public function getAlias(): string
44
    {
45 2
        return $this->getSource();
46
    }
47
48
    /**
49
     * @return string[]
50
     */
51 2
    protected function getReadPath(): array
52
    {
53 2
        return $this->getPath();
54
    }
55
56
    /**
57
     * @return string[]
58
     */
59 2
    protected function getWritePath(): array
60
    {
61 2
        return $this->getPath();
62
    }
63
64
    /**
65
     * @param IFileFormat|null $format
66
     * @throws MapperException
67
     * @return array<string|int, array<string|int, string|int|float|bool|array<string|int, string|int|float|bool>>>
68
     */
69 2
    protected function loadFromStorage(?IFileFormat $format = null): array
70
    {
71
        try {
72 2
            $format = $format ?: Shared\FormatFiles\Factory::getInstance()->getFormatClass($this->getFormat());
73 2
            return $format->unpack($this->toString(implode('/', $this->getReadPath()), $this->getFileAccessor()->readFile($this->getReadPath())));
74 1
        } catch (FilesException | PathsException $ex) {
75 1
            throw new MapperException('Unable to read from source', 0, $ex);
76
        }
77
    }
78
79
    /**
80
     * @param array<string|int, array<string|int, string|int|float|bool|array<string|int, string|int|float|bool>>> $content
81
     * @param IFileFormat|null $format
82
     * @throws MapperException
83
     * @return bool
84
     */
85 2
    protected function saveToStorage(array $content, ?IFileFormat $format = null): bool
86
    {
87
        try {
88 2
            $format = $format ?: Shared\FormatFiles\Factory::getInstance()->getFormatClass($this->getFormat());
89 2
            return $this->getFileAccessor()->saveFile($this->getWritePath(), $format->pack($content));
90 1
        } catch (FilesException | PathsException $ex) {
91 1
            throw new MapperException('Unable to write into source', 0, $ex);
92
        }
93
    }
94
}
95