Transformer   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 97
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setLocales() 0 6 1
A setTranslationsSheet() 0 6 1
B transform() 0 42 6
A ensureWeHaveAllLocales() 0 10 3
A formatSourceFile() 0 8 3
1
<?php
2
3
namespace Nikaia\TranslationSheet\Translation;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Nikaia\TranslationSheet\Sheet\TranslationsSheet;
8
9
class Transformer
10
{
11 3
    /** @var array */
12
    protected $locales = [];
13 3
14
    /** @var TranslationsSheet */
15 3
    protected $translationSheet;
16
17
    /**
18 3
     * Set transformers locales.
19
     *
20
     * @param $locales
21 3
     * @return $this
22 3
     */
23
    public function setLocales($locales)
24 3
    {
25
        $this->locales = $locales;
26 3
27 3
        return $this;
28
    }
29
30 3
    /**
31
     * Set the translation sheets that we are working on.
32 3
     *
33 3
     * @param TranslationsSheet $translationSheet
34
     * @return Transformer
35 3
     */
36
    public function setTranslationsSheet(TranslationsSheet $translationSheet)
37 3
    {
38 3
        $this->translationSheet = $translationSheet;
39 3
40 3
        return $this;
41 3
    }
42
43
    public function transform(Collection $translations)
44 3
    {
45
        return $translations
46 3
            ->sortBy('locale')
47 3
            ->groupBy('full_key')
48 3
            ->map(function (Collection $translation) {
49 3
                $firstLocale = $translation->first();
50 3
51
                $row = [];
52
                $row['full_key'] = $firstLocale->full_key;
53 3
54 3
                $translation = $translation
55
                    ->groupBy('locale')
56 3
                    ->map(function (Collection $item) {
57 3
                        return $item->first();
58 3
                    });
59
60
                $translation = $this->ensureWeHaveAllLocales($translation);
61 3
62
                $localesValues = [];
63 3
                foreach ($this->locales as $locale) {
64 3
                    $item = $translation->get($locale);
65 1
                    $value = !is_null($item) && isset($item->value) ? $item->value : '';
66
                    $localesValues [$locale] = $value;
67
                }
68
69 3
                $row = array_merge($row, $localesValues);
70
71
                $row = array_merge($row, [
72
                    'namespace' => !is_null($firstLocale->namespace) ? $firstLocale->namespace : '',
73
                    'group' => !is_null($firstLocale->group) ? $firstLocale->group : '',
74
                    'key' => $firstLocale->key,
75
                    'source_file' => $this->formatSourceFile($firstLocale),
76
                ]);
77
78
                return array_values($row);
79
            })
80
            ->sortBy(function ($product, $key) {
81
                return $key;
82
            })
83
            ->values();
84
    }
85
86
    private function ensureWeHaveAllLocales(Collection $translation)
87
    {
88
        foreach ($this->locales as $locale) {
89
            if (!$translation->get($locale)) {
90
                $translation->put($locale, new Item);
91
            }
92
        }
93
94
        return $translation;
95
    }
96
97
    private function formatSourceFile(Item $translationItem)
98
    {
99
        if ($this->translationSheet->isPrimarySheet() && Str::endsWith($translationItem->source_file, ['.json'])) {
100
            return '{locale}.json';
101
        }
102
103
        return str_replace($translationItem->locale . '/', '{locale}/', $translationItem->source_file);
104
    }
105
}
106