Passed
Push — main ( 3f2cb1...ed2a6d )
by Andrey
86:25 queued 84:06
created

BaseCommand::doesntProtect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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