GoogleTranslator::sourceLocale()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TopviewDigital\TranslationHelper\Service;
4
5
use Campo\UserAgent;
6
use Stichoza\GoogleTranslate\GoogleTranslate;
7
use TopviewDigital\TranslationHelper\Interfaces\TranslatorInterface;
8
9
class GoogleTranslator implements TranslatorInterface
10
{
11
    protected $break = 0;
12
    protected $called = 0;
13
    protected $word;
14
    protected $source_locale = null;
15
    protected $target_locale;
16
17
    public function __construct()
18
    {
19
        $this->target_locale = config('app.locale');
20
    }
21
22
    public function word(string $word)
23
    {
24
        $this->word = $word;
25
26
        return $this;
27
    }
28
29
    public function targetLocale(string $target_locale)
30
    {
31
        $this->target_locale = $target_locale;
32
33
        return $this;
34
    }
35
36
    public function sourceLocale(string $source_locale)
37
    {
38
        $this->source_locale = $source_locale;
39
40
        return $this;
41
    }
42
43
    private function randomUserAgent()
44
    {
45
        sleep(1);
46
        $this->called++;
47
48
        return [
49
            'headers' => [
50
                'User-Agent' => UserAgent::random(),
51
            ],
52
        ];
53
    }
54
55
    public function translate()
56
    {
57
        $translated = '';
58
        $translator = new GoogleTranslate();
59
        while (empty($translated) && !empty($this->word)) {
60
            $this->called = 0;
61
62
            try {
63
                $translated = is_null($this->source_locale)
64
                    ? $translator
65
                    ->setOptions($this->randomUserAgent())
66
                    ->setSource()
67
                    ->setTarget($this->target_locale)
68
                    ->translate($this->word)
69
                    : $translator
70
                    ->setOptions($this->randomUserAgent())
71
                    ->setSource($this->source_locale)
72
                    ->setTarget($this->target_locale)
73
                    ->translate($this->word);
74
            } catch (\Exception $e) {
75
                $this->break++;
76
                $mins = rand(
77
                    floor($this->called),
0 ignored issues
show
Bug introduced by
floor($this->called) of type double is incompatible with the type integer expected by parameter $min of rand(). ( Ignorable by Annotation )

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

77
                    /** @scrutinizer ignore-type */ floor($this->called),
Loading history...
78
                    floor($this->called * rand(2, 5))
0 ignored issues
show
Bug introduced by
floor($this->called * rand(2, 5)) of type double is incompatible with the type integer expected by parameter $max of rand(). ( Ignorable by Annotation )

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

78
                    /** @scrutinizer ignore-type */ floor($this->called * rand(2, 5))
Loading history...
79
                ) * $this->break;
80
                sleep($mins * 60);
81
            }
82
        }
83
84
        return $translated;
85
    }
86
}
87