Completed
Push — master ( f56d28...9862b6 )
by Aitor Riba
02:11
created

Translation::hostDown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Aitor24\Laralang;
4
5
use App;
6
7
class Translation
8
{
9
    /**
10
     * Setup public vars.
11
     */
12
    public $translator;
13
    public $translation;
14
    public $string;
15
    public $debug;
16
    public $from;
17
    public $to;
18
19
    /**
20
     * Setup default values.
21
     *
22
     * @param string $string
23
     */
24
    public function __construct($string)
25
    {
26
        $this->translator = config('laralang.default.translator');
27
        $this->debug = config('laralang.default.debug');
28
        $this->from = config('laralang.default.from_lang');
29
        $this->to = config('laralang.default.to_lang');
30
        $this->string = $string;
31
        $this->translation = $string;
32
33
34
        // Checking whether from_lang or to_lang are set as app_locale.
35
36
        if ($this->from == 'app_locale') {$this->from = App::getLocale();}
37
38
        if ($this->to == 'app_locale') {$this->to = App::getLocale();}
39
    }
40
41
42
    /**
43
     * Setup debug value
44
     *
45
     * @param boolean $debug
46
     */
47
48
    public function setDebug($debug)
49
    {
50
        $this->debug = $debug;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Setup fromLang value
57
     *
58
     * @param string $lang
59
     */
60
61
    public function setFromLang($lang)
62
    {
63
        $this->from = $lang;
64
65
        return $this;
66
    }
67
68
    /**
69
     * Setup tolang value
70
     *
71
     * @param string $lang
72
     */
73
74
    public function setToLang($lang)
75
    {
76
        $this->to = $lang;
77
78
        return $this;
79
    }
80
81
82
    /**
83
     * Setup translator
84
     *
85
     * @param string $translator
86
     */
87
88
    public function setTranslator($translator)
89
    {
90
        $this->translator = $translator;
91
92
        return $this;
93
    }
94
95
    /**
96
     * Main function of the class
97
     *
98
     * Check what translator must select
99
     *
100
     */
101
102
    private function run()
103
    {
104
105
        // Return the value if the language is the same.
106
107
        if ($this->from == $this->to) {
108
            if ($this->debug === true) {
109
                $this->translation = "<font style='color:orange;'>Same in <> out languge</font>";
110
            }
111
112
            return;
113
        }
114
115
116
        $available_transoltors = ['apertium', 'mymemory'];
117
118
        // Checks available translators.
119
120
        if (in_array($this->translator, $available_transoltors) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
121
            if ($this->debug === true) {
122
                $this->translation = "<font style='color:red;'>Not suported translator: ".$this->translator."</font>";
123
            }
124
125
            return;
126
        }
127
128
        if ($this->translator == 'apertium') {
129
            return $this->apertiumTrans();
130
        }
131
132
        elseif ($this->translator == 'mymemory') {
133
            return $this->mymemoryTrans();
134
        }
135
136
    }
137
138
    /**
139
     * Get translation from mymemory API.
140
     */
141
     private function mymemoryTrans()
142
     {
143
         // Check if it can be translated from online sources.
144
145
         $host = 'api.mymemory.translated.net';
146
         if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
147
148
             // Host online
149
             $urlString = urlencode($this->string);
150
             $url = "http://$host/get?q=$urlString&langpair=$this->from%7C$this->to";
151
             $json = file_get_contents($url);
152
             $data = json_decode($json);
153
154
             // Checking response status
155
156
             if ($data->responseStatus != 200) {
157
                 if ($this->debug == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
158
                     $details = $data->responseDetails;
159
                     if ($data->responseStatus == 403) {
160
                         $details =($data->responseDetails);
161
                     }
162
                     $this->translation = "<font style='color:red;'>Error ".$data->responseStatus.": ".$details."</font>";
163
                 }
164
165
                 return;
166
             }
167
168
             $transObtained = $data->responseData->translatedText;
169
170
             $this->translation = ucfirst(strtolower(trim($transObtained)));
171
172
173
             // Checking debug setting to determinate how to output translation
174
175 View Code Duplication
             if ($this->debug === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
176
                 $errors = '';
177
                 $words = explode(' ', $transObtained);
178
                 foreach ($words as $word) {
179
                     if ($word != '') {
180
                         if ($word[0] == '*') {
181
                             $errors = $errors.substr($word, 1).', ';
182
                         }
183
                     }
184
                 }
185
186
                 if ($errors == '') {
187
                     $this->translation = "<font style='color:#00CC00;'>".$this->translation."</font>";
188
                 } else {
189
                     $this->translation = "<font style='color:orange;'>Unknoun words: ".substr($errors, 0, -2)."</font>";
190
                 }
191
             }
192
193
             fclose($socket);
194
             return;
195
196
         } else {
197
198
             //host offline
199
             $this->hostDown();
200
         }
201
     }
202
203
    /**
204
     * Get translation from apertium API.
205
     */
206
207
    private function apertiumTrans()
208
    {
209
        // Check if it can be translated from online sources.
210
211
        $host = 'api.apertium.org';
212
        if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
213
214
            // Host online
215
216
            $urlString = urlencode($this->string);
217
            $url = "http://$host/json/translate?q=$urlString&langpair=$this->from%7C$this->to";
218
            $json = file_get_contents($url);
219
            $data = json_decode($json);
220
221
            // Checking response status
222
223
            if ($data->responseStatus != 200) {
224
                if ($this->debug === true) {
225
                    $this->translation = "<font style='color:red;'>Error ".$data->responseStatus.': '.$data->responseDetails.'</font>';
226
                }
227
228
                return;
229
            }
230
231
            $transObtained = $data->responseData->translatedText;
232
233
234
            $this->translation = ucfirst(strtolower(trim(str_replace('*', ' ', $transObtained))));
235
236
237
            // Checking debug setting to determinate how to output translation
238
239 View Code Duplication
            if ($this->debug === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
240
                $errors = '';
241
                $words = explode(' ', $transObtained);
242
                foreach ($words as $word) {
243
                    if ($word != '') {
244
                        if ($word[0] == '*') {
245
                            $errors = $errors.substr($word, 1).', ';
246
                        }
247
                    }
248
                }
249
250
                if ($errors == '') {
251
                    $this->translation = "<font style='color:#00CC00;'>".$this->translation.'</font>';
252
                } else {
253
                    $this->translation = "<font style='color:orange;'>Unknoun words: ".substr($errors, 0, -2).'</font>';
254
                }
255
            }
256
257
            fclose($socket);
258
            return;
259
260
        } else {
261
            //host offline
262
            $this->hostDown();
263
        }
264
    }
265
266
    /*
267
     * This fuction is called when host is down, and it would set translation if debug is true
268
     *
269
     */
270
    private function hostDown() {
271
        if ($this->debug === true) {
272
            $this->translation = "<font style='color:red;'>$this->translator host is down</font>";
273
        }
274
        return;
275
    }
276
277
    /*
278
     * This fuction is called by trans() function of Fadade Laralang
279
     * It would call run() function of this class and returns the translation
280
     *
281
     */
282
    public function __toString()
283
    {
284
        $this->run();
285
286
        return $this->translation;
287
    }
288
}
289