NeonFileStorage   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 95.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 44
c 1
b 0
f 0
dl 0
loc 80
ccs 45
cts 47
cp 0.9574
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 11 4
A __construct() 0 5 1
A shiftArrayKey() 0 7 2
A save() 0 16 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 14
    public function __construct(string $filePath, string $prefix, string $indent = '    ')
18
    {
19 14
        $this->filePath = $filePath;
20 14
        $this->prefix = $prefix;
21 14
        $this->indent = $indent;
22 14
    }
23
24 14
    public function load(): array
25
    {
26 14
        if (!file_exists($this->filePath)) {
27 8
            return [];
28
        }
29 6
        $content = trim(file_get_contents($this->filePath) ?: '');
30 6
        if ($content === '') {
31
            return [];
32
        }
33 6
        $texts = Neon::decode($content);
34 6
        return $this->arrayToFlat($texts);
35
    }
36
37 14
    public function save(array $texts): bool
38
    {
39 14
        $texts = array_merge($this->load(), $texts);
40 14
        ksort($texts);
41
42 14
        $translations = [];
43 14
        foreach ($texts as $key => $value) {
44 12
            $key = strpos($key, $this->prefix) === 0 ? substr($key, strlen($this->prefix)) : $key;
45 12
            $translationKeyParts = explode('.', (string) $key);
46 12
            $translations = $this->addToTranslations($translations, $translationKeyParts, $value);
47
        }
48 14
        $dirname = pathinfo($this->filePath, PATHINFO_DIRNAME);
49 14
        if (!is_dir($dirname)) {
0 ignored issues
show
Bug introduced by
It seems like $dirname can also be of type array; however, parameter $filename of is_dir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        if (!is_dir(/** @scrutinizer ignore-type */ $dirname)) {
Loading history...
50
            mkdir($dirname, 0777, true);
0 ignored issues
show
Bug introduced by
It seems like $dirname can also be of type array; however, parameter $directory of mkdir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
            mkdir(/** @scrutinizer ignore-type */ $dirname, 0777, true);
Loading history...
51
        }
52 14
        return (bool) file_put_contents($this->filePath, str_replace("\t", $this->indent, Neon::encode($translations, Encoder::BLOCK)));
0 ignored issues
show
introduced by
The constant Nette\Neon\Encoder::BLOCK has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

52
        return (bool) file_put_contents($this->filePath, str_replace("\t", $this->indent, Neon::encode($translations, /** @scrutinizer ignore-deprecated */ Encoder::BLOCK)));
Loading history...
53
    }
54
55 6
    private function arrayToFlat(array $texts, array $translations = []): array
56
    {
57 6
        foreach ($texts as $key => $value) {
58 6
            if (!is_array($value)) {
59 6
                $translations[$this->prefix . $key] = $value;
60 6
                continue;
61
            }
62 2
            $translations = $this->arrayToFlat($this->shiftArrayKey($value, $key), $translations);
63
        }
64 6
        return $translations;
65
    }
66
67 2
    private function shiftArrayKey(array $texts, string $parentKey)
68
    {
69 2
        $newTexts = [];
70 2
        foreach ($texts as $key => $value) {
71 2
            $newTexts[$parentKey . '.' . $key] = $value;
72
        }
73 2
        return $newTexts;
74
    }
75
76 12
    private function addToTranslations(array $translations, array $translationKeyParts, string $text): array
77
    {
78 12
        $keyPart = array_shift($translationKeyParts);
79 12
        if (count($translationKeyParts) === 0) {
80 12
            $translations[$keyPart] = $text;
81 12
            return $translations;
82
        }
83 6
        if (isset($translations[$keyPart]) && is_string($translations[$keyPart])) {
84 2
            $translations[$keyPart . '.' . implode('.', $translationKeyParts)] = $text;
85 2
            return $translations;
86
        }
87 6
        $translations[$keyPart] = $this->addToTranslations($translations[$keyPart] ?? [], $translationKeyParts, $text);
88 6
        return $translations;
89
    }
90
}
91