Passed
Push — master ( cad205...d59e0a )
by Robin
03:42
created

Translation   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 52
c 0
b 0
f 0
dl 0
loc 85
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A randomUserAgent() 0 8 1
A __construct() 0 11 1
B translation() 0 39 7
A handle() 0 9 3
1
<?php
2
3
namespace TopviewDigital\TranslationHelper\Service;
4
5
use Campo\UserAgent;
6
use Illuminate\Bus\Queueable;
7
use Illuminate\Contracts\Queue\ShouldQueue;
8
use Illuminate\Foundation\Bus\Dispatchable;
9
use Illuminate\Queue\InteractsWithQueue;
10
use Illuminate\Queue\SerializesModels;
11
use Stichoza\GoogleTranslate\GoogleTranslate;
12
use TopviewDigital\TranslationHelper\Model\VocabTerm;
13
14
class Translation implements ShouldQueue
15
{
16
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by TopviewDigital\Translati...per\Service\Translation: $id, $class, $relations
Loading history...
17
18
    protected $break = 0;
19
    protected $term;
20
    protected $locales;
21
    protected $translation;
22
23
    public function __construct(VocabTerm $term = null, array $locales = [])
24
    {
25
        $this->locales = array_unique(
26
            array_merge($locales, [
27
                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

27
                app()->/** @scrutinizer ignore-call */ getLocale(),
Loading history...
28
                config('app.locale'),
29
                config('app.fallback_locale'),
30
                config('app.faker_locale'),
31
            ])
32
        );
33
        $this->term = $term;
34
    }
35
36
    public function handle()
37
    {
38
        if (empty($this->term)) {
39
            sweep();
40
            foreach (VocabTerm::get()->all() as $u) {
41
                $this->translation($u, $this->locales);
0 ignored issues
show
Unused Code introduced by
The call to TopviewDigital\Translati...nslation::translation() has too many arguments starting with $this->locales. ( Ignorable by Annotation )

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

41
                $this->/** @scrutinizer ignore-call */ 
42
                       translation($u, $this->locales);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
42
            }
43
        } else {
44
            $this->translation($this->term);
45
        }
46
    }
47
48
    private function randomUserAgent()
49
    {
50
        sleep(1);
51
        $this->called++;
52
53
        return [
54
            'headers' => [
55
                'User-Agent' => UserAgent::random(),
56
            ],
57
        ];
58
    }
59
60
    private function translation(VocabTerm $term)
61
    {
62
        $row = 0;
63
        while ($row < 1) {
64
            $this->called = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property called does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
65
66
            try {
67
                $language = new GoogleTranslate();
68
                // $language->setOptions($this->randomUserAgent())
69
                //     ->setSource()
70
                //     ->setTarget(config('app.locale'))
71
                //     ->translate($term->term);
72
                // $default_locale = $language->setOptions($this->randomUserAgent())->getLastDetectedSource();
73
                $translation = empty($term->translation) ? [] : $term->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...
74
                foreach ($this->locales as $locale) {
75
                    if (!array_key_exists($locale, $term->translation)) {
76
                        $translation[$locale] = $language
77
                            ->setOptions($this->randomUserAgent())
78
                            ->setSource()
79
                            ->setTarget($locale)
80
                            ->translate($term->term);
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...
81
                        $this->called++;
82
                    }
83
                }
84
                $locale = $language->setOptions($this->randomUserAgent())->getLastDetectedSource();
85
                if (!array_key_exists($locale, $term->translation)) {
86
                    $translation[$locale] = $term->term;
87
                }
88
                $term->translation = $translation;
89
                $language = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $language is dead and can be removed.
Loading history...
90
                $term->save();
91
                $row++;
92
            } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type TopviewDigital\TranslationHelper\Service\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
93
                $this->break++;
94
                $mins = rand(
95
                    floor($this->called / ($row + 1)),
96
                    floor($this->called / ($row + 1) * 2)
97
                ) * $this->break;
98
                sleep($mins * 60);
99
            }
100
        }
101
    }
102
}
103