| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Laravelsms\Sms\Agents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Laravelsms\Sms\Contracts\Sms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class ALiYunAgent extends Sms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     private $config = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $appKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private $appSecret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private $host = 'http://dysmsapi.aliyuncs.com/?'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private $region = "cn-hangzhou"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $singleSendUrl = 'SendSms'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $method = "GET"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function __construct($config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->transformConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 26 |  | View Code Duplication |     protected function transformConfig() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $credentials = Arr::pull($this->config, 'credentials'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->appKey = Arr::pull($credentials, 'appKey'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->appSecret = Arr::pull($credentials, 'appSecret'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->setSignName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->setTemplateId(Arr::pull($this->config, 'templateId')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     protected function percentEncode($str) | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $res = urlencode($str); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $res = preg_replace('/\+/', '%20', $res); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $res = preg_replace('/\*/', '%2A', $res); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $res = preg_replace('/%7E/', '~', $res); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         return $res; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     private function computeSignature($parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         ksort($parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $canonicalizedQueryString = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         foreach ($parameters as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $stringToSign = $this->method . '&%2F&' . $this->percentEncode(substr($canonicalizedQueryString, 1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $signature = base64_encode(hash_hmac('sha1', $stringToSign, $this->appSecret . '&', true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return $signature; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function singlesSend($mobile, $send = true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $url = $this->host; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $requestParams = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'TemplateParam' => json_encode($this->templateVar), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             'PhoneNumbers' => $mobile, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'SignName' => $this->signName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'TemplateCode' => $this->templateId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if ($send) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             return $this->curl($url, $requestParams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return $requestParams; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param array $requestParams | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return array $result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @return int $result[].code 返回0则成功,返回其它则错误 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return string $result[].msg 返回消息 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return mixed $result[].verifyCode 验证码 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     protected function curl($url, $requestParams) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         // 注意使用GMT时间 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $timezone = date_default_timezone_get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         date_default_timezone_set("GMT"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $timestamp = date('Y-m-d\TH:i:s\Z'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         date_default_timezone_set($timezone); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         // 其他请求参数公共参数 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $publicParams = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             'RegionId' => $this->region, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             'Format' => 'JSON', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             'Version' => '2017-05-25', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             'SignatureVersion' => '1.0', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             'SignatureMethod' => 'HMAC-SHA1', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             'SignatureNonce' => uniqid(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             'AccessKeyId' => $this->appKey, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             'Timestamp' => $timestamp, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             'Action' => $this->singleSendUrl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $publicParams = array_merge($requestParams, $publicParams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $signature = $this->computeSignature($publicParams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $publicParams = array_merge(['Signature' => $signature], $publicParams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $ch = curl_init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         curl_setopt($ch, CURLOPT_URL, $url . http_build_query($publicParams)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         curl_setopt($ch, CURLOPT_TIMEOUT, 10); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         curl_setopt($ch, CURLOPT_FAILONERROR, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $httpResponse = $this->httpResponse($ch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $result = $this->transformerResponse($httpResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         curl_close($ch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 123 |  | View Code Duplication |     protected function transformerResponse($httpResponse) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         if (empty($httpResponse['error'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $response = json_decode($httpResponse['jsonData'], true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             if ($response['Code'] == 'OK') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 $result = ['code' => 0, 'msg' => '发送成功', 'verifyCode' => $this->verifyCode]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 $result = ['code' => time(), 'msg' => $response['Code']]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             unset($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             $result = ['code' => time(), 'msg' => $httpResponse['error']]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |  | 
            
                        
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.