Passed
Pull Request — main (#84)
by Andrey
196:26 queued 181:26
created

BaseCommand::action()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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