Translator::process()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 2
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Translator;
4
5
use Google\Cloud\Translate\TranslateClient;
6
7
class Translator
8
{
9
    private $client;
10
11
    public function __construct($key)
12
    {
13
        $this->client = new TranslateClient(['key' => $key]);
14
    }
15
16
    public function process($text, $targetLanguage)
17
    {
18
        $result = $this->client->translate($text,
19
            ['target' => $targetLanguage]
20
        );
21
22
        if (isset($result['text']) && strlen($result['text']) > 0) {
23
            $output = preg_replace("/&amp;/", "&", $result['text']);
24
            $output = preg_replace("/&#39;/", "'", $output);
25
26
            return $output;
27
        }
28
29
        return $text;
30
    }
31
}
32