Passed
Push — master ( d8cc68...c5f99b )
by Andrey
21:06 queued 05:59
created

BaseCommand::choiceLocales()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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