Passed
Push — master ( c85748...2fd0ab )
by Petr
08:11
created

TStore::saveSource()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 12
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 4
rs 9.8666
1
<?php
2
3
namespace kalanis\kw_mapper\Mappers\Shared;
4
5
6
use kalanis\kw_mapper\MapperException;
7
use kalanis\kw_mapper\Records;
8
9
10
/**
11
 * Trait TStore
12
 * @package kalanis\kw_mapper\Mappers\Shared
13
 * Abstract for manipulation with file content as table
14
 */
15
trait TStore
16
{
17
    use TTranslate;
18
19
    /**
20
     * @param Records\ARecord $record
21
     * @throws MapperException
22
     * @return Records\ARecord[]
23
     */
24 5
    protected function loadSource(Records\ARecord $record): array
25
    {
26 5
        $lines = $this->loadFromStorage();
27 5
        $records = [];
28 5
        foreach ($lines as &$line) {
29
30 5
            $item = clone $record;
31
32 5
            foreach ($this->getRelations() as $objectKey => $recordKey) {
33 5
                $entry = $item->getEntry($objectKey);
34 5
                $entry->setData($this->translateTypeFrom($entry->getType(), $line[$recordKey]), true);
35
            }
36 5
            $records[] = $item;
37
        }
38 5
        return $records;
39
    }
40
41
    /**
42
     * @param Records\ARecord[] $records
43
     * @throws MapperException
44
     * @return bool
45
     */
46 5
    protected function saveSource(array $records): bool
47
    {
48 5
        $lines = [];
49 5
        foreach ($records as &$record) {
50 5
            $dataLine = [];
51
52 5
            foreach ($this->getRelations() as $objectKey => $recordKey) {
53 5
                $entry = $record->getEntry($objectKey);
54 5
                $dataLine[$recordKey] = $this->translateTypeTo($entry->getType(), $entry->getData());
55
            }
56
57 5
            $linePk = $this->generateKeyFromPks($record);
58 5
            if ($linePk) {
59 4
                $lines[$linePk] = $dataLine;
60
            } else {
61 1
                $lines[] = $dataLine;
62
            }
63
        }
64 5
        return $this->saveToStorage($lines);
65
    }
66
67
    /**
68
     * @param Records\ARecord $record
69
     * @throws MapperException
70
     * @return string|null
71
     */
72 5
    private function generateKeyFromPks(Records\ARecord $record): ?string
73
    {
74 5
        $toComplete = [];
75 5
        foreach ($this->getPrimaryKeys() as $key) {
76 4
            $toComplete[] = $record->offsetGet($key);
77
        }
78 5
        return (count(array_filter($toComplete))) ? implode('_', $toComplete) : null ;
79
    }
80
81
    /**
82
     * @throws MapperException
83
     * @return array<string|int, array<string|int, string|int|array<string|int, string|int>>>
84
     */
85
    abstract protected function loadFromStorage(): array;
86
87
    /**
88
     * @param array<string|int, array<string|int, string|int|array<string|int, string|int>>> $content
89
     * @throws MapperException
90
     * @return bool
91
     */
92
    abstract protected function saveToStorage(array $content): bool;
93
94
    /**
95
     * @return string[]
96
     */
97
    abstract public function getPrimaryKeys(): array;
98
99
    /**
100
     * @return array<string|int, string|int>
101
     */
102
    abstract public function getRelations(): array;
103
}
104