Passed
Pull Request — main (#84)
by Andrey
196:26 queued 181:26
created

Locales::hasAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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