Conditions | 6 |
Paths | 14 |
Total Lines | 35 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Tests | 20 |
CRAP Score | 6 |
Changes | 0 |
1 | <?php |
||
20 | 4 | public function modifyAll(TokenCollection $tokenCollection): TokenCollection |
|
21 | { |
||
22 | 4 | $oldKeys = []; |
|
23 | 4 | foreach ($tokenCollection->getTokens() as $token) { |
|
24 | 2 | $oldKeys[] = $token->getTranslationKey(); |
|
25 | } |
||
26 | |||
27 | 4 | if (empty($oldKeys)) { |
|
28 | 2 | return $tokenCollection; |
|
29 | } |
||
30 | |||
31 | 2 | $guzzleClient = new Client(); |
|
32 | $options = [ |
||
33 | 'form_params' => [ |
||
34 | 2 | 'text' => implode(' | ', $oldKeys), |
|
35 | 2 | 'from' => $this->from, |
|
36 | 2 | 'to' => $this->to, |
|
37 | ] |
||
38 | ]; |
||
39 | 2 | $request = $guzzleClient->request('POST', 'https://www.bing.com/ttranslate', $options); |
|
40 | |||
41 | 2 | $newKeys = []; |
|
42 | 2 | $response = json_decode((string) $request->getBody(), true); |
|
43 | 2 | if ($response['statusCode'] === 200) { |
|
44 | 2 | $newKeys = explode(' | ', $response['translationResponse']); |
|
45 | } |
||
46 | 2 | $oldToNewKeys = array_combine($oldKeys, $newKeys); |
|
47 | |||
48 | 2 | foreach ($tokenCollection->getTokens() as $token) { |
|
49 | 2 | if (isset($oldToNewKeys[$token->getTranslationKey()])) { |
|
50 | 2 | $token->changeTranslationKey($oldToNewKeys[$token->getTranslationKey()]); |
|
51 | } |
||
52 | } |
||
53 | |||
54 | 2 | return $tokenCollection; |
|
55 | } |
||
57 |