Passed
Push — master ( d59e0a...db01b9 )
by Robin
02:53
created

Translation::randomUserAgent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TopviewDigital\TranslationHelper\Service;
4
5
use TopviewDigital\TranslationHelper\Model\VocabTerm;
6
use TopviewDigital\TranslationHelper\Interfaces\AsyncBrokerInterface;
7
8
class Translation implements AsyncBrokerInterface
9
{
10
11
    protected $locales = [];
12
    protected $words = [];
13
14
    public function __construct(VocabTerm $vocab = null)
15
    {
16
        $this->locales = [
17
            app()->getLocale(),
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

17
            app()->/** @scrutinizer ignore-call */ getLocale(),
Loading history...
18
            config('app.locale'),
19
            config('app.fallback_locale'),
20
            config('app.faker_locale'),
21
        ];
22
        $this->words = $vocab ? [$vocab->id] : $this->words;
23
    }
24
25
    public function targetLocales($locales = [])
26
    {
27
        $this->locales = array_unique(
28
            array_merge(
29
                $this->locales,
30
                $locales
31
            )
32
        );
33
        return $this;
34
    }
35
36
    public function words($words = null)
37
    {
38
39
        $words = $words ? VocabTerm::whereIn('term', (array)words)->pluck('id')->toarray() : $words;
0 ignored issues
show
Bug introduced by
The constant TopviewDigital\TranslationHelper\Service\words was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
40
        $this->words = $words ?? ($this->words ?: VocabTerm::pluck('id')->toArray());
41
        return $this;
42
    }
43
44
45
46
    public function handle()
47
    {
48
        foreach ($this->words as $word) {
49
            $this->translation(VocabTerm::find($word));
50
        }
51
    }
52
53
    private function translation(VocabTerm $word)
54
    {
55
        $translator = config('trans-helper.translation.broker');
56
        $translator = new $translator;
57
        $translated = $word->translation;
0 ignored issues
show
Bug introduced by
The property translation does not seem to exist on TopviewDigital\TranslationHelper\Model\VocabTerm. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
58
        $this->locales = array_unique($this->locales);
59
        foreach ($this->locales as $locale) {
60
            if (!array_key_exists($locale, $translated)) {
61
                $translated[$locale] = $translator->word($word->term)->targetLocale($locale)->translate();
0 ignored issues
show
Bug introduced by
The property term does not seem to exist on TopviewDigital\TranslationHelper\Model\VocabTerm. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
62
            }
63
        }
64
        $word->translation = $translated;
65
        $word->save();
66
    }
67
}
68