Completed
Push — master ( 7f7bb8...b9ae22 )
by Nassif
10:19 queued 11s
created

Writer::fileIsJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
            ->each(function ($items, $sourceFile) {
50 2
                if ($this->files->extension($sourceFile) == 'json') {
51 2
                    return $this->writeJsonFile($this->app->make('path.lang').'/'.$sourceFile, $items);
52
                }
53
54 2
                $this->writeFile(
55 2
                    $this->app->make('path.lang').'/'.$sourceFile,
56
                    $items
57 2
                );
58
            });
59 2
    }
60
61 2
    protected function writeFile($file, $items)
62
    {
63 2
        $this->output->writeln('  '.$file);
64 1
65
        $content = sprintf('<?php%s%sreturn %s;%s', PHP_EOL, PHP_EOL, Util::varExport($items), PHP_EOL);
66
67 2 View Code Duplication
        if (! $this->files->isDirectory($dir = dirname($file))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68 2
            $this->files->makeDirectory($dir, 0755, true);
69
        }
70 2
71
        $this->files->put($file, $content);
72
    }
73 2
74 2
    protected function writeJsonFile($file, $items)
75
    {
76 2
        $this->output->writeln('  JSON: '.$file);
77 2
78 View Code Duplication
        if (! $this->files->isDirectory($dir = dirname($file))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
            $this->files->makeDirectory($dir, 0755, true);
80 2
        }
81 2
82 2
        $this->files->put($file, json_encode($items, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES).PHP_EOL);
83
    }
84
85 2
    protected function groupTranslationsByFile()
86
    {
87
        $items = $this
88 2
            ->translations
89
            ->groupBy('sourceFile')
90 2
            ->map(function ($fileTranslations) {
91 2
                return $this->buildTranslationsForFile($fileTranslations);
92
            });
93 2
94 2
        // flatten does not seem to work for every case. !!! refactor !!!
95
        $result = [];
96
        foreach ($items as $subitems) {
97
            $result = array_merge($result, $subitems);
98
        }
99
100 2
        return new Collection($result);
101
    }
102
103
    protected function buildTranslationsForFile($fileTranslations)
104 2
    {
105 2
        $files = [];
106 2
        $locales = $this->spreadsheet->getLocales();
107
108
        foreach ($locales as $locale) {
109 2
            foreach ($fileTranslations as $translation) {
110
111
                // We will only write non empty translations
112
                // For instance, we have `app.title` that is the same for each locale,
113 2
                // We dont want to translate it to every locale, and prefer letting
114
                // Laravel default back to the default locale.
115
                if ($this->skipToDefault($translation, $locale)) {
116
                    continue;
117
                }
118
119
                $localeFile = $this->fileIsJson($translation['sourceFile']) ?
120
                    $locale . '.json' :
121
                    str_replace('{locale}/', $locale . '/', $translation['sourceFile']);
122
123
                if (empty($files[$localeFile])) {
124
                    $files[$localeFile] = [];
125
                }
126
127
                if ($this->fileIsJson($translation['sourceFile'])) {
128
                    $files[$localeFile][$translation['key']] = $translation[$locale];
129
                } else {
130
                    Arr::set($files[$localeFile], $translation['key'], $translation[$locale]);
131
                }
132
            }
133
        }
134
135
        return $files;
136
    }
137
138
    protected function skipToDefault($translation, $locale)
139
    {
140
        if (! isset($translation[$locale])) {
141
            return true;
142
        }
143
144
        return empty($translation[$locale]) && $this->fileIsJson($translation['sourceFile']);
145
    }
146
147
    protected function fileIsJson($sourceFile)
148
    {
149
        return $sourceFile === '{locale}.json';
150
    }
151
}
152