Completed
Pull Request — master (#29)
by
unknown
12:01
created

JsonTranslator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 44
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTranslations() 0 13 2
A getMissingTranslations() 0 9 1
A fillLanguageFiles() 0 12 1
A getSourceTranslations() 0 4 1
1
<?php
2
3
namespace Ben182\AutoTranslate\AutoTranslators;
4
5
use Ben182\AutoTranslate\AutoTranslate;
6
use Ben182\AutoTranslate\Translators\TranslatorInterface;
7
8
class JsonTranslator extends AutoTranslate
9
{
10
    public function getTranslations(string $lang)
11
    {
12
        try {
13
            return json_decode(
14
                file_get_contents(
15
                    base_path("resources/lang/$lang.json")
16
                ),
17
                true
18
            );
19
        } catch (\ErrorException $e) {
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
20
            return [];
21
        }
22
    }
23
24
    public function getMissingTranslations(string $lang)
25
    {
26
        $sourceTranslations = $this->getSourceTranslations();
27
        $targetTranslations = $this->getTranslations($lang);
28
29
        $diff = array_diff(array_keys($sourceTranslations), array_keys($targetTranslations));
30
31
        return collect($sourceTranslations)->only($diff)->toArray();
32
    }
33
34
    public function fillLanguageFiles(string $language, array $translations)
35
    {
36
        $finalTranslations = array_merge(
37
            $this->getTranslations($language),
38
            $translations
39
        );
40
41
        file_put_contents(
42
            base_path("resources/lang/$language.json"),
43
            json_encode($finalTranslations, JSON_PRETTY_PRINT)
44
        );
45
    }
46
47
    public function getSourceTranslations()
48
    {
49
        return $this->getTranslations(config('auto-translate.source_language'));
50
    }
51
}
52