Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 33 | class API extends AbstractAPI |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Merchant instance. |
||
| 37 | * |
||
| 38 | * @var Merchant |
||
| 39 | */ |
||
| 40 | protected $merchant; |
||
| 41 | |||
| 42 | // api |
||
| 43 | const API_SEND = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack'; |
||
| 44 | const API_SEND_GROUP = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack'; |
||
| 45 | const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo'; |
||
| 46 | const API_PREPARE = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder'; |
||
| 47 | |||
| 48 | // LuckyMoney type |
||
| 49 | const TYPE_NORMAL = 'NORMAL'; |
||
| 50 | const TYPE_GROUP = 'GROUP'; |
||
| 51 | |||
| 52 | // Risk control type. |
||
| 53 | const RISK_NORMAL = 'NORMAL'; |
||
| 54 | const RISK_IGN_FREQ_LMT = 'IGN_FREQ_LMT'; |
||
| 55 | const RISK_IGN_DAY_LMT = 'IGN_DAY_LMT'; |
||
| 56 | const RISK_IGN_FREQ_DAY_LMT = 'IGN_FREQ_DAY_LMT'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * API constructor. |
||
| 60 | * |
||
| 61 | * @param \EasyWeChat\Payment\Merchant $merchant |
||
| 62 | */ |
||
| 63 | 7 | public function __construct(Merchant $merchant) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Prepare luckymoney. |
||
| 70 | * |
||
| 71 | * @param array $params |
||
| 72 | * |
||
| 73 | * @return \EasyWeChat\Support\Collection |
||
| 74 | */ |
||
| 75 | 1 | public function prepare(array $params) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Query luckymoney. |
||
| 90 | * |
||
| 91 | * @param string $mchBillNo |
||
| 92 | * |
||
| 93 | * @return \EasyWeChat\Support\Collection |
||
| 94 | */ |
||
| 95 | 1 | public function query($mchBillNo) |
|
| 96 | { |
||
| 97 | $params = [ |
||
| 98 | 1 | 'appid' => $this->merchant->app_id, |
|
| 99 | 1 | 'mch_billno' => $mchBillNo, |
|
| 100 | 1 | 'bill_type' => 'MCHT', |
|
| 101 | ]; |
||
| 102 | |||
| 103 | 1 | return $this->request(self::API_QUERY, $params); |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Send LuckyMoney. |
||
| 108 | * |
||
| 109 | * @param array $params |
||
| 110 | * @param string $type |
||
| 111 | * |
||
| 112 | * @return \EasyWeChat\Support\Collection |
||
| 113 | */ |
||
| 114 | 3 | public function send(array $params, $type = self::TYPE_NORMAL) |
|
| 115 | { |
||
| 116 | 3 | $api = ($type === self::TYPE_NORMAL) ? self::API_SEND : self::API_SEND_GROUP; |
|
| 117 | |||
| 118 | 3 | $params['wxappid'] = $this->merchant->app_id; |
|
| 119 | //如果类型为分裂红则去掉client_ip参数,否则签名会出错 |
||
| 120 | 3 | if ($type === self::TYPE_GROUP) { |
|
| 121 | 2 | unset($params['client_ip']); |
|
| 122 | } |
||
| 123 | |||
| 124 | 3 | return $this->request($api, $params); |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Send normal LuckyMoney. |
||
| 129 | * |
||
| 130 | * @param array $params |
||
| 131 | * |
||
| 132 | * @return \EasyWeChat\Support\Collection |
||
| 133 | */ |
||
| 134 | 1 | public function sendNormal($params) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Send group luckymoney. |
||
| 144 | * |
||
| 145 | * @param array $params |
||
| 146 | * |
||
| 147 | * @return \EasyWeChat\Support\Collection |
||
| 148 | */ |
||
| 149 | 1 | public function sendGroup($params) |
|
| 150 | { |
||
| 151 | 1 | $params['amt_type'] = 'ALL_RAND'; |
|
| 152 | |||
| 153 | 1 | return $this->send($params, self::TYPE_GROUP); |
|
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Merchant setter. |
||
| 158 | * |
||
| 159 | * @param Merchant $merchant |
||
| 160 | * |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | 1 | public function setMerchant(Merchant $merchant) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Merchant getter. |
||
| 170 | * |
||
| 171 | * @return Merchant |
||
| 172 | */ |
||
| 173 | 1 | public function getMerchant() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Make a API request. |
||
| 180 | * |
||
| 181 | * @param string $api |
||
| 182 | * @param array $params |
||
| 183 | * @param string $method |
||
| 184 | * |
||
| 185 | * @return \EasyWeChat\Support\Collection |
||
| 186 | */ |
||
| 187 | 5 | View Code Duplication | protected function request($api, array $params, $method = 'post') |
| 202 | |||
| 203 | /** |
||
| 204 | * Parse Response XML to array. |
||
| 205 | * |
||
| 206 | * @param \Psr\Http\Message\ResponseInterface|string $response |
||
| 207 | * |
||
| 208 | * @return \EasyWeChat\Support\Collection |
||
| 209 | */ |
||
| 210 | 5 | protected function parseResponse($response) |
|
| 218 | } |
||
| 219 |