Passed
Pull Request — main (#84)
by Andrey
59:33 queued 44:34
created

BaseCommand::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
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 $locales;
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
            ->locale($locale)
62
            ->filename($filename, $this->hasInline())
63 18
            ->run();
64
    }
65 18
66
    protected function locales(): array
67
    {
68 18
        $this->log('Getting a list of localizations...');
69
70 18
        if (! empty($this->locales)) {
71
            return $this->locales;
72
        }
73 18
74
        $this->log('Localization list request...');
75 18
76 13
        return $this->locales = LocalesSupport::make($this, $this->action(), $this->targetLocales())->get();
77 11
    }
78 9
79 18
    protected function targetLocales(): array
80
    {
81
        $this->log('Getting a list of available localizations...');
82 18
83
        return Locales::available();
84 18
    }
85 18
86
    protected function files(): array
87 18
    {
88
        $this->log('Getting a list of files...');
89 18
90 18
        return File::names(Path::source(LocalesList::ENGLISH), static function ($filename) {
91 18
            return ! Str::contains($filename, 'inline');
92 18
        });
93 18
    }
94 18
95 18
    protected function start(): void
96
    {
97
        $action = $this->action()->present(true);
98 12
99
        $this->info($action . ' localizations...');
100 18
    }
101
102 18
    protected function end(): void
103
    {
104 18
        $action = $this->action()->past();
105
106
        $this->info('Localizations have ben successfully ' . $action . '.');
107 18
    }
108
109 18
    protected function processed(string $locale, string $status): void
110
    {
111
        $locale = str_pad($locale . '...', $this->length() + 3);
112 7
113
        $this->info($locale . ' ' . $status);
114 7
    }
115 7
116
    protected function length(): int
117 7
    {
118
        $this->log('Getting the maximum length of a localization string...');
119
120
        if ($this->pad > 0) {
121 7
            return $this->pad;
122
        }
123 7
124 7
        $this->log('Calculating the maximum length of a localization string...');
125
126 7
        return $this->pad = Arr::longestStringLength($this->locales());
127 7
    }
128 7
129
    protected function hasInline(): bool
130
    {
131
        $this->log('Getting a use case for a validation file.');
132
133
        return Config::hasInline();
134
    }
135
136
    protected function action(): Actionable
137
    {
138
        $this->log('Getting the action...');
139
140
        return $this->container($this->action);
141
    }
142
143
    protected function hasForce(): bool
144
    {
145
        return $this->boolOption('force');
146
    }
147
148
    protected function hasFull(): bool
149
    {
150
        return $this->boolOption('full');
151
    }
152
153
    protected function boolOption(string $key): bool
154
    {
155
        return $this->hasOption($key) && $this->option($key);
156
    }
157
158
    protected function validateLocale(string $locale): void
159
    {
160
        Locales::validate($locale);
161
    }
162
}
163