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 |
||
| 31 | class Material extends AbstractAPI |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * Allow media type. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $allowTypes = ['image', 'voice', 'video', 'thumb', 'news_image']; |
||
| 39 | |||
| 40 | const API_GET = 'https://api.weixin.qq.com/cgi-bin/material/get_material'; |
||
| 41 | const API_UPLOAD = 'https://api.weixin.qq.com/cgi-bin/material/add_material'; |
||
| 42 | const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/material/del_material'; |
||
| 43 | const API_STATS = 'https://api.weixin.qq.com/cgi-bin/material/get_materialcount'; |
||
| 44 | const API_LISTS = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material'; |
||
| 45 | const API_NEWS_UPLOAD = 'https://api.weixin.qq.com/cgi-bin/material/add_news'; |
||
| 46 | const API_NEWS_UPDATE = 'https://api.weixin.qq.com/cgi-bin/material/update_news'; |
||
| 47 | const API_NEWS_IMAGE_UPLOAD = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Upload image. |
||
| 51 | * |
||
| 52 | * @param string $path |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | 1 | public function uploadImage($path) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Upload voice. |
||
| 63 | * |
||
| 64 | * @param string $path |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | 1 | public function uploadVoice($path) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Upload thumb. |
||
| 75 | * |
||
| 76 | * @param string $path |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | 1 | public function uploadThumb($path) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Upload video. |
||
| 87 | * |
||
| 88 | * @param string $path |
||
| 89 | * @param string $title |
||
| 90 | * @param string $description |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | 1 | public function uploadVideo($path, $title, $description) |
|
| 95 | { |
||
| 96 | $params = [ |
||
| 97 | 1 | 'description' => json_encode( |
|
| 98 | [ |
||
| 99 | 1 | 'title' => $title, |
|
| 100 | 1 | 'introduction' => $description, |
|
| 101 | 1 | ], JSON_UNESCAPED_UNICODE), |
|
| 102 | ]; |
||
| 103 | |||
| 104 | 1 | return $this->uploadMedia('video', $path, $params); |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Upload articles. |
||
| 109 | * |
||
| 110 | * @param array|Article $articles |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | 1 | public function uploadArticle($articles) |
|
| 115 | { |
||
| 116 | 1 | if (!empty($articles['title']) || $articles instanceof Article) { |
|
| 117 | 1 | $articles = [$articles]; |
|
| 118 | } |
||
| 119 | |||
| 120 | 1 | $params = ['articles' => array_map(function ($article) { |
|
| 121 | 1 | if ($article instanceof Article) { |
|
| 122 | 1 | return $article->only([ |
|
| 123 | 1 | 'title', 'thumb_media_id', 'author', 'digest', |
|
| 124 | 'show_cover_pic', 'content', 'content_source_url', |
||
| 125 | ]); |
||
| 126 | } |
||
| 127 | |||
| 128 | 1 | return $article; |
|
| 129 | 1 | }, $articles)]; |
|
| 130 | |||
| 131 | 1 | return $this->parseJSON('json', [self::API_NEWS_UPLOAD, $params]); |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Update article. |
||
| 136 | * |
||
| 137 | * @param string $mediaId |
||
| 138 | * @param array $article |
||
| 139 | * @param int $index |
||
| 140 | * |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | 1 | public function updateArticle($mediaId, $article, $index = 0) |
|
| 144 | { |
||
| 145 | $params = [ |
||
| 146 | 1 | 'media_id' => $mediaId, |
|
| 147 | 1 | 'index' => $index, |
|
| 148 | 1 | 'articles' => isset($article['title']) ? $article : (isset($article[$index]) ? $article[$index] : []), |
|
| 149 | ]; |
||
| 150 | |||
| 151 | 1 | return $this->parseJSON('json', [self::API_NEWS_UPDATE, $params]); |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Upload image for article. |
||
| 156 | * |
||
| 157 | * @param string $path |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | 1 | public function uploadArticleImage($path) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Fetch material. |
||
| 168 | * |
||
| 169 | * @param string $mediaId |
||
| 170 | * |
||
| 171 | * @return mixed |
||
| 172 | */ |
||
| 173 | 1 | public function get($mediaId) |
|
| 174 | { |
||
| 175 | 1 | $response = $this->getHttp()->json(self::API_GET, ['media_id' => $mediaId]); |
|
| 176 | |||
| 177 | 1 | foreach ($response->getHeader('Content-Type') as $mime) { |
|
| 178 | 1 | if (preg_match('/(image|video|audio)/i', $mime)) { |
|
| 179 | 1 | return $response->getBody(); |
|
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | 1 | $json = $this->getHttp()->parseJSON($response); |
|
| 184 | |||
| 185 | // XXX: 微信开发这帮混蛋,尼玛文件二进制输出不带header,简直日了!!! |
||
| 186 | 1 | if (!$json) { |
|
| 187 | return $response->getBody(); |
||
| 188 | } |
||
| 189 | |||
| 190 | 1 | $this->checkAndThrow($json); |
|
| 191 | |||
| 192 | 1 | return $json; |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Delete material by media ID. |
||
| 197 | * |
||
| 198 | * @param string $mediaId |
||
| 199 | * |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | 1 | public function delete($mediaId) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * List materials. |
||
| 209 | * |
||
| 210 | * example: |
||
| 211 | * |
||
| 212 | * { |
||
| 213 | * "total_count": TOTAL_COUNT, |
||
| 214 | * "item_count": ITEM_COUNT, |
||
| 215 | * "item": [{ |
||
| 216 | * "media_id": MEDIA_ID, |
||
| 217 | * "name": NAME, |
||
| 218 | * "update_time": UPDATE_TIME |
||
| 219 | * }, |
||
| 220 | * // more... |
||
| 221 | * ] |
||
| 222 | * } |
||
| 223 | * |
||
| 224 | * @param string $type |
||
| 225 | * @param int $offset |
||
| 226 | * @param int $count |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | 1 | View Code Duplication | public function lists($type, $offset = 0, $count = 20) |
| 231 | { |
||
| 232 | $params = [ |
||
| 233 | 1 | 'type' => $type, |
|
| 234 | 1 | 'offset' => intval($offset), |
|
| 235 | 1 | 'count' => min(20, $count), |
|
| 236 | ]; |
||
| 237 | |||
| 238 | 1 | return $this->parseJSON('json', [self::API_LISTS, $params]); |
|
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get stats of materials. |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | 1 | public function stats() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Upload material. |
||
| 253 | * |
||
| 254 | * @param string $type |
||
| 255 | * @param string $path |
||
| 256 | * @param array $form |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | * |
||
| 260 | * @throws InvalidArgumentException |
||
| 261 | */ |
||
| 262 | 5 | protected function uploadMedia($type, $path, array $form = []) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Get API by type. |
||
| 275 | * |
||
| 276 | * @param string $type |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | 5 | public function getAPIByType($type) |
|
| 292 | } |
||
| 293 |