ChineseTranslate   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 37
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B translate() 0 25 1
1
<?php
2
3
namespace Jiemoon\ChineseSlug;
4
5
class ChineseTranslate
6
{
7
8
    private $config;
9
10 2
    public function __construct($config)
11
    {
12 2
        $this->config = $config;
13 2
    }
14
15
16 2
    public function translate($string, $server_name = 'baidu')
17
    {
18
        // TODO
19
        // server_name baidu \ youdao \ google
20 2
        $baidu = $this->config[$server_name];
21 2
        $salt = rand();
22 2
        $sign = md5($baidu['api_id'] . $string . $salt . $baidu['api_secret']);
23 2
        $client = new \GuzzleHttp\Client();
24 2
        $res = $client->request('GET', 'http://api.fanyi.baidu.com/api/trans/vip/translate', [
25
            'query' => [
26 2
                'q' => $string,
27 2
                'from' => 'zh',
28 2
                'to' => 'en',
29 2
                'appid' => $baidu['api_id'],
30 2
                'salt' => $salt,
31 2
                'sign' => $sign
32
            ]
33
        ]);
34
35 2
        $body = json_decode($res->getBody(), true);
36 2
        $trans_result = $body['trans_result'][0]['dst'];
37 2
        $slug = strtolower($trans_result);
38
39 2
        return $slug;
40
    }
41
}
42