Passed
Pull Request — main (#100)
by Andrey
70:18 queued 55:15
created

BaseCommand::localesLength()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 11
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\Info;
13
use Helldar\LaravelLangPublisher\Facades\Locales;
14
use Helldar\LaravelLangPublisher\Facades\Packages;
15
use Helldar\LaravelLangPublisher\Facades\Validator;
16
use Helldar\LaravelLangPublisher\Services\Command\Locales as LocalesSupport;
17
use Helldar\LaravelLangPublisher\Support\Info as InfoSupport;
18
use Helldar\Support\Facades\Helpers\Arr;
19
use Helldar\Support\Facades\Helpers\Filesystem\File;
20
use Helldar\Support\Facades\Helpers\Str;
21
use Illuminate\Console\Command;
22
23
abstract class BaseCommand extends Command
24
{
25
    use Containable;
26
    use Logger;
27
    use Pathable;
28
29
    protected $action;
30
31
    protected $locales_length = 0;
32
33
    protected $files_length = 0;
34
35
    protected $files;
36
37
    protected $locales;
38 15
39
    public function handle()
40 15
    {
41 15
        $this->setLogger();
42 15
        $this->start();
43 15
        $this->clean();
44 12
        $this->ran();
45 12
        $this->end();
46
    }
47
48
    abstract protected function processor(): Processor;
49 14
50
    protected function ran(): void
51 14
    {
52 14
        $this->log('Starting processing of the package list...');
53
54 14
        foreach ($this->packages() as $package) {
55
            $this->log('Packages handling:', $package);
56 12
57
            $this->validatePackage($package);
58 11
59
            $this->ranLocales($package);
60 12
        }
61
    }
62 12
63 12
    protected function ranLocales(string $package): void
64
    {
65 12
        $this->log('Starting processing of the locales list for the', $package, 'package...');
66
67 11
        foreach ($this->locales() as $locale) {
68
            $this->log('Localization handling:', $locale);
69 11
70
            $this->validateLocale($locale);
71 11
72
            $this->ranFiles($package, $locale);
73 11
        }
74 11
    }
75
76 11
    protected function ranFiles(string $package, string $locale): void
77
    {
78 11
        $this->log('Starting processing of the files for the', $package, 'package and', $locale, 'localization...');
79
80 11
        foreach ($this->files($package) as $filename) {
81
            $this->log('Processing the localization file:', $filename);
82 11
83
            $this->processing($locale, $filename, $package);
84 11
85
            $status = $this->process($package, $locale, $filename);
86 11
87 11
            $this->processed($locale, $filename, $status, $package);
88 11
        }
89 11
    }
90 11
91 11
    protected function process(string $package, string $locale, string $filename): string
92
    {
93
        $this->log('Launching the processor for localization:', $locale, ',', $filename);
94 12
95
        return $this->processor()
96 12
            ->force($this->hasForce())
97
            ->package($package)
98 12
            ->locale($locale)
99 11
            ->filename($filename, $this->hasInline())
100
            ->run();
101
    }
102 12
103
    protected function locales(): array
104
    {
105 7
        $this->log('Getting a list of localizations...');
106
107 7
        if (! empty($this->locales)) {
108
            return $this->locales;
109 7
        }
110
111
        return $this->locales = LocalesSupport::make($this->input, $this->output, $this->action(), $this->targetLocales())->get();
112 14
    }
113
114 14
    protected function targetLocales(): array
115
    {
116
        $this->log('Getting a list of installed localizations...');
117 11
118
        return Locales::installed();
119 11
    }
120
121 11
    protected function packages(): array
122 11
    {
123
        $this->log('Getting a list of packages available for processing...');
124
125 11
        return Packages::get();
126
    }
127 11
128 11
    protected function files(string $package): array
129 11
    {
130
        $this->log('Getting a list of files for the ', $package, 'package...');
131
132 15
        if ($this->files[$package] ?? false) {
133
            return $this->files[$package];
134 15
        }
135
136 15
        $path = $this->pathSource($package, LocalesList::ENGLISH);
137 15
138
        return $this->files[$package] = File::names($path, static function ($filename) {
139 12
            return ! Str::contains($filename, 'inline');
140
        });
141 12
    }
142
143 12
    protected function start(): void
144 12
    {
145
        $this->log('Running the console command:', parent::class);
146 12
147
        $action = $this->action()->present(true);
148 12
149 12
        $this->info($action . ' localizations...');
150 12
    }
151 12
152 12
    protected function end(): void
153 12
    {
154 12
        $this->log('Completing the execution of the console command...');
155
156 12
        $action = $this->action()->past();
157 12
158
        $this->info('Localizations have ben successfully ' . $action . '.');
159 12
    }
160
161 12
    protected function processing(string $locale, string $filename, string $package = null): void
162
    {
163 12
        $this->log('Displaying a message about the start of file processing: locale is', $locale, ', filename is', $filename, ', package is', $package . '...');
164 12
165
        $message = $this->message($locale, $filename, $package)->start();
166
167 12
        $this->output->write($message);
168
    }
169 12
170
    protected function processed(string $locale, string $filename, string $status, string $package = null): void
171
    {
172 12
        $this->log('Displaying a message about the finish of file processing: locale is', $locale, ', filename is', $filename, ', package is', $package . '...');
173
174 12
        $message = $this->message($locale, $filename, $package)->finish($status);
175
176 12
        $this->output->writeln($message);
177 11
    }
178
179
    protected function message(string $locale, string $filename, string $package = null): InfoSupport
180 12
    {
181
        $this->log('Preparing an object for displaying a message: locale is', $locale, ', filename is', $filename, ', package is', $package . '...');
182 12
183
        return Info::same()
184
            ->package($package)
185 11
            ->locale($locale, $this->localesLength())
186
            ->filename($filename, $this->filesLength());
187 11
    }
188
189 11
    protected function localesLength(): int
190
    {
191
        $this->log('Getting the maximum length of a localization string...');
192 15
193
        if ($this->locales_length > 0) {
194 15
            return $this->locales_length;
195
        }
196 15
197
        $this->log('Calculating the maximum length of a localization string...');
198
199 10
        return $this->locales_length = Arr::longestStringLength($this->locales());
200
    }
201 10
202
    protected function filesLength(): int
203
    {
204 2
        $this->log('Getting the maximum length of a filenames...');
205
206 2
        if ($this->files_length > 0) {
207
            return $this->files_length;
208
        }
209 10
210
        $this->log('Calculating the maximum length of a filenames...');
211 10
212
        $files = [];
213
214 13
        foreach ($this->packages() as $package) {
215
            $files = array_merge($files, $this->files($package));
216 13
        }
217 12
218
        return $this->files_length = Arr::longestStringLength(array_unique($files));
219 14
    }
220
221 14
    protected function hasInline(): bool
222 12
    {
223
        $this->log('Getting a use case for a validation file.');
224 4
225
        return Config::hasInline();
226 4
    }
227
228
    protected function action(): Actionable
229 15
    {
230
        $this->log('Getting the action...');
231 15
232
        return $this->container($this->action);
233 15
    }
234 15
235
    protected function hasForce(): bool
236
    {
237
        $this->log('Getting the value of the "force" option...');
238
239
        return $this->boolOption('force');
240
    }
241
242
    protected function hasFull(): bool
243
    {
244
        $this->log('Getting the value of the "full" option...');
245
246
        return $this->boolOption('full');
247
    }
248
249
    protected function boolOption(string $key): bool
250
    {
251
        $this->log('Getting the value of the "', $key, '" option...');
252
253
        return $this->hasOption($key) && $this->option($key);
254
    }
255
256
    protected function validateLocale(string $locale): void
257
    {
258
        $this->log('Calling the localization validation method: ', $locale, '...');
259
260
        Validator::locale($locale);
261
    }
262
263
    protected function validatePackage(string $package): void
264
    {
265
        $this->log('Calling the package validation method: ', $package, '...');
266
267
        Validator::package($package);
268
    }
269
270
    protected function doesntProtect(string $locale): bool
271
    {
272
        $this->log('Check if the localization is among the protected:', $locale);
273
274
        return ! Locales::isProtected($locale);
275
    }
276
277
    protected function clean(): void
278
    {
279
        $this->log('Clear the variable from the saved localizations...');
280
281
        $this->locales = null;
282
    }
283
}
284