Passed
Pull Request — master (#1306)
by Curtis
06:06
created

Updater   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 83
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addNewKeys() 0 11 1
A __construct() 0 5 1
A updateDifferences() 0 9 3
A addKey() 0 4 1
A extraLangs() 0 5 1
A run() 0 8 1
A processKey() 0 5 1
A removeExtraKeys() 0 10 1
A extraLangFile() 0 3 1
1
<?php
2
3
namespace App\Service\enso\Localisation\Json;
4
5
use Illuminate\Support\Collection;
6
use LaravelEnso\Helpers\Services\JsonReader;
7
use App\Models\enso\Localisation\Language;
8
9
class Updater extends Handler
10
{
11
    private ?string $locale;
12
    private array $langArray;
13
    private ?string $subDir;
14
15
    public function __construct(Language $language, array $langArray, ?string $subDir = null)
16
    {
17
        $this->langArray = $langArray;
18
        $this->locale = $language->name;
19
        $this->subDir = $subDir;
20
    }
21
22
    public function run()
23
    {
24
        $this->savePartial(
25
            $this->locale, $this->langArray, $this->subDir
0 ignored issues
show
Bug introduced by
It seems like $this->subDir can also be of type null; however, parameter $subDir of App\Service\enso\Localis...\Handler::savePartial() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

25
            $this->locale, $this->langArray, /** @scrutinizer ignore-type */ $this->subDir
Loading history...
Bug introduced by
It seems like $this->locale can also be of type null; however, parameter $locale of App\Service\enso\Localis...\Handler::savePartial() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

25
            /** @scrutinizer ignore-type */ $this->locale, $this->langArray, $this->subDir
Loading history...
26
        );
27
28
        $this->extraLangs()
29
            ->each(fn ($locale) => $this->updateDifferences($locale));
30
    }
31
32
    public function addKey()
33
    {
34
        $this->extraLangs()
35
            ->each(fn ($locale) => $this->processKey($locale));
36
    }
37
38
    private function processKey($locale)
39
    {
40
        $extraLangFile = $this->extraLangFile($locale, $this->updateDir());
41
        [$addedCount, $extraLangFile] = $this->addNewKeys($extraLangFile);
42
        $this->savePartial($locale, $extraLangFile, $this->updateDir());
43
    }
44
45
    private function updateDifferences(string $locale)
46
    {
47
        $removedCount = $addedCount = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $addedCount is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $removedCount is dead and can be removed.
Loading history...
48
        $extraLangFile = $this->extraLangFile($locale, $this->subDir);
0 ignored issues
show
Bug introduced by
It seems like $this->subDir can also be of type null; however, parameter $subDir of App\Service\enso\Localis...pdater::extraLangFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

48
        $extraLangFile = $this->extraLangFile($locale, /** @scrutinizer ignore-type */ $this->subDir);
Loading history...
49
        [$removedCount, $extraLangFile] = $this->removeExtraKeys($extraLangFile);
50
        [$addedCount, $extraLangFile] = $this->addNewKeys($extraLangFile);
51
52
        if ($addedCount || $removedCount) {
53
            $this->savePartial($locale, $extraLangFile, $this->subDir);
0 ignored issues
show
Bug introduced by
It seems like $this->subDir can also be of type null; however, parameter $subDir of App\Service\enso\Localis...\Handler::savePartial() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

53
            $this->savePartial($locale, $extraLangFile, /** @scrutinizer ignore-type */ $this->subDir);
Loading history...
54
        }
55
    }
56
57
    private function removeExtraKeys(array $extraLangFile)
58
    {
59
        $keysToRemove = (new Collection($extraLangFile))
60
            ->diffKeys($this->langArray)
61
            ->keys();
62
63
        $extraLangFile = (new Collection($extraLangFile))
64
            ->filter(fn ($key) => $keysToRemove->contains($key));
65
66
        return [$keysToRemove->count(), $extraLangFile->toArray()];
67
    }
68
69
    private function addNewKeys(array $extraLangFile)
70
    {
71
        $keysToAdd = (new Collection($this->langArray))
72
            ->diffKeys($extraLangFile);
73
74
        $arrayToAdd = $this->newTranslations($keysToAdd->all());
75
76
        $extraLangFile = (new Collection($arrayToAdd))
77
            ->merge($extraLangFile);
78
79
        return [$keysToAdd->count(), $extraLangFile->toArray()];
80
    }
81
82
    private function extraLangFile(string $locale, string $subDir)
83
    {
84
        return (new JsonReader($this->jsonFileName($locale, $subDir)))->array();
85
    }
86
87
    private function extraLangs()
88
    {
89
        return Language::extra()
90
            ->where('name', '<>', $this->locale)
91
            ->pluck('name');
92
    }
93
}
94