Passed
Push — master ( 707b07...190c0b )
by Reza
03:48
created

LangService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getContent() 0 3 1
A getPath() 0 3 1
A updateAll() 0 11 4
A updateLanguage() 0 9 1
A getLanguages() 0 10 1
A getFiles() 0 3 1
A getTexts() 0 3 1
1
<?php
2
3
namespace EasyPanel\Services;
4
5
use Illuminate\Support\Facades\File;
6
use Illuminate\Support\Str;
7
8
class LangService
9
{
10
    public static function getLanguages()
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 updateAll($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(app()->langPath().DIRECTORY_SEPARATOR.'*_panel.json');
0 ignored issues
show
introduced by
The method langPath() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

38
        return File::glob(app()->/** @scrutinizer ignore-call */ langPath().DIRECTORY_SEPARATOR.'*_panel.json');
Loading history...
39
    }
40
41
    public static function getTexts($lang)
42
    {
43
        return json_decode(static::getContent($lang), true);
44
    }
45
46
    public static function updateLanguage($lang, $texts)
47
    {
48
        $file = static::getContent($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 getContent($lang)
58
    {
59
        return File::get(static::getPath($lang));
60
    }
61
62
    public static function getPath($lang)
63
    {
64
        return app()->langPath().DIRECTORY_SEPARATOR."{$lang}.json";
65
    }
66
}
67