Passed
Push — main ( 55f929...127e84 )
by Andrey
161:41 queued 158:10
created

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