Translator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 23
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 14 3
A __construct() 0 3 1
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