Passed
Push — 9.x ( c610c7...55b0ab )
by Andrey
11:17
created

BaseCommand::ranFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
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\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
    abstract protected function processor(): Processor;
39
40
    public function handle()
41
    {
42
        $this->setLogger();
43
        $this->start();
44
        $this->clean();
45
        $this->ran();
46
        $this->end();
47
    }
48
49
    protected function ran(): void
50
    {
51
        foreach ($this->packages() as $package) {
52
            $this->log('Packages handling: ' . $package);
53
54
            $this->validatePackage($package);
55
56
            $this->ranLocales($package);
57
        }
58
    }
59
60
    protected function ranLocales(string $package): void
61
    {
62
        foreach ($this->locales() as $locale) {
63
            $this->log('Localization handling: ' . $locale);
64
65
            $this->validateLocale($locale);
66
67
            $this->ranFiles($package, $locale);
68
        }
69
    }
70
71
    protected function ranFiles(string $package, string $locale): void
72
    {
73
        foreach ($this->files($package) as $filename) {
74
            $this->log('Processing the localization file: ' . $filename);
75
76
            $status = $this->process($package, $locale, $filename);
77
78
            $this->processed($locale, $filename, $status, $package);
79
        }
80
    }
81
82
    protected function process(string $package, string $locale, string $filename): string
83
    {
84
        $this->log('Launching the processor for localization: ' . $locale . ', ' . $filename);
85
86
        return $this->processor()
87
            ->force($this->hasForce())
88
            ->package($package)
89
            ->locale($locale)
90
            ->filename($filename, $this->hasInline())
91
            ->run();
92
    }
93
94
    protected function locales(): array
95
    {
96
        $this->log('Getting a list of localizations...');
97
98
        if (! empty($this->locales)) {
99
            return $this->locales;
100
        }
101
102
        return $this->locales = LocalesSupport::make($this->input, $this->output, $this->action(), $this->targetLocales())->get();
103
    }
104
105
    protected function targetLocales(): array
106
    {
107
        $this->log('Getting a list of installed localizations...');
108
109
        return Locales::installed();
110
    }
111
112
    protected function packages(): array
113
    {
114
        return Packages::filtered();
115
    }
116
117
    protected function files(string $package): array
118
    {
119
        $this->log('Getting a list of files...');
120
121
        if ($this->files[$package] ?? false) {
122
            return $this->files[$package];
123
        }
124
125
        $path = $this->pathSource($package, LocalesList::ENGLISH);
126
127
        return $this->files[$package] = File::names($path, static function ($filename) {
128
            return ! Str::contains($filename, 'inline');
129
        });
130
    }
131
132
    protected function start(): void
133
    {
134
        $action = $this->action()->present(true);
135
136
        $this->info($action . ' localizations...');
137
    }
138
139
    protected function end(): void
140
    {
141
        $action = $this->action()->past();
142
143
        $this->info('Localizations have ben successfully ' . $action . '.');
144
    }
145
146
    protected function processed(string $locale, string $filename, string $status, string $package = null): void
147
    {
148
        $message = Message::same()
149
            ->length($this->localesLength(), $this->filesLength($package))
0 ignored issues
show
Bug introduced by
It seems like $package can also be of type null; however, parameter $package of Helldar\LaravelLangPubli...eCommand::filesLength() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
            ->length($this->localesLength(), $this->filesLength(/** @scrutinizer ignore-type */ $package))
Loading history...
150
            ->package($package)
0 ignored issues
show
Bug introduced by
It seems like $package can also be of type null; however, parameter $package of Helldar\LaravelLangPubli...port\Message::package() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
            ->package(/** @scrutinizer ignore-type */ $package)
Loading history...
151
            ->locale($locale)
152
            ->filename($filename)
153
            ->status($status)
154
            ->get();
155
156
        $this->line($message);
157
    }
158
159
    protected function localesLength(): int
160
    {
161
        $this->log('Getting the maximum length of a localization string...');
162
163
        if ($this->locales_length > 0) {
164
            return $this->locales_length;
165
        }
166
167
        $this->log('Calculating the maximum length of a localization string...');
168
169
        return $this->locales_length = Arr::longestStringLength($this->locales());
170
    }
171
172
    protected function filesLength(string $package): int
173
    {
174
        $this->log('Getting the maximum length of a filenames for ' . $package . '...');
175
176
        if ($this->files_length[$package] ?? false) {
177
            return $this->files_length[$package];
178
        }
179
180
        $this->log('Calculating the maximum length of a filenames for ' . $package . '...');
181
182
        return $this->files_length[$package] = Arr::longestStringLength($this->files($package));
183
    }
184
185
    protected function hasInline(): bool
186
    {
187
        $this->log('Getting a use case for a validation file.');
188
189
        return Config::hasInline();
190
    }
191
192
    protected function action(): Actionable
193
    {
194
        $this->log('Getting the action...');
195
196
        return $this->container($this->action);
197
    }
198
199
    protected function hasForce(): bool
200
    {
201
        return $this->boolOption('force');
202
    }
203
204
    protected function hasFull(): bool
205
    {
206
        return $this->boolOption('full');
207
    }
208
209
    protected function boolOption(string $key): bool
210
    {
211
        return $this->hasOption($key) && $this->option($key);
212
    }
213
214
    protected function validateLocale(string $locale): void
215
    {
216
        Validator::locale($locale);
217
    }
218
219
    protected function validatePackage(string $package): void
220
    {
221
        Validator::package($package);
222
    }
223
224
    protected function doesntProtect(string $locale): bool
225
    {
226
        return ! Locales::isProtected($locale);
227
    }
228
229
    protected function clean(): void
230
    {
231
        $this->log('Clear the variable from the saved localizations...');
232
233
        $this->locales = null;
234
    }
235
}
236