Completed
Pull Request — master (#26)
by Michal
02:19
created

NeonFileStorage   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 97.92%

Importance

Changes 0
Metric Value
wmc 18
eloc 45
dl 0
loc 81
ccs 47
cts 48
cp 0.9792
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A load() 0 11 4
A shiftArrayKey() 0 7 2
A save() 0 17 4
A addToTranslations() 0 13 4
A arrayToFlat() 0 10 3
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\Bridge\KdybyTranslation\Storage;
4
5
use Efabrica\TranslationsAutomatization\Storage\StorageInterface;
6
use Nette\Neon\Encoder;
7
use Nette\Neon\Neon;
8
9
class NeonFileStorage implements StorageInterface
10
{
11
    private $filePath;
12
13
    private $prefix;
14
15
    private $indent;
16
17 24
    public function __construct(string $filePath, string $prefix, string $indent = "\t")
18
    {
19 24
        $this->filePath = $filePath;
20 24
        $this->prefix = $prefix;
21 24
        $this->indent = $indent;
22 24
    }
23
24 24
    public function load(): array
25
    {
26 24
        if (!file_exists($this->filePath)) {
27 14
            return [];
28
        }
29 10
        $content = trim(file_get_contents($this->filePath) ?: '');
30 10
        if ($content === '') {
31 4
            return [];
32
        }
33 6
        $texts = Neon::decode($content);
34 6
        return $this->arrayToFlat($texts);
35
    }
36
37 20
    public function save(array $texts): bool
38
    {
39 20
        $texts = array_merge($this->load(), $texts);
40 20
        ksort($texts);
41
42 20
        $translations = [];
43 20
        foreach ($texts as $key => $value) {
44 18
            $key = (string) $key;
45 18
            $key = strpos($key, $this->prefix) === 0 ? substr($key, strlen($this->prefix)) : $key;
46 18
            $translationKeyParts = explode('.', $key);
47 18
            $translations = $this->addToTranslations($translations, $translationKeyParts, $value);
48
        }
49 20
        $dirname = pathinfo($this->filePath, PATHINFO_DIRNAME);
50 20
        if (!is_dir($dirname)) {
51
            mkdir($dirname, 0777, true);
52
        }
53 20
        return (bool) file_put_contents($this->filePath, str_replace("\t", $this->indent, Neon::encode($translations, Encoder::BLOCK)));
54
    }
55
56 6
    private function arrayToFlat(array $texts, array $translations = []): array
57
    {
58 6
        foreach ($texts as $key => $value) {
59 6
            if (!is_array($value)) {
60 6
                $translations[$this->prefix . $key] = $value;
61 6
                continue;
62
            }
63 2
            $translations = $this->arrayToFlat($this->shiftArrayKey($value, $key), $translations);
64
        }
65 6
        return $translations;
66
    }
67
68 2
    private function shiftArrayKey(array $texts, string $parentKey)
69
    {
70 2
        $newTexts = [];
71 2
        foreach ($texts as $key => $value) {
72 2
            $newTexts[$parentKey . '.' . $key] = $value;
73
        }
74 2
        return $newTexts;
75
    }
76
77 18
    private function addToTranslations(array $translations, array $translationKeyParts, string $text): array
78
    {
79 18
        $keyPart = array_shift($translationKeyParts);
80 18
        if (count($translationKeyParts) === 0) {
81 18
            $translations[$keyPart] = $text;
82 18
            return $translations;
83
        }
84 8
        if (isset($translations[$keyPart]) && is_string($translations[$keyPart])) {
85 2
            $translations[$keyPart . '.' . implode('.', $translationKeyParts)] = $text;
86 2
            return $translations;
87
        }
88 8
        $translations[$keyPart] = $this->addToTranslations($translations[$keyPart] ?? [], $translationKeyParts, $text);
89 8
        return $translations;
90
    }
91
}
92