Test Setup Failed
Push — master ( cb0bb4...2baf3b )
by Robin
02:42
created

Translation::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace TopviewDigital\TranslationHelper\Queue;
4
5
use Illuminate\Queue\SerializesModels;
6
use Illuminate\Queue\InteractsWithQueue;
7
use Illuminate\Contracts\Bus\SelfHandling;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Bus\SelfHandling was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Illuminate\Contracts\Queue\ShouldQueue;
9
use TopviewDigital\TranslationHelper\Model\VocabTerm;
10
use Campo\UserAgent;
11
use Stichoza\GoogleTranslate\GoogleTranslate;
12
13
class Translation extends Job implements SelfHandling, ShouldQueue
0 ignored issues
show
Bug introduced by
The type TopviewDigital\TranslationHelper\Queue\Job was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
{
15
    use InteractsWithQueue, SerializesModels;
16
17
    protected $break = 0;
18
    protected $term;
19
    protected $locale;
20
    protected $translation;
21
22
    public function __construct(VocabTerm $term = null, array $locale = [])
23
    {
24
        $this->locale = array_unique(
25
            array_merge($locale, [
26
                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

26
                app()->/** @scrutinizer ignore-call */ getLocale(),
Loading history...
27
                config('app.locale'),
28
                config('app.fallback_locale'),
29
                config('app.faker_locale')
30
            ])
31
        );
32
        $this->term = $term;
33
    }
34
35
    public function handle()
36
    {
37
        if (empty($this->term)) {
38
            sweep();
39
            array_map(function ($u) {
40
                self::dispatch($u, $this->locales)->onQueue('translation');
41
            }, VocabTerm::get()->all());
42
        } else {
43
            $this->translation($this->term);
44
        }
45
    }
46
47
    private function randomUserAgent()
48
    {
49
        sleep(1);
50
        $this->called++;
51
        return [
52
            'headers' =>
53
            [
54
                'User-Agent' => UserAgent::random()
55
            ]
56
        ];
57
    }
58
59
    private function translation(VocabTerm $term)
60
    {
61
        $row = 0;
62
        while ($row < 1) {
63
            $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...
64
            try {
65
                $language = new GoogleTranslate();
66
                $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...
67
                foreach ($this->locales as $locale) {
68
                    if (!array_key_exists($locale, $term->translation)) {
69
                        $translation[$locale] = $language
70
                            ->setOptions($this->randomUserAgent())
71
                            ->setSource(config('app.locale'))
72
                            ->setTarget($locale)
73
                            ->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...
74
                        $this->called++;
75
                    }
76
                }
77
                $term->translation = $translation;
78
                $language = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $language is dead and can be removed.
Loading history...
79
                $term->save();
80
                $row++;
81
            } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type TopviewDigital\TranslationHelper\Queue\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
82
                $this->break++;
83
                $mins = rand(
84
                    floor($this->called / ($row + 1)),
85
                    floor($this->called / ($row + 1) * 2)
86
                ) * $this->break;
87
                sleep($mins * 60);
88
            }
89
        }
90
    }
91
}
92