|
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 localization files:', $this->locale); |
|
26
|
|
|
|
|
27
|
|
|
$status_dir = $this->deleteDirectory($this->locale); |
|
28
|
|
|
$status_file = $this->deleteFile($this->locale); |
|
29
|
|
|
|
|
30
|
|
|
return $this->resolveStatus($status_dir, $status_file); |
|
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
|
|
|
return $this->directory($path); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function deleteFile(string $locale): string |
|
43
|
|
|
{ |
|
44
|
|
|
$this->log('Removing the json localization file for the locale:', $locale); |
|
45
|
|
|
|
|
46
|
|
|
$path = $this->pathTargetFull($locale, null); |
|
47
|
|
|
|
|
48
|
|
|
return $this->file($path); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function resolveStatus(...$statuses): string |
|
52
|
|
|
{ |
|
53
|
|
|
for ($i = 0; $i < count($statuses); $i++) { |
|
|
|
|
|
|
54
|
|
|
$current = $statuses[$i] ?? null; |
|
55
|
|
|
$next = $statuses[$i + 1] ?? null; |
|
56
|
|
|
|
|
57
|
|
|
if ($current !== $next && ! is_null($next)) { |
|
58
|
|
|
return Status::SKIPPED; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return Status::DELETED; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function directory(string $path): string |
|
66
|
|
|
{ |
|
67
|
|
|
if (File::exists($path)) { |
|
68
|
|
|
File::deleteDirectory($path); |
|
69
|
|
|
|
|
70
|
|
|
return Status::DELETED; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return Status::SKIPPED; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function file(string $path): string |
|
77
|
|
|
{ |
|
78
|
|
|
if (File::exists($path)) { |
|
79
|
|
|
File::delete($path); |
|
80
|
|
|
|
|
81
|
|
|
return Status::DELETED; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return Status::SKIPPED; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: