| Conditions | 17 |
| Paths | 19 |
| Total Lines | 119 |
| Code Lines | 79 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 123 | public function renewDomains() |
||
| 124 | { |
||
| 125 | // 所有请求共用一个CookieJar实例 |
||
| 126 | $this->jar = new CookieJar(); |
||
| 127 | |||
| 128 | $this->login(); |
||
| 129 | $authCookie = $this->jar->getCookieByName('WHMCSZH5eHTGhfvzP')->getValue(); |
||
| 130 | if (empty($authCookie)) { |
||
| 131 | throw new LlfException(34520002); |
||
| 132 | } |
||
| 133 | |||
| 134 | // 检查域名状态 |
||
| 135 | $response = $this->client->get(self::DOMAIN_STATUS_URL, [ |
||
| 136 | 'headers' => [ |
||
| 137 | 'Referer' => 'https://my.freenom.com/clientarea.php' |
||
| 138 | ], |
||
| 139 | 'cookies' => $this->jar |
||
| 140 | ]); |
||
| 141 | $body = (string)$response->getBody(); |
||
| 142 | |||
| 143 | if (!preg_match(self::LOGIN_STATUS_REGEX, $body)) { |
||
| 144 | throw new LlfException(34520009); |
||
| 145 | } |
||
| 146 | |||
| 147 | // 域名数据 |
||
| 148 | if (!preg_match_all(self::DOMAIN_INFO_REGEX, $body, $domains, PREG_SET_ORDER)) { |
||
| 149 | throw new LlfException(34520003); |
||
| 150 | } |
||
| 151 | |||
| 152 | // 页面token |
||
| 153 | if (!preg_match(self::TOKEN_REGEX, $body, $matches)) { |
||
| 154 | throw new LlfException(34520004); |
||
| 155 | } |
||
| 156 | $token = $matches['token']; |
||
| 157 | |||
| 158 | // 续期 |
||
| 159 | $result = ''; |
||
| 160 | $renewed = $renewedTG = ''; // 续期成功的域名 |
||
| 161 | $notRenewed = $notRenewedTG = ''; // 记录续期出错的域名,用于邮件通知内容 |
||
| 162 | $domainInfo = $domainInfoTG = ''; // 域名状态信息,用于邮件通知内容 |
||
| 163 | foreach ($domains as $d) { |
||
| 164 | $domain = $d['domain']; |
||
| 165 | $days = intval($d['days']); |
||
| 166 | $id = $d['id']; |
||
| 167 | |||
| 168 | // 免费域名只允许在到期前14天内续期 |
||
| 169 | if ($days <= 14) { |
||
| 170 | try { |
||
| 171 | $response = $this->client->post(self::RENEW_DOMAIN_URL, [ |
||
| 172 | 'headers' => [ |
||
| 173 | 'Referer' => sprintf('https://my.freenom.com/domains.php?a=renewdomain&domain=%s', $id), |
||
| 174 | 'Content-Type' => 'application/x-www-form-urlencoded' |
||
| 175 | ], |
||
| 176 | 'form_params' => [ |
||
| 177 | 'token' => $token, |
||
| 178 | 'renewalid' => $id, |
||
| 179 | sprintf('renewalperiod[%s]', $id) => '12M', // 续期一年 |
||
| 180 | 'paymentmethod' => 'credit', // 支付方式:信用卡 |
||
| 181 | ], |
||
| 182 | 'cookies' => $this->jar |
||
| 183 | ]); |
||
| 184 | } catch (\Exception $e) { |
||
| 185 | system_log(sprintf('%s:续期请求出错:%s', $this->username, $e->getMessage())); |
||
| 186 | continue; |
||
| 187 | } |
||
| 188 | |||
| 189 | $body = (string)$response->getBody(); |
||
| 190 | sleep(1); |
||
| 191 | |||
| 192 | if (stripos($body, 'Order Confirmation') === false) { // 续期失败 |
||
| 193 | $result .= sprintf("%s续期失败\n", $domain); |
||
| 194 | $notRenewed .= sprintf('<a href="http://%s" rel="noopener" target="_blank">%s</a>', $domain, $domain); |
||
| 195 | $notRenewedTG .= sprintf('[%s](http://%s) ', $domain, $domain); |
||
| 196 | } else { |
||
| 197 | $result .= sprintf("%s续期成功\n", $domain); |
||
| 198 | $renewed .= sprintf('<a href="http://%s" rel="noopener" target="_blank">%s</a>', $domain, $domain); |
||
| 199 | $renewedTG .= sprintf('[%s](http://%s) ', $domain, $domain); |
||
| 200 | continue; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | $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); |
||
| 205 | $domainInfoTG .= sprintf('[%s](http://%s)还有*%d*天到期,', $domain, $domain, $days); |
||
| 206 | } |
||
| 207 | $domainInfoTG .= "更多信息可以参考[Freenom官网](https://my.freenom.com/domains.php?a=renewals)哦~\n\n(如果你不想每次执行都收到推送,请将 .env 中 NOTICE_FREQ 的值设为0,使程序只在有续期操作时才推送)"; |
||
| 208 | |||
| 209 | if ($notRenewed || $renewed) { |
||
| 210 | Mail::send( |
||
| 211 | '主人,我刚刚帮你续期域名啦~', |
||
| 212 | [ |
||
| 213 | $this->username, |
||
| 214 | $renewed ? sprintf('续期成功:%s<br>', $renewed) : '', |
||
| 215 | $notRenewed ? sprintf('续期出错:%s<br>', $notRenewed) : '', |
||
| 216 | $domainInfo ?: '哦豁,没看到其它域名。' |
||
| 217 | ] |
||
| 218 | ); |
||
| 219 | TelegramBot::send(sprintf( |
||
| 220 | "主人,我刚刚帮你续期域名啦~\n\n%s%s\n另外,%s", |
||
| 221 | $renewedTG ? sprintf("续期成功:%s\n", $renewedTG) : '', |
||
| 222 | $notRenewedTG ? sprintf("续期失败:%s\n", $notRenewedTG) : '', |
||
| 223 | $domainInfoTG |
||
| 224 | )); |
||
| 225 | system_log(sprintf("%s:续期结果如下:\n%s", $this->username, $result)); |
||
| 226 | } else { |
||
| 227 | if (config('noticeFreq') == 1) { |
||
| 228 | Mail::send( |
||
| 229 | '报告,今天没有域名需要续期', |
||
| 230 | [ |
||
| 231 | $this->username, |
||
| 232 | $domainInfo |
||
| 233 | ], |
||
| 234 | '', |
||
| 235 | 'notice' |
||
| 236 | ); |
||
| 237 | TelegramBot::send("报告,今天没有域名需要续期,所有域名情况如下:\n\n" . $domainInfoTG); |
||
| 238 | } else { |
||
| 239 | system_log('当前通知频率为「仅当有续期操作时」,故本次不会推送通知'); |
||
| 240 | } |
||
| 241 | system_log(sprintf('%s:<green>执行成功,今次没有需要续期的域名</green>', $this->username)); |
||
| 242 | } |
||
| 369 | } |