Passed
Pull Request — main (#84)
by Andrey
59:33 queued 44:34
created

Locales::get()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

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