| Conditions | 16 |
| Paths | 18 |
| Total Lines | 113 |
| Code Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 117 | public function renewDomains() |
||
| 118 | { |
||
| 119 | // 所有请求共用一个CookieJar实例 |
||
| 120 | $this->jar = new CookieJar(); |
||
| 121 | |||
| 122 | $this->login(); |
||
| 123 | $authCookie = $this->jar->getCookieByName('WHMCSZH5eHTGhfvzP')->getValue(); |
||
| 124 | if (empty($authCookie)) { |
||
| 125 | throw new LlfException(32520002); |
||
| 126 | } |
||
| 127 | |||
| 128 | // 检查域名状态 |
||
| 129 | $response = $this->client->get(self::DOMAIN_STATUS_URL, [ |
||
| 130 | 'headers' => [ |
||
| 131 | 'Referer' => 'https://my.freenom.com/clientarea.php' |
||
| 132 | ], |
||
| 133 | 'cookies' => $this->jar |
||
| 134 | ]); |
||
| 135 | $body = (string)$response->getBody(); |
||
| 136 | |||
| 137 | // 域名数据 |
||
| 138 | if (!preg_match_all(self::DOMAIN_INFO_REGEX, $body, $domains, PREG_SET_ORDER)) { |
||
| 139 | throw new LlfException(32520003); |
||
| 140 | } |
||
| 141 | |||
| 142 | // 页面token |
||
| 143 | if (!preg_match(self::TOKEN_REGEX, $body, $matches)) { |
||
| 144 | throw new LlfException(32520004); |
||
| 145 | } |
||
| 146 | $token = $matches['token']; |
||
| 147 | |||
| 148 | // 续期 |
||
| 149 | $result = ''; |
||
| 150 | $renewed = $renewedTG = ''; // 续期成功的域名 |
||
| 151 | $notRenewed = $notRenewedTG = ''; // 记录续期出错的域名,用于邮件通知内容 |
||
| 152 | $domainInfo = $domainInfoTG = ''; // 域名状态信息,用于邮件通知内容 |
||
| 153 | foreach ($domains as $d) { |
||
| 154 | $domain = $d['domain']; |
||
| 155 | $days = intval($d['days']); |
||
| 156 | $id = $d['id']; |
||
| 157 | |||
| 158 | // 免费域名只允许在到期前14天内续期 |
||
| 159 | if ($days <= 14) { |
||
| 160 | try { |
||
| 161 | $response = $this->client->post(self::RENEW_DOMAIN_URL, [ |
||
| 162 | 'headers' => [ |
||
| 163 | 'Referer' => sprintf('https://my.freenom.com/domains.php?a=renewdomain&domain=%s', $id), |
||
| 164 | 'Content-Type' => 'application/x-www-form-urlencoded' |
||
| 165 | ], |
||
| 166 | 'form_params' => [ |
||
| 167 | 'token' => $token, |
||
| 168 | 'renewalid' => $id, |
||
| 169 | sprintf('renewalperiod[%s]', $id) => '12M', // 续期一年 |
||
| 170 | 'paymentmethod' => 'credit', // 支付方式:信用卡 |
||
| 171 | ], |
||
| 172 | 'cookies' => $this->jar |
||
| 173 | ]); |
||
| 174 | } catch (\Exception $e) { |
||
| 175 | system_log(sprintf('%s:续期请求出错:%s', $this->username, $e->getMessage())); |
||
| 176 | continue; |
||
| 177 | } |
||
| 178 | |||
| 179 | $body = (string)$response->getBody(); |
||
| 180 | sleep(1); |
||
| 181 | |||
| 182 | if (stripos($body, 'Order Confirmation') === false) { // 续期失败 |
||
| 183 | $result .= sprintf("%s续期失败\n", $domain); |
||
| 184 | $notRenewed .= sprintf('<a href="http://%s" rel="noopener" target="_blank">%s</a>', $domain, $domain); |
||
| 185 | $notRenewedTG .= sprintf('[%s](http://%s) ', $domain, $domain); |
||
| 186 | } else { |
||
| 187 | $result .= sprintf("%s续期成功\n", $domain); |
||
| 188 | $renewed .= sprintf('<a href="http://%s" rel="noopener" target="_blank">%s</a>', $domain, $domain); |
||
| 189 | $renewedTG .= sprintf('[%s](http://%s) ', $domain, $domain); |
||
| 190 | continue; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | $domainInfo .= sprintf('<a href="http://%s" rel="noopener" target="_blank">%s</a>还有<span style="font-weight: bold; font-size: 16px;">%d</span>天到期,', $domain, $domain, $days); |
||
| 195 | $domainInfoTG .= sprintf('[%s](http://%s)还有*%d*天到期,', $domain, $domain, $days); |
||
| 196 | } |
||
| 197 | $domainInfoTG .= "更多信息可以参考[Freenom官网](https://my.freenom.com/domains.php?a=renewals)哦~\n\n(如果你不想每次执行都收到推送,请将config.php中noticeFreq的值设为0,使程序只在有续期操作时才推送)"; |
||
| 198 | |||
| 199 | if ($notRenewed || $renewed) { |
||
| 200 | Mail::send( |
||
| 201 | '主人,我刚刚帮你续期域名啦~', |
||
| 202 | [ |
||
| 203 | $this->username, |
||
| 204 | $renewed ? sprintf('续期成功:%s<br>', $renewed) : '', |
||
| 205 | $notRenewed ? sprintf('续期出错:%s<br>', $notRenewed) : '', |
||
| 206 | $domainInfo ?: '哦豁,没看到其它域名。' |
||
| 207 | ] |
||
| 208 | ); |
||
| 209 | TelegramBot::send(sprintf( |
||
| 210 | "主人,我刚刚帮你续期域名啦~\n\n%s%s\n另外,%s", |
||
| 211 | $renewedTG ? sprintf("续期成功:%s\n", $renewedTG) : '', |
||
| 212 | $notRenewedTG ? sprintf("续期失败:%s\n", $notRenewedTG) : '', |
||
| 213 | $domainInfoTG |
||
| 214 | )); |
||
| 215 | system_log(sprintf("%s:续期结果如下:\n%s", $this->username, $result)); |
||
| 216 | } else { |
||
| 217 | if (config('noticeFreq') === 1) { |
||
| 218 | Mail::send( |
||
| 219 | '报告,今天没有域名需要续期', |
||
| 220 | [ |
||
| 221 | $this->username, |
||
| 222 | $domainInfo |
||
| 223 | ], |
||
| 224 | '', |
||
| 225 | 'notice' |
||
| 226 | ); |
||
| 227 | TelegramBot::send("报告,今天没有域名需要续期,所有域名情况如下:\n\n" . $domainInfoTG); |
||
| 228 | } |
||
| 229 | system_log(sprintf('%s:<green>执行成功,今次没有需要续期的域名。</green>', $this->username)); |
||
| 230 | } |
||
| 346 |