Passed
Push — 9.x ( d63f62...9358ea )
by Andrey
20:32 queued 05:32
created

Remove::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Services\Processors;
4
5
use Helldar\LaravelLangPublisher\Constants\Status;
6
use Helldar\LaravelLangPublisher\Facades\Locales;
7
use Illuminate\Support\Facades\File;
8
9
final class Remove extends Processor
10
{
11
    public function run(): string
12
    {
13
        return $this->doesntProtect() ? $this->delete() : Status::SKIPPED;
14
    }
15
16
    protected function doesntProtect(): bool
17
    {
18
        $this->log('Check if the localization is among the protected:', $this->locale);
19
20
        return ! Locales::isProtected($this->locale);
21
    }
22
23
    protected function delete(): string
24
    {
25
        $this->log('Removing json and php localization files:', $this->locale);
26
27
        $status_dir  = $this->deleteDirectory($this->locale);
28
        $status_file = $this->deleteFile($this->locale);
29
30
        return $status_dir === $status_file ? $status_dir : Status::DELETED;
31
    }
32
33
    protected function deleteDirectory(string $locale): string
34
    {
35
        $this->log('Removing the localization directory for the locale:', $locale);
36
37
        $path = $this->pathTarget($locale);
38
39
        if (File::exists($path)) {
40
            File::deleteDirectory($path);
41
42
            return Status::DELETED;
43
        }
44
45
        return Status::SKIPPED;
46
    }
47
48
    protected function deleteFile(string $locale): string
49
    {
50
        $this->log('Removing the json localization file for the locale:', $locale);
51
52
        $path = $this->pathTargetFull($locale, null, true);
53
54
        if (File::exists($path)) {
55
            File::delete($path);
56
57
            return Status::DELETED;
58
        }
59
60
        return Status::SKIPPED;
61
    }
62
}
63