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 | 1 | ]; |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
| 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 = []) |
|
| 263 | { |
||
| 264 | 5 | if (!file_exists($path) || !is_readable($path)) { |
|
| 265 | 1 | throw new InvalidArgumentException("File does not exist, or the file is unreadable: '$path'"); |
|
| 266 | } |
||
| 267 | |||
| 268 | 5 | $form['type'] = $type; |
|
| 269 | |||
| 270 | 5 | return $this->parseJSON('upload', [$this->getAPIByType($type), ['media' => $path], $form]); |
|
| 271 | } |
||
| 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 |