Passed
Push — main ( 3f2cb1...ed2a6d )
by Andrey
86:25 queued 84:06
created

Locales::select()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Services\Command;
4
5
use Helldar\LaravelLangPublisher\Concerns\Logger;
6
use Helldar\LaravelLangPublisher\Contracts\Actionable;
7
use Helldar\Support\Concerns\Makeable;
8
use Illuminate\Console\OutputStyle;
9
use Symfony\Component\Console\Input\InputInterface;
10
11
/**
12
 * @method static Locales make(InputInterface $input, OutputStyle $output, Actionable $action, array $locales)
13
 */
14
final class Locales
15
{
16
    use Logger;
17
    use Makeable;
18
19
    protected $input;
20
21
    protected $output;
22
23
    protected $action;
24
25
    protected $locales = [];
26
27
    protected $select_template = 'What languages to %s? (specify the necessary localizations separated by commas)';
28
29
    protected $select_all_template = 'Do you want to %s all localizations?';
30
31 15
    public function __construct(InputInterface $input, OutputStyle $output, Actionable $action, array $locales)
32
    {
33 15
        $this->log('Object initialization:', self::class);
34
35 15
        $this->input   = $input;
36 15
        $this->output  = $output;
37 15
        $this->action  = $action;
38 15
        $this->locales = $locales;
39 15
    }
40
41 15
    public function get(): array
42
    {
43 15
        $this->log('Getting a list of localizations...');
44
45 15
        $input = $this->input();
46
47 15
        if ($this->hasAll($input) && $this->confirm()) {
48 2
            $this->log('Returning a list of all localizations...');
49
50 2
            return $this->locales;
51
        }
52
53 13
        if (! empty($input)) {
54 11
            $this->log('Returning a input list of localizations...');
55
56 11
            return $this->correctLocalesList($input);
57
        }
58
59 3
        $this->log('Asking what localizations need to be done...');
60
61 3
        return $this->correctLocalesList($this->select());
62
    }
63
64 3
    protected function select(): array
65
    {
66 3
        $this->log('Displaying an interactive question with a choice of localizations...');
67
68 3
        $locales = null;
69
70 3
        while (! $locales) {
71 3
            $locales = $this->ask();
72
        }
73
74 3
        return $locales;
75
    }
76
77 5
    protected function confirm(): bool
78
    {
79 5
        $this->log('Confirmation of processing of all localizations...');
80
81 5
        return $this->output->confirm($this->confirmQuestion());
82
    }
83
84 3
    protected function ask(): ?array
85
    {
86 3
        $this->log('Localization selection request...');
87
88 3
        return (array) $this->output->choice($this->choiceQuestion(), $this->locales);
89
    }
90
91 15
    protected function input(): array
92
    {
93 15
        $this->log('Getting a list of localizations from arguments...');
94
95 15
        return (array) $this->input->getArgument('locales');
96
    }
97
98 3
    protected function choiceQuestion(): string
99
    {
100 3
        return sprintf($this->select_template, $this->action->future());
101
    }
102
103 5
    protected function confirmQuestion(): string
104
    {
105 5
        return sprintf($this->select_all_template, $this->action->future());
106
    }
107
108 13
    protected function correctLocalesList(array $locales): array
109
    {
110 13
        $this->log('Correction of the array of localizations...');
111
112 13
        return $this->hasAll($locales) ? $this->locales : $locales;
113
    }
114
115 15
    protected function hasAll(array $locales): bool
116
    {
117 15
        $this->log('Checking for occurrence of the return character of all localizations...');
118
119 15
        return in_array('*', $locales, true) || empty($locales);
120
    }
121
}
122