Completed
Push — develop ( 0d976a...21f0a6 )
by ARCANEDEV
13:35 queued 12:35
created

CheckCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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