Passed
Branch master (d300ec)
by Petr
08:24
created

AFileSource::saveToStorage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
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
     * @param IFileFormat|null $format
50
     * @throws MapperException
51
     * @return array<string|int, array<string|int, string|int|float|bool|array<string|int, string|int|float|bool>>>
52
     */
53 2
    protected function loadFromStorage(?IFileFormat $format = null): array
54
    {
55
        try {
56 2
            $format = $format ?: Shared\FormatFiles\Factory::getInstance()->getFormatClass($this->getFormat());
57 2
            return $format->unpack($this->toString(implode('/', $this->getPath()), $this->getFileAccessor()->readFile($this->getPath())));
58 1
        } catch (FilesException | PathsException $ex) {
59 1
            throw new MapperException('Unable to read from source', 0, $ex);
60
        }
61
    }
62
63
    /**
64
     * @param array<string|int, array<string|int, string|int|float|bool|array<string|int, string|int|float|bool>>> $content
65
     * @param IFileFormat|null $format
66
     * @throws MapperException
67
     * @return bool
68
     */
69 2
    protected function saveToStorage(array $content, ?IFileFormat $format = null): bool
70
    {
71
        try {
72 2
            $format = $format ?: Shared\FormatFiles\Factory::getInstance()->getFormatClass($this->getFormat());
73 2
            return $this->getFileAccessor()->saveFile($this->getPath(), $format->pack($content));
74 1
        } catch (FilesException | PathsException $ex) {
75 1
            throw new MapperException('Unable to write into source', 0, $ex);
76
        }
77
    }
78
}
79