Passed
Push — 9.x ( 025072...127945 )
by Andrey
15:06
created

BaseCommand::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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