Passed
Push — master ( 351bf1...5ad45b )
by Jelly
01:07
created

Translation   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 41.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 133
ccs 14
cts 34
cp 0.4118
rs 10
wmc 12
lcom 1
cbo 3

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getTranslatedText() 0 11 2
A __construct() 0 5 1
A translate() 0 4 1
A translug() 0 4 1
A isEnglish() 0 8 2
A removeSegment() 0 4 1
A getTranslation() 0 7 2
A getTranslatedTextFromResponse() 0 4 1
A getTranslateUrl() 0 11 1
1
<?php
2
3
namespace JellyBool\Translug;
4
5
use GuzzleHttp\Client;
6
use JellyBool\Translug\Exceptions\TranslationErrorException;
7
8
/**
9
 * Class Translation.
10
 */
11
class Translation
12
{
13
    /**
14
     * Youdao new API url.
15
     *
16
     * @var string
17
     */
18
    protected $api = 'https://openapi.youdao.com/api?from=zh-CHS&to=EN&';
19
    /**
20
     * @var Client
21
     */
22
    protected $http;
23
24
    /**
25
     * @var array
26
     */
27
    protected $config = [];
28
29
    /**
30
     * Translation constructor.
31
     *
32
     * @param Client $http
33
     * @param array  $config
34
     */
35 5
    public function __construct(Client $http, array $config = [])
36
    {
37 5
        $this->http = $http;
38 5
        $this->config = $config;
39 5
    }
40
41
    /**
42
     * @param $text
43
     *
44
     * @return mixed
45
     */
46 3
    public function translate($text)
47
    {
48 3
        return $this->getTranslatedText($text);
49
    }
50
51
    /**
52
     * @param $text
53
     *
54
     * @return string
55
     */
56 1
    public function translug($text)
57
    {
58 1
        return str_slug($this->getTranslatedText($text));
59
    }
60
61
    /**
62
     * @param $text
63
     *
64
     * @return mixed
65
     */
66 4
    private function getTranslatedText($text)
67
    {
68 4
        if ($this->isEnglish($text)) {
69 4
            return $text;
70
        }
71
        $text = $this->removeSegment($text);
72
        $url = $this->getTranslateUrl($text);
73
        $response = $this->http->get($url);
74
75
        return $this->getTranslation(json_decode($response->getBody(), true));
76
    }
77
78
    /**
79
     * @param array $translateResponse
80
     *
81
     * @return mixed
82
     */
83
    private function getTranslation(array $translateResponse)
84
    {
85
        if ((int) $translateResponse['errorCode'] === 0) {
86
            return $this->getTranslatedTextFromResponse($translateResponse);
87
        }
88
        throw new TranslationErrorException('Translate error, error_code : '.$translateResponse['errorCode'].'. Refer url: http://ai.youdao.com/docs/api.s');
89
    }
90
91
    /**
92
     * @param array $translateResponse
93
     *
94
     * @return mixed
95
     */
96
    private function getTranslatedTextFromResponse(array $translateResponse)
97
    {
98
        return $translateResponse['translation'][0];
99
    }
100
101
    /**
102
     * @param $text
103
     *
104
     * @return string
105
     */
106
    private function getTranslateUrl($text)
107
    {
108
        $salt = md5(time());
109
        $query = [
110
            'sign'   => md5($this->config['appKey'].$text.$salt.$this->config['appSecret']),
111
            'appKey' => $this->config['appKey'],
112
            'salt'   => $salt,
113
        ];
114
115
        return $this->api.http_build_query($query).'&q='.urlencode($text);
116
    }
117
118
    /**
119
     * @param $text
120
     *
121
     * @return bool
122
     */
123 4
    private function isEnglish($text)
124
    {
125 4
        if (preg_match("/\p{Han}+/u", $text)) {
126
            return false;
127
        }
128
129 4
        return true;
130
    }
131
132
    /**
133
     * Remove segment #.
134
     *
135
     * @param $text
136
     *
137
     * @return mixed
138
     */
139
    private function removeSegment($text)
140
    {
141
        return str_replace('#', '', ltrim($text));
142
    }
143
}
144