Total Complexity | 41 |
Total Lines | 378 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like SmsService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SmsService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class SmsService extends Service |
||
16 | { |
||
17 | /** |
||
18 | * @var |
||
19 | */ |
||
20 | private $accessKey, $secretKey, $url, $method; |
||
21 | |||
22 | /** |
||
23 | * 需要发送的的参数 |
||
24 | * @var |
||
25 | */ |
||
26 | private $param = []; |
||
27 | |||
28 | /** |
||
29 | * 响应内容 |
||
30 | * @var |
||
31 | */ |
||
32 | private $output; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $contentType = "application/json"; |
||
38 | |||
39 | /** |
||
40 | * @param string $accessKey |
||
41 | * @return $this |
||
42 | */ |
||
43 | public function accessKey(string $accessKey): self |
||
44 | { |
||
45 | $this->accessKey = $accessKey; |
||
46 | return $this; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param string $secretKey |
||
51 | * @return $this |
||
52 | */ |
||
53 | public function secretKey(string $secretKey): self |
||
54 | { |
||
55 | $this->secretKey = $secretKey; |
||
56 | return $this; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * 请求参数 |
||
61 | * @param array $param |
||
62 | * @return $this |
||
63 | */ |
||
64 | public function param(array $param): self |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * 发送短信 |
||
72 | * https://developer.qiniu.com/sms/api/5897/sms-api-send-message#1 |
||
73 | * @return $this |
||
74 | */ |
||
75 | public function message(): self |
||
76 | { |
||
77 | $this->url = "https://sms.qiniuapi.com/v1/message"; |
||
78 | $this->method = "POST"; |
||
79 | return $this; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * 发送单条短信 |
||
84 | * https://developer.qiniu.com/sms/api/5897/sms-api-send-message#2 |
||
85 | * @return $this |
||
86 | */ |
||
87 | public function messageSingle(): self |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * 发送国际/港澳台短信 |
||
96 | * https://developer.qiniu.com/sms/api/5897/sms-api-send-message#3 |
||
97 | * @return $this |
||
98 | */ |
||
99 | public function messageOversea(): self |
||
100 | { |
||
101 | $this->url = "https://sms.qiniuapi.com/v1/message/oversea"; |
||
102 | $this->method = "POST"; |
||
103 | return $this; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * 发送全文本短信(不需要传模版 ID) |
||
108 | * https://developer.qiniu.com/sms/api/5897/sms-api-send-message#3 |
||
109 | * @return $this |
||
110 | */ |
||
111 | public function messageFulltext(): self |
||
112 | { |
||
113 | $this->url = "https://sms.qiniuapi.com/v1/message/fulltext"; |
||
114 | $this->method = "POST"; |
||
115 | return $this; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * 查询短信发送记录 |
||
120 | * https://developer.qiniu.com/sms/api/5852/query-send-sms |
||
121 | * @return $this |
||
122 | */ |
||
123 | public function messages(): self |
||
124 | { |
||
125 | $this->url = "https://sms.qiniuapi.com/v1/messages"; |
||
126 | $this->method = "GET"; |
||
127 | return $this; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * 创建签名 |
||
132 | * https://developer.qiniu.com/sms/api/5844/sms-api-create-signature |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function signature(): self |
||
136 | { |
||
137 | $this->url = "https://sms.qiniuapi.com/v1/signature"; |
||
138 | $this->method = "POST"; |
||
139 | return $this; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * 查询签名 |
||
144 | * https://developer.qiniu.com/sms/api/5844/sms-api-create-signature |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function signatureQuery(): self |
||
148 | { |
||
149 | $this->url = "https://sms.qiniuapi.com/v1/signature"; |
||
150 | $this->method = "GET"; |
||
151 | $this->contentType = "application/x-www-form-urlencoded"; |
||
152 | return $this; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * 查询单个签名信息 |
||
157 | * https://developer.qiniu.com/sms/api/5970/query-a-single-signature |
||
158 | * @param $id |
||
159 | * @return $this |
||
160 | * @throws DtaException |
||
161 | */ |
||
162 | public function signatureQueryId($id): self |
||
163 | { |
||
164 | if (empty($id)) { |
||
165 | throw new DtaException('请检查id参数'); |
||
166 | } |
||
167 | $this->url = "https://sms.qiniuapi.com/v1/signature/{$id}"; |
||
168 | $this->method = "GET"; |
||
169 | return $this; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * 编辑签名 |
||
174 | * https://developer.qiniu.com/sms/api/5890/sms-api-edit-signature |
||
175 | * @param $id |
||
176 | * @return $this |
||
177 | * @throws DtaException |
||
178 | */ |
||
179 | public function signatureEditId($id): self |
||
180 | { |
||
181 | if (empty($id)) { |
||
182 | throw new DtaException('请检查id参数'); |
||
183 | } |
||
184 | $this->url = "https://sms.qiniuapi.com/v1/signature/{$id}"; |
||
185 | $this->method = "PUT"; |
||
186 | return $this; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * 删除签名 |
||
191 | * https://developer.qiniu.com/sms/api/5891/sms-api-delete-signature |
||
192 | * @param $id |
||
193 | * @return $this |
||
194 | * @throws DtaException |
||
195 | */ |
||
196 | public function signatureDelId($id): self |
||
197 | { |
||
198 | if (empty($id)) { |
||
199 | throw new DtaException('请检查id参数'); |
||
200 | } |
||
201 | $this->url = "https://sms.qiniuapi.com/v1/signature/{$id}"; |
||
202 | $this->method = "DELETE"; |
||
203 | return $this; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * 创建模板 |
||
208 | * https://developer.qiniu.com/sms/api/5893/sms-api-create-template |
||
209 | * @return $this |
||
210 | */ |
||
211 | public function template(): self |
||
212 | { |
||
213 | $this->url = "https://sms.qiniuapi.com/v1/template"; |
||
214 | $this->method = "POST"; |
||
215 | return $this; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * 查询模板 |
||
220 | * https://developer.qiniu.com/sms/api/5894/sms-api-query-template |
||
221 | * @return $this |
||
222 | */ |
||
223 | public function templateQuery(): self |
||
224 | { |
||
225 | $this->url = "https://sms.qiniuapi.com/v1/template"; |
||
226 | $this->method = "GET"; |
||
227 | return $this; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * 查询单个模版信息 |
||
232 | * https://developer.qiniu.com/sms/api/5969/query-a-single-template |
||
233 | * @param $id |
||
234 | * @return $this |
||
235 | * @throws DtaException |
||
236 | */ |
||
237 | public function templateQueryId($id): self |
||
238 | { |
||
239 | if (empty($id)) { |
||
240 | throw new DtaException('请检查id参数'); |
||
241 | } |
||
242 | $this->url = "https://sms.qiniuapi.com/v1/template/{$id}"; |
||
243 | $this->method = "GET"; |
||
244 | return $this; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * 编辑签名 |
||
249 | * https://developer.qiniu.com/sms/api/5895/sms-api-edit-template |
||
250 | * @param $id |
||
251 | * @return $this |
||
252 | * @throws DtaException |
||
253 | */ |
||
254 | public function templateEditId($id): self |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * 删除签名 |
||
266 | * https://developer.qiniu.com/sms/api/5896/sms-api-delete-template |
||
267 | * @param $id |
||
268 | * @return $this |
||
269 | * @throws DtaException |
||
270 | */ |
||
271 | public function templateDelId($id): self |
||
272 | { |
||
273 | if (empty($id)) { |
||
274 | throw new DtaException('请检查id参数'); |
||
275 | } |
||
276 | $this->url = "https://sms.qiniuapi.com/v1/template/{$id}"; |
||
277 | $this->method = "DELETE"; |
||
278 | return $this; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * 返回数组数据 |
||
283 | * @return array|mixed |
||
284 | * @throws DtaException |
||
285 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
286 | */ |
||
287 | public function toArray() |
||
288 | { |
||
289 | //首先检测是否支持curl |
||
290 | if (!extension_loaded("curl")) { |
||
291 | throw new HttpException(404, '请开启curl模块!'); |
||
292 | } |
||
293 | $this->http(); |
||
294 | // 正常 |
||
295 | if (is_array($this->output)) { |
||
296 | return $this->output; |
||
297 | } |
||
298 | if (is_object($this->output)) { |
||
299 | $this->output = $this->object2array($this->output); |
||
300 | return $this->output; |
||
301 | } |
||
302 | $this->output = json_decode($this->output, true); |
||
303 | return $this->output; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * @param $object |
||
308 | * @return array |
||
309 | */ |
||
310 | private function object2array(&$object): array |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * 网络请求 |
||
327 | * @return $this |
||
328 | * @throws DtaException |
||
329 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
330 | */ |
||
331 | private function http(): self |
||
332 | { |
||
333 | if (empty($this->accessKey)) { |
||
334 | throw new DtaException('请检查accessKey参数'); |
||
335 | } |
||
336 | if (empty($this->secretKey)) { |
||
337 | throw new DtaException('请检查secretKey参数'); |
||
338 | } |
||
339 | $headers = [ |
||
|
|||
340 | 'Authorization' => $this->authentication(), |
||
341 | 'Content-Type' => $this->contentType |
||
342 | ]; |
||
343 | |||
344 | $auth = new Auth($this->accessKey, $this->secretKey); |
||
345 | $recToken = $auth->authorizationV2($this->url, $this->method, json_encode($this->param), $this->contentType); |
||
346 | $rtcToken['Content-Type'] = $this->contentType; |
||
347 | dump($recToken); |
||
348 | dump($this->url . "?" . http_build_query($this->param)); |
||
349 | $ret = Client::get($this->url . "?" . http_build_query($this->param), $recToken); |
||
350 | dump($ret); |
||
351 | $this->output = $ret->json(); |
||
352 | return $this; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * 服务鉴权 |
||
357 | * https://developer.qiniu.com/sms/api/5842/sms-api-authentication |
||
358 | * @return string |
||
359 | */ |
||
360 | private function authentication() |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * @param $data |
||
376 | * @return string |
||
377 | */ |
||
378 | private function sign($data) |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * @param $string |
||
386 | * @return string|string[] |
||
387 | */ |
||
388 | private function encode($string) |
||
393 | } |
||
394 | } |