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 AccessToken |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * App ID. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $appId; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * App secret. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $secret; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Cache. |
||
| 49 | * |
||
| 50 | * @var Cache |
||
| 51 | */ |
||
| 52 | protected $cache; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Cache Key. |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $cacheKey; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Http instance. |
||
| 63 | * |
||
| 64 | * @var Http |
||
| 65 | */ |
||
| 66 | protected $http; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Query name. |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $queryName = 'access_token'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Response Json key name. |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $tokenJsonKey = 'access_token'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Cache key prefix. |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $prefix = 'easywechat.common.access_token.'; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | protected $autoRefresh = true; |
||
| 93 | |||
| 94 | // API |
||
| 95 | const API_TOKEN_GET = 'https://api.weixin.qq.com/cgi-bin/token'; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Constructor. |
||
| 99 | 61 | * |
|
| 100 | * @param string $appId |
||
| 101 | 61 | * @param string $secret |
|
| 102 | 61 | * @param \Doctrine\Common\Cache\Cache $cache |
|
| 103 | 61 | */ |
|
| 104 | 61 | public function __construct($appId, $secret, Cache $cache = null) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Get token from WeChat API. |
||
| 113 | 5 | * |
|
| 114 | * @param bool $forceRefresh |
||
| 115 | 5 | * |
|
| 116 | 5 | * @return string |
|
| 117 | */ |
||
| 118 | 5 | public function getToken($forceRefresh = false) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * 设置自定义 token. |
||
| 137 | * |
||
| 138 | 2 | * @param string $token |
|
| 139 | * @param int $expires |
||
| 140 | 2 | * |
|
| 141 | * @return $this |
||
| 142 | 2 | */ |
|
| 143 | public function setToken($token, $expires = 7200) |
||
| 149 | |||
| 150 | 20 | /** |
|
| 151 | * Return the app id. |
||
| 152 | 20 | * |
|
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function getAppId() |
||
| 159 | |||
| 160 | 2 | /** |
|
| 161 | * Return the secret. |
||
| 162 | 2 | * |
|
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | public function getSecret() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set cache instance. |
||
| 172 | 2 | * |
|
| 173 | * @param \Doctrine\Common\Cache\Cache $cache |
||
| 174 | 2 | * |
|
| 175 | * @return AccessToken |
||
| 176 | 2 | */ |
|
| 177 | public function setCache(Cache $cache) |
||
| 183 | |||
| 184 | 5 | /** |
|
| 185 | * Return the cache manager. |
||
| 186 | 5 | * |
|
| 187 | * @return \Doctrine\Common\Cache\Cache |
||
| 188 | */ |
||
| 189 | public function getCache() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Set the query name. |
||
| 196 | 1 | * |
|
| 197 | * @param string $queryName |
||
| 198 | 1 | * |
|
| 199 | * @return $this |
||
| 200 | 1 | */ |
|
| 201 | public function setQueryName($queryName) |
||
| 207 | |||
| 208 | 1 | /** |
|
| 209 | * Return the query name. |
||
| 210 | 1 | * |
|
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | public function getQueryName() |
||
| 217 | |||
| 218 | 1 | /** |
|
| 219 | * Return the API request queries. |
||
| 220 | 1 | * |
|
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | public function getQueryFields() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the access token from WeChat server. |
||
| 230 | 2 | * |
|
| 231 | * @throws \EasyWeChat\Core\Exceptions\HttpException |
||
| 232 | * |
||
| 233 | 2 | * @return string |
|
| 234 | 2 | */ |
|
| 235 | 2 | View Code Duplication | public function getTokenFromServer() |
| 253 | |||
| 254 | 2 | /** |
|
| 255 | * Return the http instance. |
||
| 256 | 2 | * |
|
| 257 | * @return \EasyWeChat\Core\Http |
||
| 258 | */ |
||
| 259 | public function getHttp() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Set the http instance. |
||
| 266 | 2 | * |
|
| 267 | * @param \EasyWeChat\Core\Http $http |
||
| 268 | 2 | * |
|
| 269 | * @return $this |
||
| 270 | 2 | */ |
|
| 271 | public function setHttp(Http $http) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Set the access token prefix. |
||
| 280 | * |
||
| 281 | * @param string $prefix |
||
| 282 | * |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | public function setPrefix($prefix) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Set access token cache key. |
||
| 294 | * |
||
| 295 | * @param string $cacheKey |
||
| 296 | * |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function setCacheKey($cacheKey) |
||
| 305 | |||
| 306 | 5 | /** |
|
| 307 | * Get access token cache key. |
||
| 308 | 5 | * |
|
| 309 | 5 | * @return string $this->cacheKey |
|
| 310 | */ |
||
| 311 | public function getCacheKey() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param bool $enabled |
||
| 322 | * |
||
| 323 | * @return $this |
||
| 324 | */ |
||
| 325 | public function autoRefresh($enabled = true) |
||
| 331 | } |
||
| 332 |