GoogleTranslator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 89
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
B translate() 0 27 4
A getUrl() 0 10 1
A format() 0 13 2
1
<?php
2
3
namespace Happyr\AutoFallbackTranslationBundle\Service;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
/**
8
 * @author Tobias Nyholm <[email protected]>
9
 */
10
class GoogleTranslator extends AbstractTranslator implements TranslatorService
11
{
12
    /**
13
     * @var string
14
     */
15
    private $key;
16
17
    /**
18
     * @param string $key
19
     */
20
    public function __construct($key)
21
    {
22
        if (empty($key)) {
23
            throw new \InvalidArgumentException('Google key can not be empty');
24
        }
25
26
        $this->key = $key;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function translate($string, $from, $to)
33
    {
34
        $url = $this->getUrl($string, $from, $to, $this->key);
35
        $request = $this->getMessageFactory()->createRequest('GET', $url);
36
37
        /** @var ResponseInterface $response */
38
        $response = $this->getHttpClient()->sendRequest($request);
39
40
        if ($response->getStatusCode() !== 200) {
41
            $this->log('error', 'Fallback Translator: Did not get a 200 response for GET '.$this->getUrl($string, $from, $to, '[key]'));
42
43
            return $string;
44
        }
45
46
        $responseBody = $response->getBody()->__toString();
47
        $data = json_decode($responseBody, true);
48
49
        if (!is_array($data)) {
50
            $this->log('error', sprintf("Fallback Translator: Unexpected response for GET %s. \n\n %s", $this->getUrl($string, $from, $to, '[key]'), $responseBody));
51
52
            return $string;
53
        }
54
55
        foreach ($data['data']['translations'] as $translaton) {
56
            return $this->format($string, $translaton['translatedText']);
57
        }
58
    }
59
60
    /**
61
     * @param string $string
62
     * @param string $from
63
     * @param string $to
64
     * @param string $key
65
     *
66
     * @return string
67
     */
68
    private function getUrl($string, $from, $to, $key)
69
    {
70
        return sprintf(
71
            'https://www.googleapis.com/language/translate/v2?key=%s&source=%s&target=%s&q=%s',
72
            $key,
73
            $from,
74
            $to,
75
            urlencode($string)
76
        );
77
    }
78
79
    /**
80
     * @param $original
81
     * @param $translaton
82
     *
83
     * @return string
84
     */
85
    private function format($original, $translationHtmlEncoded)
86
    {
87
        $translation = htmlspecialchars_decode($translationHtmlEncoded);
88
89
        // if capitalized, make sure we also capitalize.
90
        $firstChar = mb_substr($original, 0, 1);
91
        if (mb_strtoupper($firstChar) === $firstChar) {
92
            $first = mb_strtoupper(mb_substr($translation, 0, 1));
93
            $translation = $first.mb_substr($translation, 1);
94
        }
95
96
        return $translation;
97
    }
98
}
99