LangResourcesRepository   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 72
c 1
b 0
f 0
dl 0
loc 183
rs 10
wmc 26

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hasLangExist() 0 11 3
B getMissingForLang() 0 29 7
A processFileLang() 0 13 5
A addLanguageFolder() 0 23 3
A mergeLangs() 0 29 6
A __construct() 0 7 2
1
<?php
2
/**
3
 * Manipular os Arquivos de Trandução
4
 */
5
6
namespace Translation\Repositories;
7
8
use Illuminate\Database\Query\JoinClause;
9
use Illuminate\Foundation\Application;
0 ignored issues
show
Bug introduced by
The type Illuminate\Foundation\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Illuminate\Support\NamespacedItemResolver;
11
use Translation\Models\Translation as Translation;
12
use Illuminate\Support\Facades\Schema;
13
use Illuminate\Filesystem\Filesystem;
14
use File;
15
use Illuminate\Support\Str;
16
use Translation\Traits\ManipuleFileLangTrait;
17
18
class LangResourcesRepository extends Repository
19
{
20
    use ManipuleFileLangTrait;
21
    // /**
22
    //  * @var \Illuminate\Database\Connection
23
    //  */
24
    // protected $database;
25
26
    // /**
27
    //  * The model being queried.
28
    //  *
29
    //  * @var \Translation\Models\Translation
30
    //  */
31
    // protected $model;
32
33
    // /**
34
    //  *  Validator
35
    //  *
36
    //  * @var \Illuminate\Validation\Validator
37
    //  */
38
    // protected $validator;
39
40
    // /**
41
    //  *  Validation errors.
42
    //  *
43
    //  * @var \Illuminate\Support\MessageBag
44
    //  */
45
    // protected $errors;
46
    protected $languages = [];
47
48
    protected $allOptionsTranslation = [];
49
50
    // /**
51
    //  *  Constructor
52
    //  *
53
    //  * @param  \Translation\Models\Translation  $model     Bade model for queries.
54
    //  * @param  \Illuminate\Validation\Validator $validator Validator factory
55
    //  * @return void
56
    //  */
57
    // public function __construct(Translation $model, Application $app)
58
    // {
59
    //     $this->model         = $model;
60
    //     $this->app           = $app;
61
    //     $this->defaultLocale = $app['config']->get('app.locale');
62
    //     $this->database      = $app['db'];
63
    // }
64
    /**
65
     *  Constructor
66
     *
67
     * @param  \Translation\Models\Translation  $model     Bade model for queries.
68
     * @param  \Illuminate\Validation\Validator $validator Validator factory
69
     * @return void
70
     */
71
    public function __construct()
72
    {
73
        $inputPath = base_path('resources/lang');
0 ignored issues
show
Bug introduced by
The function base_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

73
        $inputPath = /** @scrutinizer ignore-call */ base_path('resources/lang');
Loading history...
74
        $languagesOptions = File::directories($inputPath);
75
        foreach ($languagesOptions as $langDir)
76
        {
77
            $this->addLanguageFolder($langDir);
78
        }
79
        
80
    }
81
    public function addLanguageFolder($langDir)
82
    {
83
        $lang = Str::afterLast($langDir, '/');
84
        $this->languages[$lang] = [
85
            'code' => $lang,
86
            'langDir' => $langDir,
87
            'options' => [
88
89
            ]
90
        ];
91
        $allFiles = File::files($langDir);
92
        // $allFiles = File::allFiles($langDir); //@todo pegar subPastas
93
        foreach ($allFiles as $f)
94
        {
95
            $f = $f->getPathname();
96
            if (!Str::endsWith($f, '.php')) {
97
                //@todo processar .yml 
98
                \Log::info('Não processando sem ser php -> '. $f);
99
                continue;
100
            }
101
            $indice = Str::between($f, '/', '.php');
102
            $indice = Str::afterLast($indice, '/');
103
            $this->languages[$lang]['options'][$indice] = $this->processFileLang($f, $indice);
104
        }
105
106
    }
107
    private function processFileLang($f, $indice)
108
    {
109
        $data = include($f);
110
        if (!is_array($data)) dd('LangFile', $data, $f, $indice); //@todo
0 ignored issues
show
Bug introduced by
The function dd was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

110
        if (!is_array($data)) /** @scrutinizer ignore-call */ dd('LangFile', $data, $f, $indice); //@todo
Loading history...
111
        if (!isset($this->allOptionsTranslation[$indice])) {
112
            $this->allOptionsTranslation[$indice] = [];
113
        }
114
        foreach ($data as $option => $result) {
115
            if (!in_array($option, $this->allOptionsTranslation[$indice])) {
116
                $this->allOptionsTranslation[$indice][] = $option;
117
            }
118
        }
119
        return $data;
120
    }
121
122
123
124
125
    public function hasLangExist(string $lang): bool
126
    {
127
        if (!isset($this->languages[$lang])) {
128
            $allLangs = [];
129
            foreach($this->languages as $value) {
130
                $allLangs = $value['code'];
131
            }
132
            dd('LangFile',$allLangs, $lang);
0 ignored issues
show
Bug introduced by
The function dd was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

132
            /** @scrutinizer ignore-call */ 
133
            dd('LangFile',$allLangs, $lang);
Loading history...
133
            return false;
134
        }
135
        return true;
136
    }
137
138
139
140
141
    public function getMissingForLang(string $lang)
142
    {
143
        if (!isset($this->languages[$lang])) {
144
            $allLangs = [];
145
            foreach($this->languages as $value) {
146
                $allLangs = $value['code'];
147
            }
148
            dd('LangFile',$allLangs, $lang);
0 ignored issues
show
Bug introduced by
The function dd was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

148
            /** @scrutinizer ignore-call */ 
149
            dd('LangFile',$allLangs, $lang);
Loading history...
149
        }
150
151
        $langData = $this->languages[$lang];
152
153
        $missing = [];
154
155
        foreach($this->allOptionsTranslation as $file=>$fileIndices) {
156
157
            if (!isset($langData['options'][$file])) {
158
                $missing[$file] = $file;
159
            } else {
160
                $missing[$file] = [];
161
                foreach($fileIndices as $fileIndice) {
162
                    if (!isset($langData['options'][$file][$fileIndice])) {
163
                        $missing[$file][$fileIndice] = $fileIndice;
164
                    }
165
                }
166
            }
167
        }
168
169
        return $missing;
170
    }
171
172
    public function mergeLangs($langTo, $langFrom)
173
    {
174
175
        $langDataTo = $this->languages[$langTo];
176
        $langDataFrom = $this->languages[$langFrom];
177
178
        $missing = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $missing is dead and can be removed.
Loading history...
179
        foreach($langDataFrom['options'] as $file=>$fileIndices) {
180
            $update = false;
181
            if (!isset($langDataTo['options'][$file])) {
182
                File::move(
183
                    $langDataFrom['langDir'] . DIRECTORY_SEPARATOR . $file. '.php',
184
                    $langDataTo['langDir'] . DIRECTORY_SEPARATOR . $file . '.php'
185
                );
186
                $langDataTo['options'][$file] = $langDataFrom['options'][$file];
187
            } else {
188
                foreach($fileIndices as $fileIndice=>$indiceValue) {
189
                    if (!isset($langDataTo['options'][$file][$fileIndice])) {
190
                        $update = true;
191
                        $langDataTo['options'][$file][$fileIndice] = $indiceValue;
192
                    }
193
                }
194
            }
195
196
            if (!$update) {
197
                // @todo escrever arquivo
198
                $formatted = $this->formatFileContents($langTo, $file);
199
                $this->writeLangFile($langTo, $file, $formatted);
200
                $update = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $update is dead and can be removed.
Loading history...
201
            }
202
        }
203
    }
204
}
205