Completed
Push — master ( bcac64...9b6ee4 )
by Nassif
11:39
created

Writer::groupTranslationsByFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
ccs 0
cts 10
cp 0
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 10
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Nikaia\TranslationSheet\Translation;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Collection;
8
use Nikaia\TranslationSheet\Commands\Output;
9
use Nikaia\TranslationSheet\Spreadsheet;
10
use Nikaia\TranslationSheet\Util;
11
use Illuminate\Foundation\Application;
12
13
class Writer
14
{
15
    use Output;
16
17
    /** @var Collection */
18
    protected $translations;
19
20
    /** @var Spreadsheet */
21
    protected $spreadsheet;
22
23
    /** @var Filesystem */
24
    protected $files;
25
26
    /** @var Application */
27
    protected $app;
28
29
    public function __construct(Spreadsheet $spreadsheet, Filesystem $files, Application $app)
30
    {
31
        $this->spreadsheet = $spreadsheet;
32
        $this->files = $files;
33
        $this->app = $app;
34
35
        $this->nullOutput();
36
    }
37
38
    public function setTranslations($translations)
39
    {
40
        $this->translations = $translations;
41
42
        return $this;
43
    }
44
45
    public function write()
46
    {
47
        $this
48
            ->groupTranslationsByFile()
49
            ->map(function ($items, $sourceFile) {
50
                $this->writeFile(
51
                    $this->app->make('path.lang').'/'.$sourceFile,
52
                    $items
53
                );
54
            });
55
    }
56
57
    protected function writeFile($file, $items)
58
    {
59
        $this->output->writeln('  '.$file);
60
61
        $content = "<?php\n\nreturn ".Util::varExport($items).";\n";
62
63
        if (! $this->files->isDirectory($dir = dirname($file))) {
64
            $this->files->makeDirectory($dir, 0755, true);
65
        }
66
67
        $this->files->put($file, $content);
68
    }
69
70
    protected function groupTranslationsByFile()
71
    {
72
        $items = $this
73
            ->translations
74
            ->groupBy('sourceFile')
75
            ->map(function ($fileTranslations) {
76
                return $this->buildTranslationsForFile($fileTranslations);
77
            });
78
79
        // flatten does not seem to work for every case. !!! refactor !!!
80
        $result = [];
81
        foreach ($items as $subitems) {
82
            $result = array_merge($result, $subitems);
83
        }
84
85
        return new Collection($result);
86
    }
87
88
    protected function buildTranslationsForFile($fileTranslations)
89
    {
90
        $files = [];
91
        $locales = $this->spreadsheet->getLocales();
92
93
        foreach ($locales as $locale) {
94
            foreach ($fileTranslations as $translation) {
95
96
                // We will only write non empty translations
97
                // For instance, we have `app.title` that is the same for each locale,
98
                // We dont want to translate it to every locale, and prefer letting
99
                // Laravel default back to the default locale.
100
                if (empty($translation[$locale])) {
101
                    continue;
102
                }
103
104
                $localeFile = str_replace('{locale}/', $locale.'/', $translation['sourceFile']);
105
                if (empty($files[$localeFile])) {
106
                    $files[$localeFile] = [];
107
                }
108
109
                Arr::set($files[$localeFile], $translation['key'], $translation[$locale]);
110
            }
111
        }
112
113
        return $files;
114
    }
115
}
116