Passed
Push — main ( 64d3ca...30b562 )
by Andrey
72:20 queued 69:59
created

BaseCommand   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 98.11%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 26
eloc 46
c 4
b 1
f 0
dl 0
loc 114
ccs 52
cts 53
cp 0.9811
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A isFull() 0 3 2
A installed() 0 3 1
A available() 0 3 1
A locales() 0 3 1
A __construct() 0 5 1
A select() 0 7 2
A isForce() 0 3 2
A localization() 0 3 1
A exec() 0 9 2
A wantsJson() 0 7 5
A setProcessor() 0 3 2
A getLocales() 0 5 3
A choiceLocales() 0 8 1
A wrapSelectedValues() 0 6 2
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Console;
4
5
use Helldar\LaravelLangPublisher\Contracts\Localizationable;
6
use Helldar\LaravelLangPublisher\Facades\Locale;
7
use Helldar\LaravelLangPublisher\Services\Localization;
8
use Helldar\LaravelLangPublisher\Support\Result;
9
use Helldar\LaravelLangPublisher\Traits\Containable;
10
use Helldar\LaravelLangPublisher\Traits\Containers\Pathable;
11
use Helldar\LaravelLangPublisher\Traits\Containers\Processable;
12
use Illuminate\Console\Command;
13
use Illuminate\Support\Arr;
14
15
abstract class BaseCommand extends Command
16
{
17
    use Containable;
18
    use Processable;
19
    use Pathable;
20
21
    /** @var \Helldar\LaravelLangPublisher\Support\Result */
22
    protected $result;
23
24
    protected $select_template = 'What languages to %s? (specify the necessary localizations separated by commas)';
25
26
    protected $select_all_template = 'Do you want to %s all localizations?';
27
28
    protected $action = 'install';
29
30
    protected $action_default = false;
31
32 19
    public function __construct(Result $result)
33
    {
34 19
        parent::__construct();
35
36 19
        $this->result = $result->setOutput($this);
37 19
    }
38
39 18
    protected function locales(): array
40
    {
41 18
        return (array) $this->argument('locales');
42
    }
43
44 14
    protected function available(): array
45
    {
46 14
        return Locale::available();
47
    }
48
49 5
    protected function installed(): array
50
    {
51 5
        return Locale::installed($this->wantsJson());
52
    }
53
54 11
    protected function select(array $locales): array
55
    {
56 11
        $question = sprintf($this->select_all_template, $this->action);
57
58 11
        return $this->confirm($question, $this->action_default)
59 4
            ? ['*']
60 11
            : $this->wrapSelectedValues($locales, $this->choiceLocales($locales));
61
    }
62
63 18
    protected function isForce(): bool
64
    {
65 18
        return $this->hasOption('force') && (bool) $this->option('force');
66
    }
67
68 18
    protected function isFull(): bool
69
    {
70 18
        return $this->hasOption('full') && (bool) $this->option('full');
71
    }
72
73 18
    protected function wantsJson(): bool
74
    {
75 18
        return (bool) $this->option('json')
76 13
            || (bool) $this->option('jet')
77 11
            || (bool) $this->option('fortify')
78 9
            || (bool) $this->option('cashier')
79 18
            || (bool) $this->option('nova');
80
    }
81
82 18
    protected function setProcessor(string $php, string $json): void
83
    {
84 18
        $this->processor = $this->wantsJson() ? $json : $php;
85 18
    }
86
87 18
    protected function exec(array $locales): void
88
    {
89 18
        foreach ($this->getLocales($locales) as $locale) {
90 18
            $this->result->merge(
91 18
                $this->localization()
92 18
                    ->processor($this->getProcessor())
93 18
                    ->force($this->isForce())
94 18
                    ->full($this->isFull())
95 18
                    ->run($locale)
96
            );
97
        }
98 12
    }
99
100 18
    protected function getLocales(array $locales): array
101
    {
102 18
        $items = $this->locales() ?: $this->select($locales);
103
104 18
        return $items === ['*'] ? $locales : $items;
105
    }
106
107 18
    protected function localization(): Localizationable
108
    {
109 18
        return app(Localization::class);
110
    }
111
112 7
    protected function wrapSelectedValues(array $available, $selected): array
113
    {
114 7
        return Arr::wrap(
115 7
            is_numeric($selected)
116
                ? Arr::get($available, (int) $selected)
117 7
                : $selected
118
        );
119
    }
120
121 7
    protected function choiceLocales(array $locales)
122
    {
123 7
        return $this->choice(
124 7
            sprintf($this->select_template, $this->action),
125
            $locales,
126 7
            null,
127 7
            null,
128 7
            true
129
        );
130
    }
131
}
132