Passed
Push — master ( 51edae...d29a9b )
by Curtis
11:52 queued 05:54
created

Updater::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
rs 10
1
<?php
2
3
namespace App\Service\enso\Localisation;
4
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Support\Facades\File;
7
use App\Models\enso\Localisation\Language;
8
use LaravelEnso\Localisation\Services\Traits\JsonFilePathResolver;
9
use LaravelEnso\Localisation\Services\Traits\LegacyFolderPathResolver;
10
11
class Updater
12
{
13
    use JsonFilePathResolver, LegacyFolderPathResolver;
14
15
    private Language $language;
16
    private array $request;
17
    private string $flagSufix;
18
19
    public function __construct(Language $language, array $request, string $flagSufix)
20
    {
21
        $this->language = $language;
22
        $this->request = $request;
23
        $this->flagSufix = $flagSufix;
24
    }
25
26
    public function run()
27
    {
28
        DB::transaction(function () {
29
            $oldName = $this->language->name;
30
            $this->language->updateWithFlagSufix($this->request, $this->flagSufix);
31
            $this->updateLangFiles($oldName, $this->request['name']);
32
        });
33
    }
34
35
    public function updateJson($oldName, $newName)
36
    {
37
        File::move(
38
            $this->jsonFileName($oldName),
39
            $this->jsonFileName($newName)
40
        );
41
    }
42
43
    public function updateAppJson($oldName, $newName)
44
    {
45
        File::move(
46
            $this->jsonFileName($oldName, 'app'),
47
            $this->jsonFileName($newName, 'app')
48
        );
49
    }
50
51
    public function updateEnsoJson($oldName, $newName)
52
    {
53
        File::move(
54
            $this->jsonFileName($oldName, 'enso'),
55
            $this->jsonFileName($newName, 'enso')
56
        );
57
    }
58
59
    private function updateLangFiles(string $oldName, string $newName)
60
    {
61
        if ($oldName === $newName) {
62
            return;
63
        }
64
65
        $this->updateJson($oldName, $newName);
66
        $this->updateAppJson($oldName, $newName);
67
        $this->updateEnsoJson($oldName, $newName);
68
        $this->updateLegacyFolder($oldName, $newName);
69
    }
70
71
    private function updateLegacyFolder($oldName, $newName)
72
    {
73
        File::move(
74
            $this->legacyFolderName($oldName),
75
            $this->legacyFolderName($newName)
76
        );
77
    }
78
}
79