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

GoogleCloudTranslator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 38
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setSource() 0 6 1
A setTarget() 0 6 1
A translate() 0 9 1
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