Passed
Pull Request — main (#84)
by Andrey
49:37 queued 34:36
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\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
    public function handle()
31
    {
32 19
        $this->start();
33
        $this->ran();
34 19
        $this->end();
35
    }
36 19
37 19
    abstract protected function processor(): Processor;
38
39 18
    protected function ran(): void
40
    {
41 18
        foreach ($this->locales() as $locale) {
42
            $this->log('Localization handling: ' . $locale);
43
44 14
            $this->validateLocale($locale);
45
46 14
            foreach ($this->files() as $filename) {
47
                $this->log('Processing the localization file: ' . $filename);
48
49 5
                $status = $this->process($locale, $filename);
50
51 5
                $this->processed($locale, $status);
52
            }
53
        }
54 11
    }
55
56 11
    protected function process(string $locale, string $filename): string
57
    {
58 11
        $this->log('Launching the processor for localization: ' . $locale . ', ' . $filename);
59 4
60 11
        return $this->processor()
61
            ->force($this->hasForce())
62
            ->locale($locale)
63 18
            ->filename($filename, $this->hasInline())
64
            ->run();
65 18
    }
66
67
    protected function locales(): array
68 18
    {
69
        $this->log('Getting a list of localizations...');
70 18
71
        return LocalesSupport::make($this->input, $this->output, $this->action(), $this->targetLocales())->get();
72
    }
73 18
74
    protected function targetLocales(): array
75 18
    {
76 13
        $this->log('Getting a list of installed localizations...');
77 11
78 9
        return Locales::installed();
79 18
    }
80
81
    protected function files(): array
82 18
    {
83
        $this->log('Getting a list of files...');
84 18
85 18
        if (! empty($this->files)) {
86
            return $this->files;
87 18
        }
88
89 18
        return $this->files = File::names(Path::source(LocalesList::ENGLISH), static function ($filename) {
90 18
            return ! Str::contains($filename, 'inline');
91 18
        });
92 18
    }
93 18
94 18
    protected function start(): void
95 18
    {
96
        $action = $this->action()->present(true);
97
98 12
        $this->info($action . ' localizations...');
99
    }
100 18
101
    protected function end(): void
102 18
    {
103
        $action = $this->action()->past();
104 18
105
        $this->info('Localizations have ben successfully ' . $action . '.');
106
    }
107 18
108
    protected function processed(string $locale, string $status): void
109 18
    {
110
        $locale = str_pad($locale . '...', $this->length() + 3);
111
112 7
        $this->info($locale . ' ' . $status);
113
    }
114 7
115 7
    protected function length(): int
116
    {
117 7
        $this->log('Getting the maximum length of a localization string...');
118
119
        if ($this->pad > 0) {
120
            return $this->pad;
121 7
        }
122
123 7
        $this->log('Calculating the maximum length of a localization string...');
124 7
125
        return $this->pad = Arr::longestStringLength($this->locales());
126 7
    }
127 7
128 7
    protected function hasInline(): bool
129
    {
130
        $this->log('Getting a use case for a validation file.');
131
132
        return Config::hasInline();
133
    }
134
135
    protected function action(): Actionable
136
    {
137
        $this->log('Getting the action...');
138
139
        return $this->container($this->action);
140
    }
141
142
    protected function hasForce(): bool
143
    {
144
        return $this->boolOption('force');
145
    }
146
147
    protected function hasFull(): bool
148
    {
149
        return $this->boolOption('full');
150
    }
151
152
    protected function boolOption(string $key): bool
153
    {
154
        return $this->hasOption($key) && $this->option($key);
155
    }
156
157
    protected function validateLocale(string $locale): void
158
    {
159
        Locales::validate($locale);
160
    }
161
}
162