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

AStorage::getWriteSource()   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 1
Bugs 0 Features 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 1
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_mapper\Mappers\Storage;
4
5
6
use kalanis\kw_mapper\Interfaces\IFileFormat;
7
use kalanis\kw_mapper\MapperException;
8
use kalanis\kw_mapper\Mappers\AMapper;
9
use kalanis\kw_mapper\Storage\Shared;
10
use kalanis\kw_mapper\Storage\Storage;
11
use kalanis\kw_storage\StorageException;
12
13
14
/**
15
 * Class AStorage
16
 * @package kalanis\kw_mapper\Mappers\Storage
17
 * The path is separated
18
 * - storage has first half which is usually static
19
 * - content has second half which can be changed by circumstances
20
 */
21
abstract class AStorage extends AMapper
22
{
23
    use Storage\TStorage;
24
    use Shared\TFormat;
25
26 24
    public function getAlias(): string
27
    {
28 24
        return $this->getSource();
29
    }
30
31 13
    protected function getReadSource(): string
32
    {
33 13
        return $this->getSource();
34
    }
35
36 8
    protected function getWriteSource(): string
37
    {
38 8
        return $this->getSource();
39
    }
40
41
    /**
42
     * @param IFileFormat|null $format
43
     * @throws MapperException
44
     * @return array<string|int, array<string|int, string|int|float|bool|array<string|int, string|int|float|bool>>>
45
     */
46 13
    protected function loadFromStorage(?IFileFormat $format = null): array
47
    {
48
        try {
49 13
            $format = $format ?: Shared\FormatFiles\Factory::getInstance()->getFormatClass($this->getFormat());
50 13
            return $format->unpack($this->getStorage()->read($this->getReadSource()));
51 1
        } catch (StorageException $ex) {
52 1
            throw new MapperException('Unable to read from source', 0, $ex);
53
        }
54
    }
55
56
    /**
57
     * @param array<string|int, array<string|int, string|int|float|bool|array<string|int, string|int|float|bool>>> $content
58
     * @param IFileFormat|null $format
59
     * @throws MapperException
60
     * @return bool
61
     */
62 8
    protected function saveToStorage(array $content, ?IFileFormat $format = null): bool
63
    {
64
        try {
65 8
            $format = $format ?: Shared\FormatFiles\Factory::getInstance()->getFormatClass($this->getFormat());
66 8
            return $this->getStorage()->write($this->getWriteSource(), $format->pack($content));
67 1
        } catch (StorageException $ex) {
68 1
            throw new MapperException('Unable to write into source', 0, $ex);
69
        }
70
    }
71
}
72