Passed
Pull Request — 9.x (#103)
by Andrey
12:39
created

Remove::resolveStatus()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 10
c 0
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 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++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

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:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
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