Completed
Push — master ( 56aaf5...cbecf1 )
by Aitor Riba
01:54
created

MymemoryTrans::main()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 5
nop 0
1
<?php
2
3
namespace Aitor24\Laralang\Builder;
4
5
class MymemoryTrans extends Translation
6
{
7
8
     /**
9
      * Get translation from mymemory API.
10
      */
11
     public function main()
12
     {
13
         $host = 'api.mymemory.translated.net';
14
15
         // Check if host is online.
16
         if ($this->checkHost($host)) {
17
18
             // Host online
19
             $urlString = urlencode($this->string);
20
             $url = "http://$host/get?q=$urlString&langpair=$this->from%7C$this->to";
21
             $json = file_get_contents($url);
22
             $data = json_decode($json);
23
24
             // Checking response status
25
             if ($data->responseStatus != 200) {
26
                 if ($this->debug == true) {
27
                     $details = $data->responseDetails;
28
                     if ($data->responseStatus == 403) {
29
                         $details = ($data->responseDetails);
30
                     }
31
                     $this->translation = "<font style='color:red;'>Error ".$data->responseStatus.': '.$details.'</font>';
32
                 }
33
34
                 return;
35
             }
36
37
38
             $transObtained = $data->responseData->translatedText;
39
40
             $this->translation = ucfirst(strtolower(trim($transObtained)));
41
42
             $this->checkSave();
43
44
             return;
45
         }
46
     }
47
48
}
49