CheckCommand::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelLang\Commands;
6
7
use Arcanedev\LaravelLang\Contracts\TransChecker;
8
9
/**
10
 * Class     CheckCommand
11
 *
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class CheckCommand extends AbstractCommand
15
{
16
    /* -----------------------------------------------------------------
17
     |  Properties
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * The name and signature of the console command.
23
     *
24
     * @var string
25
     */
26
    protected $signature   = 'trans:check';
27
28
    /**
29
     * The console command description.
30
     *
31
     * @var string
32
     */
33
    protected $description = 'Check the missing translations.';
34
35
    /**
36
     * The TransChecker instance.
37
     *
38
     * @var \Arcanedev\LaravelLang\Contracts\TransChecker
39
     */
40
    private $checker;
41
42
    /**
43
     * Missing translations count.
44
     *
45
     * @var int
46
     */
47
    private $missingTranslations = 0;
48
49
    /* -----------------------------------------------------------------
50
     |  Constructor
51
     | -----------------------------------------------------------------
52
     */
53
54
    /**
55
     * Init the CheckCommand.
56
     *
57
     * @param  \Arcanedev\LaravelLang\Contracts\TransChecker  $checker
58
     */
59 48
    public function __construct(TransChecker $checker)
60
    {
61 48
        $this->name    = $this->signature;
62 48
        $this->checker = $checker;
63 48
        $this->missingTranslations   = 0;
64
65 48
        parent::__construct();
66 48
    }
67
68
    /* -----------------------------------------------------------------
69
     |  Main Methods
70
     | -----------------------------------------------------------------
71
     */
72
73
    /**
74
     * Execute the console command.
75
     */
76 12
    public function handle(): int
77
    {
78 12
        $this->copyright();
79
80 12
        $this->info('Checking the missing translations...');
81 12
        $this->line('');
82
83 12
        $missing = $this->checker->check();
84
85 12
        $this->table(['locale', 'translations'], $this->prepareRows($missing));
86
87 12
        $this->line('');
88 12
        $this->showMessage();
89 12
        $this->line('');
90
91 12
        return $this->getExitCode();
92
    }
93
94
    /* -----------------------------------------------------------------
95
     |  Other Methods
96
     | -----------------------------------------------------------------
97
     */
98
99
    /**
100
     * Prepare table rows.
101
     *
102
     * @param  array  $missing
103
     *
104
     * @return array
105
     */
106 12
    private function prepareRows(array $missing): array
107
    {
108 12
        $rows = [];
109
110 12
        foreach ($missing as $locale => $translations) {
111 6
            foreach ($translations as $translation) {
112 6
                $rows[] = [$locale, $translation];
113 6
                $this->missingTranslations++;
114
            }
115 6
            $rows[] = $this->tableSeparator();
116
        }
117
118 12
        $rows[] = ['Total', "{$this->missingTranslations} translations are missing."];
119
120 12
        return $rows;
121
    }
122
123
    /**
124
     * Show the message.
125
     *
126
     * @codeCoverageIgnore
127
     */
128
    private function showMessage(): void
129
    {
130
        if ($this->hasMissingTranslations())
131
            $this->comment('Try to fix your translations and run again the `trans:check` command.');
132
        else
133
            $this->info('No missing translations, YOU ROCK !! (^_^)b');
134
    }
135
136
    /**
137
     * Get the exit code.
138
     *
139
     * @return int
140
     */
141 12
    protected function getExitCode(): int
142
    {
143 12
        return $this->hasMissingTranslations() ? 1 : 0;
144
    }
145
146
    /**
147
     * Check if has missing translations.
148
     *
149
     * @return bool
150
     */
151 12
    protected function hasMissingTranslations(): bool
152
    {
153 12
        return $this->missingTranslations > 0;
154
    }
155
}
156