Passed
Push — master ( 6139d1...678a4c )
by Petr
13:22 queued 10:27
created

AFileSource::loadFromStorage()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

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