|
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; |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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(), |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
64
|
|
|
try { |
|
65
|
|
|
$language = new GoogleTranslate(); |
|
66
|
|
|
$translation = $term->translation; |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
74
|
|
|
$this->called++; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
$term->translation = $translation; |
|
78
|
|
|
$language = null; |
|
|
|
|
|
|
79
|
|
|
$term->save(); |
|
80
|
|
|
$row++; |
|
81
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths