Completed
Push — master ( 9862b6...800bb7 )
by Aitor Riba
02:26
created

Translation   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 270
Duplicated Lines 3.7 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 0
dl 10
loc 270
rs 9.6
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setToLang() 0 6 1
A __construct() 0 16 3
A setDebug() 0 6 1
A setFromLang() 0 6 1
A setTranslator() 0 6 1
C run() 0 35 7
B mymemoryTrans() 5 49 6
C apertiumTrans() 5 58 9
A hostDown() 0 6 2
A __toString() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
                     $this->translation = "<font style='color:#00CC00;'>".$this->translation."</font>";
177
                 } else {
178
                     $this->translation = "<font style='color:orange;'>Unknoun words: ".substr($errors, 0, -2)."</font>";
0 ignored issues
show
Bug introduced by
The variable $errors does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
179
            }
180
181
             fclose($socket);
182
             return;
183
184
         } else {
185
186
             //host offline
187
             $this->hostDown();
188
         }
189
     }
190
191
    /**
192
     * Get translation from apertium API.
193
     */
194
195
    private function apertiumTrans()
196
    {
197
        // Check if it can be translated from online sources.
198
199
        $host = 'api.apertium.org';
200
        if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
201
202
            // Host online
203
204
            $urlString = urlencode($this->string);
205
            $url = "http://$host/json/translate?q=$urlString&langpair=$this->from%7C$this->to";
206
            $json = file_get_contents($url);
207
            $data = json_decode($json);
208
209
            // Checking response status
210
211
            if ($data->responseStatus != 200) {
212
                if ($this->debug === true) {
213
                    $this->translation = "<font style='color:red;'>Error ".$data->responseStatus.': '.$data->responseDetails.'</font>';
214
                }
215
216
                return;
217
            }
218
219
            $transObtained = $data->responseData->translatedText;
220
221
222
            $this->translation = ucfirst(strtolower(trim(str_replace('*', ' ', $transObtained))));
223
224
225
            // Checking debug setting to determinate how to output translation
226
227
            if ($this->debug === true) {
228
                $errors = '';
229
                $words = explode(' ', $transObtained);
230
                foreach ($words as $word) {
231
                    if ($word != '') {
232
                        if ($word[0] == '*') {
233
                            $errors = $errors.substr($word, 1).', ';
234
                        }
235
                    }
236
                }
237
238 View Code Duplication
                if ($errors == '') {
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...
239
                    $this->translation = "<font style='color:#00CC00;'>".$this->translation.'</font>';
240
                } else {
241
                    $this->translation = "<font style='color:orange;'>Unknoun words: ".substr($errors, 0, -2).'</font>';
242
                }
243
            }
244
245
            fclose($socket);
246
            return;
247
248
        } else {
249
            //host offline
250
            $this->hostDown();
251
        }
252
    }
253
254
    /*
255
     * This fuction is called when host is down, and it would set translation if debug is true
256
     *
257
     */
258
    private function hostDown() {
259
        if ($this->debug === true) {
260
            $this->translation = "<font style='color:red;'>$this->translator host is down</font>";
261
        }
262
        return;
263
    }
264
265
    /*
266
     * This fuction is called by trans() function of Fadade Laralang
267
     * It would call run() function of this class and returns the translation
268
     *
269
     */
270
    public function __toString()
271
    {
272
        $this->run();
273
274
        return $this->translation;
275
    }
276
}
277