Completed
Push — master ( f9436b...7799f6 )
by Nassif
02:05
created

Writer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.73%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 103
ccs 43
cts 44
cp 0.9773
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setTranslations() 0 6 1
A write() 0 11 1
A writeFile() 0 12 2
A groupTranslationsByFile() 0 17 2
A buildTranslationsForFile() 0 27 5
1
<?php
2
3
namespace Nikaia\TranslationSheet\Translation;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Foundation\Application;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Collection;
9
use Nikaia\TranslationSheet\Commands\Output;
10
use Nikaia\TranslationSheet\Spreadsheet;
11
use Nikaia\TranslationSheet\Util;
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 2
    public function __construct(Spreadsheet $spreadsheet, Filesystem $files, Application $app)
30
    {
31 2
        $this->spreadsheet = $spreadsheet;
32 2
        $this->files = $files;
33 2
        $this->app = $app;
34
35 2
        $this->nullOutput();
36 2
    }
37
38 2
    public function setTranslations($translations)
39
    {
40 2
        $this->translations = $translations;
41
42 2
        return $this;
43
    }
44
45 2
    public function write()
46
    {
47
        $this
48 2
            ->groupTranslationsByFile()
49
            ->map(function ($items, $sourceFile) {
50 2
                $this->writeFile(
51 2
                    $this->app->make('path.lang').'/'.$sourceFile,
52 2
                    $items
53
                );
54 2
            });
55 2
    }
56
57 2
    protected function writeFile($file, $items)
58
    {
59 2
        $this->output->writeln('  '.$file);
60
61 2
        $content = "<?php\n\nreturn ".Util::varExport($items).";\n";
62
63 2
        if (! $this->files->isDirectory($dir = dirname($file))) {
64 1
            $this->files->makeDirectory($dir, 0755, true);
65
        }
66
67 2
        $this->files->put($file, $content);
68 2
    }
69
70 2
    protected function groupTranslationsByFile()
71
    {
72
        $items = $this
73 2
            ->translations
74 2
            ->groupBy('sourceFile')
75
            ->map(function ($fileTranslations) {
76 2
                return $this->buildTranslationsForFile($fileTranslations);
77 2
            });
78
79
        // flatten does not seem to work for every case. !!! refactor !!!
80 2
        $result = [];
81 2
        foreach ($items as $subitems) {
82 2
            $result = array_merge($result, $subitems);
83
        }
84
85 2
        return new Collection($result);
86
    }
87
88 2
    protected function buildTranslationsForFile($fileTranslations)
89
    {
90 2
        $files = [];
91 2
        $locales = $this->spreadsheet->getLocales();
92
93 2
        foreach ($locales as $locale) {
94 2
            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 2
                if (! isset($translation[$locale])) {
101
                    continue;
102
                }
103
104 2
                $localeFile = str_replace('{locale}/', $locale.'/', $translation['sourceFile']);
105 2
                if (empty($files[$localeFile])) {
106 2
                    $files[$localeFile] = [];
107
                }
108
109 2
                Arr::set($files[$localeFile], $translation['key'], $translation[$locale]);
110
            }
111
        }
112
113 2
        return $files;
114
    }
115
}
116