Passed
Push — main ( 545ade...3cfdef )
by Andrey
79:39 queued 77:29
created

BaseCommand::ranFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Console;
4
5
use Helldar\LaravelLangPublisher\Concerns\Containable;
6
use Helldar\LaravelLangPublisher\Concerns\Logger;
7
use Helldar\LaravelLangPublisher\Concerns\Pathable;
8
use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList;
9
use Helldar\LaravelLangPublisher\Contracts\Actionable;
10
use Helldar\LaravelLangPublisher\Contracts\Processor;
11
use Helldar\LaravelLangPublisher\Facades\Config;
12
use Helldar\LaravelLangPublisher\Facades\Locales;
13
use Helldar\LaravelLangPublisher\Facades\Message;
14
use Helldar\LaravelLangPublisher\Facades\Packages;
15
use Helldar\LaravelLangPublisher\Facades\Validator;
16
use Helldar\LaravelLangPublisher\Services\Command\Locales as LocalesSupport;
17
use Helldar\Support\Facades\Helpers\Arr;
18
use Helldar\Support\Facades\Helpers\Filesystem\File;
19
use Helldar\Support\Facades\Helpers\Str;
20
use Illuminate\Console\Command;
21
22
abstract class BaseCommand extends Command
23
{
24
    use Containable;
25
    use Logger;
26
    use Pathable;
27
28
    protected $action;
29
30
    protected $locales_length = 0;
31
32
    protected $files_length;
33
34
    protected $files;
35
36
    protected $locales;
37
38 15
    public function handle()
39
    {
40 15
        $this->setLogger();
41 15
        $this->start();
42 15
        $this->clean();
43 15
        $this->ran();
44 12
        $this->end();
45 12
    }
46
47
    abstract protected function processor(): Processor;
48
49 14
    protected function ran(): void
50
    {
51 14
        foreach ($this->packages() as $package) {
52 14
            $this->log('Packages handling: ' . $package);
53
54 14
            $this->validatePackage($package);
55
56 12
            $this->ranLocales($package);
57
        }
58 11
    }
59
60 12
    protected function ranLocales(string $package): void
61
    {
62 12
        foreach ($this->locales() as $locale) {
63 12
            $this->log('Localization handling: ' . $locale);
64
65 12
            $this->validateLocale($locale);
66
67 11
            $this->ranFiles($package, $locale);
68
        }
69 11
    }
70
71 11
    protected function ranFiles(string $package, string $locale): void
72
    {
73 11
        foreach ($this->files($package) as $filename) {
74 11
            $this->log('Processing the localization file: ' . $filename);
75
76 11
            $status = $this->process($package, $locale, $filename);
77
78 11
            $this->processed($locale, $filename, $status, $package);
79
        }
80 11
    }
81
82 11
    protected function process(string $package, string $locale, string $filename): string
83
    {
84 11
        $this->log('Launching the processor for localization: ' . $locale . ', ' . $filename);
85
86 11
        return $this->processor()
87 11
            ->force($this->hasForce())
88 11
            ->package($package)
89 11
            ->locale($locale)
90 11
            ->filename($filename, $this->hasInline())
91 11
            ->run();
92
    }
93
94 12
    protected function locales(): array
95
    {
96 12
        $this->log('Getting a list of localizations...');
97
98 12
        if (! empty($this->locales)) {
99 11
            return $this->locales;
100
        }
101
102 12
        return $this->locales = LocalesSupport::make($this->input, $this->output, $this->action(), $this->targetLocales())->get();
103
    }
104
105 7
    protected function targetLocales(): array
106
    {
107 7
        $this->log('Getting a list of installed localizations...');
108
109 7
        return Locales::installed();
110
    }
111
112 14
    protected function packages(): array
113
    {
114 14
        return Packages::get();
115
    }
116
117 11
    protected function files(string $package): array
118
    {
119 11
        $this->log('Getting a list of files...');
120
121 11
        if ($this->files[$package] ?? false) {
122 11
            return $this->files[$package];
123
        }
124
125 11
        $path = $this->pathSource($package, LocalesList::ENGLISH);
126
127 11
        return $this->files[$package] = File::names($path, static function ($filename) {
128 11
            return ! Str::contains($filename, 'inline');
129 11
        });
130
    }
131
132 15
    protected function start(): void
133
    {
134 15
        $action = $this->action()->present(true);
135
136 15
        $this->info($action . ' localizations...');
137 15
    }
138
139 12
    protected function end(): void
140
    {
141 12
        $action = $this->action()->past();
142
143 12
        $this->info('Localizations have ben successfully ' . $action . '.');
144 12
    }
145
146 12
    protected function processed(string $locale, string $filename, string $status, string $package = null): void
147
    {
148 12
        $message = Message::same()
149 12
            ->length($this->localesLength(), $this->filesLength($package))
150 12
            ->package($package)
151 12
            ->locale($locale)
152 12
            ->filename($filename)
153 12
            ->status($status)
154 12
            ->get();
155
156 12
        $this->line($message);
157 12
    }
158
159 12
    protected function localesLength(): int
160
    {
161 12
        $this->log('Getting the maximum length of a localization string...');
162
163 12
        if ($this->locales_length > 0) {
164 12
            return $this->locales_length;
165
        }
166
167 12
        $this->log('Calculating the maximum length of a localization string...');
168
169 12
        return $this->locales_length = Arr::longestStringLength($this->locales());
170
    }
171
172 12
    protected function filesLength(?string $package): int
173
    {
174 12
        $this->log('Getting the maximum length of a filenames for ' . $package . '...');
175
176 12
        if ($this->files_length[$package] ?? false) {
177 11
            return $this->files_length[$package];
178
        }
179
180 12
        $this->log('Calculating the maximum length of a filenames for ' . $package . '...');
181
182 12
        return $this->files_length[$package] = $package ? Arr::longestStringLength($this->files($package)) : 0;
183
    }
184
185 11
    protected function hasInline(): bool
186
    {
187 11
        $this->log('Getting a use case for a validation file.');
188
189 11
        return Config::hasInline();
190
    }
191
192 15
    protected function action(): Actionable
193
    {
194 15
        $this->log('Getting the action...');
195
196 15
        return $this->container($this->action);
197
    }
198
199 10
    protected function hasForce(): bool
200
    {
201 10
        return $this->boolOption('force');
202
    }
203
204 2
    protected function hasFull(): bool
205
    {
206 2
        return $this->boolOption('full');
207
    }
208
209 10
    protected function boolOption(string $key): bool
210
    {
211 10
        return $this->hasOption($key) && $this->option($key);
212
    }
213
214 13
    protected function validateLocale(string $locale): void
215
    {
216 13
        Validator::locale($locale);
217 12
    }
218
219 14
    protected function validatePackage(string $package): void
220
    {
221 14
        Validator::package($package);
222 12
    }
223
224 4
    protected function doesntProtect(string $locale): bool
225
    {
226 4
        return ! Locales::isProtected($locale);
227
    }
228
229 15
    protected function clean(): void
230
    {
231 15
        $this->log('Clear the variable from the saved localizations...');
232
233 15
        $this->locales = null;
234 15
    }
235
}
236