Completed
Push — master ( b4607a...6c7f70 )
by Benjamin
09:10 queued 08:07
created

MissingCommand::handle()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 47

Duplication

Lines 9
Ratio 19.15 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 9
loc 47
ccs 0
cts 33
cp 0
rs 8.8452
c 0
b 0
f 0
cc 5
nc 8
nop 0
crap 30
1
<?php
2
3
namespace Ben182\AutoTranslate\Commands;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use Illuminate\Console\Command;
8
use Ben182\AutoTranslate\AutoTranslate;
9
10
class MissingCommand extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'autotrans:missing';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Translates all source translations that are not set in your target translations';
25
26
    protected $autoTranslator;
27
28
    /**
29
     * Create a new command instance.
30
     *
31
     * @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...
32
     */
33
    public function __construct(AutoTranslate $autoTranslator)
34
    {
35
        parent::__construct();
36
        $this->autoTranslator = $autoTranslator;
37
    }
38
39
    /**
40
     * Execute the console command.
41
     *
42
     * @return mixed
43
     */
44
    public function handle()
45
    {
46
        $targetLanguages = Arr::wrap(config('auto-translate.target_language'));
47
48
        $foundLanguages = count($targetLanguages);
49
        $this->line('Found '.$foundLanguages.' '.Str::plural('language', $foundLanguages).' to translate');
50
51
        $missingCount = 0;
52
        $strLen = 0;
53
        foreach ($targetLanguages as $targetLanguage) {
54
            $missing = $this->autoTranslator->getMissingTranslations($targetLanguage);
55
            $missingCount += $missing->count();
56
            $strLen += $missing->map(function ($value) {
57
                return strlen($value);
58
            })->sum();
59
            $this->line('Found '.$missing->count().' missing keys in '.$targetLanguage);
60
        }
61
62
        if ($missingCount === 0) {
63
            $this->line('0 missing keys found...aborting');
64
65
            return;
66
        }
67
68
        $this->line($strLen.' characters will be translated');
69
70
        if (! $this->confirm('Continue?', true)) {
71
            return;
72
        }
73
74
        $bar = $this->output->createProgressBar($missingCount);
75
        $bar->start();
76
77 View Code Duplication
        foreach ($targetLanguages as $targetLanguage) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
            $missing = $this->autoTranslator->getMissingTranslations($targetLanguage);
79
80
            $translated = $this->autoTranslator->translate($targetLanguage, $missing, function () use ($bar) {
81
                $bar->advance();
82
            });
83
84
            $this->autoTranslator->fillLanguageFiles($targetLanguage, $translated);
85
        }
86
87
        $bar->finish();
88
89
        $this->info("\nTranslated ".$missingCount.' missing language keys.');
90
    }
91
}
92