Passed
Push — master ( bcac8b...ca1b68 )
by Reza
03:47
created

LangManager::updateLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
namespace EasyPanel\Services;
4
5
use Illuminate\Support\Facades\File;
6
use Illuminate\Support\Str;
7
8
class LangManager
9
{
10
    public static function get()
11
    {
12
        $files = collect(static::getFiles());
13
14
        return $files->mapWithKeys(function ($file, $key){
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

14
        return $files->mapWithKeys(function ($file, /** @scrutinizer ignore-unused */ $key){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
15
            preg_match('/(\w+)_panel\.json/i', $file, $m);
16
            $key = "$m[1]_panel";
17
            $value = Str::upper($m[1]);
18
            return [$key => $value];
19
        })->toArray();
20
    }
21
22
    public static function update($texts)
23
    {
24
        foreach (static::getFiles() as $file) {
25
            $decodedFile = json_decode(File::get($file), 1);
26
            foreach ($texts as $key => $text) {
27
                if (array_key_exists($key, $decodedFile)){
28
                    unset($texts[$text]);
29
                }
30
            }
31
            $array = array_merge($decodedFile, $texts);
32
            File::put($file, json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
33
        }
34
    }
35
36
    public static function getFiles()
37
    {
38
        return File::glob(resource_path('lang/*_panel.json'));
39
    }
40
41
    public static function getTexts($lang)
42
    {
43
        return json_decode(static::getLang($lang), true);
44
    }
45
46
    public static function updateLanguage($lang, $texts)
47
    {
48
        $file = static::getLang($lang);
49
50
        $decodedFile = json_decode($file, 1);
51
52
        $array = array_merge($decodedFile, $texts);
53
54
        File::put(static::getPath($lang), json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
55
    }
56
57
    public static function getLang($lang)
58
    {
59
        return File::get(static::getPath($lang));
60
    }
61
62
    public static function getPath($lang)
63
    {
64
        return resource_path("lang/{$lang}.json");
65
    }
66
}
67