GoogleCloudTranslator::setTarget()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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