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 | 4 | public function run(): string |
|
12 | { |
||
13 | 4 | $this->log('Start the handler for execution:', self::class); |
|
14 | |||
15 | 4 | return $this->doesntProtect() ? $this->delete() : Status::SKIPPED; |
|
16 | } |
||
17 | |||
18 | 4 | protected function doesntProtect(): bool |
|
19 | { |
||
20 | 4 | $this->log('Check if the localization is among the protected:', $this->locale); |
|
21 | |||
22 | 4 | return ! Locales::isProtected($this->locale); |
|
23 | } |
||
24 | |||
25 | 3 | protected function delete(): string |
|
26 | { |
||
27 | 3 | $this->log('Removing localization files:', $this->locale); |
|
28 | |||
29 | 3 | $status_dir = $this->deleteDirectory($this->locale); |
|
30 | 3 | $status_file = $this->deleteFile($this->locale); |
|
31 | |||
32 | 3 | return $this->resolveStatus($status_dir, $status_file); |
|
33 | } |
||
34 | |||
35 | 3 | protected function deleteDirectory(string $locale): string |
|
36 | { |
||
37 | 3 | $this->log('Removing the localization directory for the locale:', $locale); |
|
38 | |||
39 | 3 | $path = $this->pathTarget($locale); |
|
40 | |||
41 | 3 | return $this->directory($path); |
|
42 | } |
||
43 | |||
44 | 3 | protected function deleteFile(string $locale): string |
|
45 | { |
||
46 | 3 | $this->log('Removing the json localization file for the locale:', $locale); |
|
47 | |||
48 | 3 | $path = $this->pathTargetFull($locale, null); |
|
49 | |||
50 | 3 | return $this->file($path); |
|
51 | } |
||
52 | |||
53 | 3 | protected function resolveStatus(...$statuses): string |
|
54 | { |
||
55 | 3 | for ($i = 0; $i < count($statuses); $i++) { |
|
0 ignored issues
–
show
|
|||
56 | 3 | $current = $statuses[$i] ?? null; |
|
57 | 3 | $next = $statuses[$i + 1] ?? null; |
|
58 | |||
59 | 3 | if ($current !== $next && ! is_null($next)) { |
|
60 | 2 | return Status::SKIPPED; |
|
61 | } |
||
62 | } |
||
63 | |||
64 | 1 | return Status::DELETED; |
|
65 | } |
||
66 | |||
67 | 3 | protected function directory(string $path): string |
|
68 | { |
||
69 | 3 | if (File::exists($path)) { |
|
70 | 2 | File::deleteDirectory($path); |
|
71 | |||
72 | 2 | return Status::DELETED; |
|
73 | } |
||
74 | |||
75 | 1 | return Status::SKIPPED; |
|
76 | } |
||
77 | |||
78 | 3 | protected function file(string $path): string |
|
79 | { |
||
80 | 3 | if (File::exists($path)) { |
|
81 | File::delete($path); |
||
82 | |||
83 | return Status::DELETED; |
||
84 | } |
||
85 | |||
86 | 3 | return Status::SKIPPED; |
|
87 | } |
||
88 | } |
||
89 |
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: