1 | <?php |
||
2 | |||
3 | namespace Translation\Clients; |
||
4 | |||
5 | use ErrorException; |
||
6 | use GuzzleHttp\ClientInterface; |
||
0 ignored issues
–
show
|
|||
7 | use Illuminate\Contracts\Foundation\Application; |
||
8 | use Translation\Contracts\Client; |
||
9 | |||
10 | class GoogleTranslateApi implements Client |
||
11 | { |
||
12 | /** |
||
13 | * @var ClientInterface |
||
14 | */ |
||
15 | protected $client; |
||
16 | |||
17 | /** |
||
18 | * @var \Illuminate\Config\Repository Holds the current config instance. |
||
0 ignored issues
–
show
The type
Illuminate\Config\Repository was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
19 | */ |
||
20 | protected $config; |
||
21 | |||
22 | /** |
||
23 | * @var string API endpoint |
||
24 | */ |
||
25 | protected $endpoint = 'https://www.googleapis.com/language/translate/v2'; |
||
26 | |||
27 | /** |
||
28 | * @var string source language |
||
29 | */ |
||
30 | protected $source; |
||
31 | |||
32 | /** |
||
33 | * @var string target language |
||
34 | */ |
||
35 | protected $target; |
||
36 | |||
37 | /** |
||
38 | * @param Application $app |
||
39 | * @param ClientInterface $client |
||
40 | */ |
||
41 | public function __construct(Application $app, ClientInterface $client) |
||
42 | { |
||
43 | $this->config = $app->make('config'); |
||
44 | $this->client = $client; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param $text |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | public function translate($text) |
||
53 | { |
||
54 | $response = $this->client->request( |
||
55 | 'GET', $this->endpoint, [ |
||
56 | 'query' => [ |
||
57 | 'key' => $this->getApiKey(), |
||
58 | 'format' => 'html', // text | html for source text |
||
59 | 'source' => $this->getSource(), // source language |
||
60 | 'target' => $this->getTarget(), |
||
61 | 'q' => $text, |
||
62 | ], |
||
63 | ] |
||
64 | ); |
||
65 | |||
66 | return $this->parseResponse(json_decode($response->getBody(), true)); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Extract and decode the translation response. |
||
71 | * |
||
72 | * @param array $contents |
||
73 | * |
||
74 | * @throws \Exception |
||
75 | * |
||
76 | * @return mixed |
||
77 | */ |
||
78 | protected function parseResponse($contents) |
||
79 | { |
||
80 | if (isset($contents['data'])) { |
||
81 | // get translation result array |
||
82 | $results = $contents['data']['translations']; |
||
83 | |||
84 | // return the first result in the array |
||
85 | return $results[0]['translatedText']; |
||
86 | } elseif (isset($contents['error'])) { |
||
87 | // return API error details |
||
88 | throw new ErrorException("Error: Result code {$contents['code']}: {$contents['message']}"); |
||
89 | } else { |
||
90 | // It's bad. Real bad. |
||
91 | throw new ErrorException('Error: Unknown response from API endpoint'); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Returns the api key from the configuration. |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | protected function getApiKey() |
||
101 | { |
||
102 | return $this->config->get('translation.clients.api_key'); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Returns the source language. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | protected function getSource() |
||
111 | { |
||
112 | return $this->source; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Returns the target language. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | protected function getTarget() |
||
121 | { |
||
122 | return $this->target; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function setSource($source = null) |
||
129 | { |
||
130 | return $this->source = $source; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function setTarget($target) |
||
137 | { |
||
138 | return $this->target = $target; |
||
139 | } |
||
140 | } |
||
141 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths