Completed
Pull Request — master (#28)
by
unknown
10:53
created

GoogleCloudTranslator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Ben182\AutoTranslate\Translators;
4
5
use Ben182\AutoTranslate\Exceptions\LanguageCodeNotExist;
6
use Google\Cloud\Translate\TranslateClient;
7
8
class GoogleCloudTranslator implements TranslatorInterface
9
{
10
    protected $translator;
11
    protected $source;
12
    protected $target;
13
14
    public function __construct()
15
    {
16
        $this->translator = new TranslateClient([
17
            'key' => config('auto-translate.google_cloud_translator.api_key')
18
        ]);
19
20
    }
21
22
    public function setSource(string $source)
23
    {
24
        $this->source = $source;
25
26
        return $this;
27
    }
28
29
    public function setTarget(string $target)
30
    {
31
        $this->target = $target;
32
33
        return $this;
34
    }
35
36
    public function translate(string $string) : string
37
    {
38
        $result =  $this->translator->translate($string, [
39
            'source' => $this->source,
40
            'target' => $this->target
41
        ]);
42
43
        return $result['text'];
44
    }
45
}
46