Completed
Push — master ( 4e6963...479a33 )
by Benjamin
15s
created

MissingCommand::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 17
cp 0
rs 9.504
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Ben182\AutoTranslate\Commands;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Console\Command;
7
use Ben182\AutoTranslate\AutoTranslate;
8
9
class MissingCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'autotrans:missing';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Translates all source translations that are not set in your target translations';
24
25
    protected $autoTranslator;
26
27
    /**
28
     * Create a new command instance.
29
     *
30
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
31
     */
32
    public function __construct(AutoTranslate $autoTranslator)
33
    {
34
        parent::__construct();
35
        $this->autoTranslator = $autoTranslator;
36
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return mixed
42
     */
43
    public function handle()
44
    {
45
        $targetLanguages = Arr::wrap(config('auto-translate.target_language'));
46
47
        $this->line('Found '.count($targetLanguages).' languages to translate');
48
49
        $bar = $this->output->createProgressBar(count($targetLanguages));
50
        $bar->start();
51
52
        $missingCount = 0;
53
54
        foreach ($targetLanguages as $targetLanguage) {
55
            $missing = $this->autoTranslator->getMissingTranslations($targetLanguage);
56
            $missingCount += $missing->count();
57
58
            $translated = $this->autoTranslator->translate($targetLanguage, $missing);
59
60
            $this->autoTranslator->fillLanguageFiles($targetLanguage, $translated);
61
62
            $bar->advance();
63
        }
64
65
        $bar->finish();
66
67
        $this->info('Translated '.$missingCount.' missing language keys.');
68
    }
69
}
70