Passed
Push — main ( 55f929...127e84 )
by Andrey
161:41 queued 158:10
created

Locales::get()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
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 12
    public function __construct(InputInterface $input, OutputStyle $output, Actionable $action, array $locales)
32
    {
33 12
        $this->log('Object initialization: ' . self::class);
34
35 12
        $this->input   = $input;
36 12
        $this->output  = $output;
37 12
        $this->action  = $action;
38 12
        $this->locales = $locales;
39 12
    }
40
41 12
    public function get(): array
42
    {
43 12
        $input = $this->input();
44
45 12
        if ($this->hasAll($input) && $this->confirm()) {
46 2
            $this->log('Returning a list of all localizations...');
47
48 2
            return $this->locales;
49
        }
50
51 10
        if (! empty($input)) {
52 8
            $this->log('Returning a input list of localizations...');
53
54 8
            return $this->correctLocalesList($input);
55
        }
56
57 3
        $this->log('Asking what localizations need to be done...');
58
59 3
        return $this->correctLocalesList($this->select());
60
    }
61
62 3
    protected function select(): array
63
    {
64 3
        $this->log('Displaying an interactive question with a choice of localizations...');
65
66 3
        if ($locales = $this->ask()) {
67 3
            return $locales;
68
        }
69
70
        return $this->ask();
71
    }
72
73 5
    protected function confirm(): bool
74
    {
75 5
        $this->log('Confirmation of processing of all localizations...');
76
77 5
        return $this->output->confirm($this->confirmQuestion());
78
    }
79
80 3
    protected function ask(): ?array
81
    {
82 3
        $this->log('Localization selection request...');
83
84 3
        return (array) $this->output->choice($this->choiceQuestion(), $this->locales);
85
    }
86
87 12
    protected function input(): array
88
    {
89 12
        $this->log('Getting a list of localizations from arguments...');
90
91 12
        return (array) $this->input->getArgument('locales');
92
    }
93
94 3
    protected function choiceQuestion(): string
95
    {
96 3
        return sprintf($this->select_template, $this->action->future());
97
    }
98
99 5
    protected function confirmQuestion(): string
100
    {
101 5
        return sprintf($this->select_all_template, $this->action->future());
102
    }
103
104 10
    protected function correctLocalesList(array $locales): array
105
    {
106 10
        $this->log('Correction of the array of localizations...');
107
108 10
        return $this->hasAll($locales) ? $this->locales : $locales;
109
    }
110
111 12
    protected function hasAll(array $locales): bool
112
    {
113 12
        $this->log('Checking for occurrence of the return character of all localizations...');
114
115 12
        return in_array('*', $locales, true) || empty($locales);
116
    }
117
}
118