Complex classes like Pusher often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Pusher, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Pusher |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * The XingeApp instance. |
||
| 12 | * |
||
| 13 | * @var \ElfSundae\XgPush\XingeApp |
||
| 14 | */ |
||
| 15 | protected $xinge; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The pusher environment. |
||
| 19 | * |
||
| 20 | * 向iOS设备推送时必填,1表示推送生产环境;2表示推送开发环境。推送Android平台不填或填0. |
||
| 21 | * |
||
| 22 | * @var int |
||
| 23 | */ |
||
| 24 | protected $environment = XingeApp::IOSENV_DEV; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The key of custom payload. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $customKey = 'custom'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Xinge account prefix. |
||
| 35 | * |
||
| 36 | * @warning 信鸽不允许使用简单的账号,例如纯数字的id。 |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $accountPrefix; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Create a new instance. |
||
| 44 | * |
||
| 45 | * @param string $appKey |
||
| 46 | * @param string $appSecret |
||
| 47 | */ |
||
| 48 | 1 | public function __construct($appKey, $appSecret) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Get the XingeApp instance. |
||
| 55 | * |
||
| 56 | * @return \ElfSundae\XgPush\XingeApp |
||
| 57 | */ |
||
| 58 | public function xinge() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get the app key. |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | public function getAppKey() |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get the app secret. |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public function getAppSecret() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Get the pusher environment. |
||
| 85 | * |
||
| 86 | * @return int |
||
| 87 | */ |
||
| 88 | public function getEnvironment() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Set the pusher environment. |
||
| 95 | * |
||
| 96 | * @param mixed $env |
||
| 97 | * @return $this |
||
| 98 | */ |
||
| 99 | public function setEnvironment($env) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get the key of custom payload. |
||
| 114 | * |
||
| 115 | * @return string|null |
||
| 116 | */ |
||
| 117 | public function getCustomKey() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Set the key of custom payload. |
||
| 124 | * |
||
| 125 | * @param string|null $key |
||
| 126 | * @return $this |
||
| 127 | */ |
||
| 128 | public function setCustomKey($key) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get the account prefix. |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function getAccountPrefix() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Set the account prefix. |
||
| 147 | * |
||
| 148 | * @param string $prefix |
||
| 149 | * @return $this |
||
| 150 | */ |
||
| 151 | public function setAccountPrefix($prefix) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Determine if the Xinge response is success. |
||
| 160 | * |
||
| 161 | * @see http://developer.qq.com/wiki/xg/%E6%9C%8D%E5%8A%A1%E7%AB%AFAPI%E6%8E%A5%E5%85%A5/Rest%20API%20%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/Rest%20API%20%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97.html |
||
| 162 | * |
||
| 163 | * @param mixed $response |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | public function succeed($response) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get the code of Xinge response. |
||
| 173 | * |
||
| 174 | * @param mixed $response |
||
| 175 | * @return int|null |
||
| 176 | */ |
||
| 177 | public function code($response) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get the error message of Xinge response. |
||
| 186 | * |
||
| 187 | * @param mixed $response |
||
| 188 | * @return string|null |
||
| 189 | */ |
||
| 190 | public function message($response) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get the result data of Xinge response. |
||
| 199 | * |
||
| 200 | * @param mixed $response |
||
| 201 | * @param string $key |
||
| 202 | * @param mixed $default |
||
| 203 | * @return mixed |
||
| 204 | */ |
||
| 205 | public function result($response, $key = null, $default = null) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Encode the custom data. |
||
| 214 | * |
||
| 215 | * @param mixed $data |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | public function encodeCustomData($data) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get Xinge account for the given user. |
||
| 229 | * |
||
| 230 | * @param mixed $user |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function accountForUser($user) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get Xinge accounts for users. |
||
| 250 | * |
||
| 251 | * @param array $users |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | public function accountsForUsers(array $users) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Creates a new MessageIOS instance. |
||
| 261 | * |
||
| 262 | * @param string $alert |
||
| 263 | * @param mixed $custom |
||
| 264 | * @param int $badge |
||
| 265 | * @param string $sound |
||
| 266 | * @return \ElfSundae\XgPush\MessageIOS |
||
| 267 | */ |
||
| 268 | public function createIOSMessage($alert = '', $custom = null, $badge = 1, $sound = 'default') |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Create a new Message instance. |
||
| 285 | * |
||
| 286 | * @param string $title |
||
| 287 | * @param string $content |
||
| 288 | * @param mixed $custom |
||
| 289 | * @param int $type |
||
| 290 | * @return \ElfSundae\XgPush\Message |
||
| 291 | */ |
||
| 292 | public function createAndroidMessage($title = '', $content = '', $custom = null, $type = Message::TYPE_MESSAGE) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Create a new Message instance for notification. |
||
| 305 | * The default action is opening app. |
||
| 306 | * |
||
| 307 | * @param string $title |
||
| 308 | * @param string $content |
||
| 309 | * @param mixed $custom |
||
| 310 | * @return \ElfSundae\XgPush\Message |
||
| 311 | */ |
||
| 312 | public function createAndroidNotification($title = '', $content = '', $custom = null) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Push message to a device. |
||
| 326 | * |
||
| 327 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
| 328 | * @param string $deviceToken |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | public function toDevice($message, $deviceToken) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Push message to all devices. |
||
| 338 | * |
||
| 339 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
| 340 | * @return array |
||
| 341 | */ |
||
| 342 | public function toAllDevices($message) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Push message to users. |
||
| 349 | * |
||
| 350 | * @warning 用户数限制 100 个。 |
||
| 351 | * |
||
| 352 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
| 353 | * @param mixed|array $users |
||
| 354 | * @return array |
||
| 355 | */ |
||
| 356 | public function toUser($message, $users) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Push message to tagged devices. |
||
| 369 | * |
||
| 370 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
| 371 | * @param string|string[] $tags |
||
| 372 | * @param string $tagsOperation 'OR', 'AND' |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | public function toTags($message, $tags, $tagsOperation = 'OR') |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Create a batch push. |
||
| 382 | * |
||
| 383 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
| 384 | * @return string|null |
||
| 385 | */ |
||
| 386 | public function createBatch($message) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Batch pushing to a list of users. |
||
| 393 | * |
||
| 394 | * @warning 用户数限制 1000 个。 |
||
| 395 | * |
||
| 396 | * @param int|string $pushId |
||
| 397 | * @param mixed|array $users |
||
| 398 | * @return array |
||
| 399 | */ |
||
| 400 | public function batchToUsers($pushId, $users) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Batch pushing to a list of devices. |
||
| 407 | * |
||
| 408 | * @warning 设备数限制 1000 个。 |
||
| 409 | * |
||
| 410 | * @param int|string $pushId |
||
| 411 | * @param string|string[] $deviceTokens |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | public function batchToDevices($pushId, $deviceTokens) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Query group pushing status. |
||
| 421 | * |
||
| 422 | * @param string|string[] $pushIds |
||
| 423 | * @return array |
||
| 424 | */ |
||
| 425 | public function queryPushStatus($pushIds) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Cancel a timed pushing task that has not been pushed. |
||
| 434 | * |
||
| 435 | * @param string $pushId |
||
| 436 | * @return array |
||
| 437 | */ |
||
| 438 | public function cancelTimedPush($pushId) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Query all device tokens for the given user. |
||
| 445 | * |
||
| 446 | * @param mixed $user |
||
| 447 | * @return string[] |
||
| 448 | */ |
||
| 449 | public function queryDeviceTokensForUser($user) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Delete device tokens for the given user. |
||
| 456 | * |
||
| 457 | * @param mixed $user |
||
| 458 | * @param string|string[] $deviceTokens |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | public function deleteDeviceTokensForUser($user, $deviceTokens = null) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Query count of registered devices. |
||
| 482 | * |
||
| 483 | * @return int |
||
| 484 | */ |
||
| 485 | public function queryCountOfDevices() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Query info for the given device token. |
||
| 492 | * |
||
| 493 | * @param string $deviceToken |
||
| 494 | * @return array |
||
| 495 | */ |
||
| 496 | public function queryDeviceTokenInfo($deviceToken) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Query count of registered tokens for the given tag. |
||
| 503 | * |
||
| 504 | * @param string $tag |
||
| 505 | * @return int |
||
| 506 | */ |
||
| 507 | public function queryCountOfDeviceTokensForTag($tag) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Query tags. |
||
| 514 | * |
||
| 515 | * @return array |
||
| 516 | */ |
||
| 517 | public function queryTags($start = 0, $limit = 100) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Query all tags for the given device token. |
||
| 524 | * |
||
| 525 | * @param string $deviceToken |
||
| 526 | * @return string[] |
||
| 527 | */ |
||
| 528 | public function queryTagsForDeviceToken($deviceToken) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Query all tags for the given user. |
||
| 535 | * |
||
| 536 | * @param mixed $user |
||
| 537 | * @param array &$deviceTokens |
||
| 538 | * @return array |
||
| 539 | */ |
||
| 540 | public function queryTagsForUser($user, &$deviceTokens = null) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Add tags for device tokens. |
||
| 554 | * |
||
| 555 | * @warning 每次最多设置 20 对。 |
||
| 556 | * |
||
| 557 | * @param \ElfSundae\XgPush\TagTokenPair[] $tagTokenPairs |
||
| 558 | * @return bool |
||
| 559 | */ |
||
| 560 | public function addTags($tagTokenPairs) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Add tags for the given device token. |
||
| 567 | * |
||
| 568 | * @param string $deviceToken |
||
| 569 | * @param string|string[] $tags |
||
| 570 | * @return bool |
||
| 571 | */ |
||
| 572 | public function addTagsForDeviceToken($deviceToken, $tags) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Add tags for the given user. |
||
| 579 | * |
||
| 580 | * @param mixed $user |
||
| 581 | * @param string|array $tags |
||
| 582 | * @return bool |
||
| 583 | */ |
||
| 584 | public function addTagsForUser($user, $tags) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Remove tags for device tokens. |
||
| 593 | * |
||
| 594 | * @warning 每次最多删除 20 对。 |
||
| 595 | * |
||
| 596 | * @param \ElfSundae\XgPush\TagTokenPair[] $tagTokenPairs |
||
| 597 | * @return bool |
||
| 598 | */ |
||
| 599 | public function removeTags($tagTokenPairs) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Remove tags for the given device token. |
||
| 606 | * |
||
| 607 | * @param string $deviceToken |
||
| 608 | * @param string|string[] $tags |
||
| 609 | * @return bool |
||
| 610 | */ |
||
| 611 | public function removeTagsForDeviceToken($deviceToken, $tags) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Remove tags for the given user. |
||
| 620 | * |
||
| 621 | * @param mixed $user |
||
| 622 | * @param string|string[] $tags |
||
| 623 | * @return bool |
||
| 624 | */ |
||
| 625 | public function removeTagsForUser($user, $tags) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Set tags for the given device token. |
||
| 634 | * |
||
| 635 | * @param string $deviceToken |
||
| 636 | * @param string|string[] $tags |
||
| 637 | * @return bool |
||
| 638 | */ |
||
| 639 | public function setTagsForDeviceToken($deviceToken, $tags) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Set tags for the given user. |
||
| 659 | * |
||
| 660 | * @param mixed $user |
||
| 661 | * @param string|string[] $tags |
||
| 662 | * @return bool |
||
| 663 | */ |
||
| 664 | public function setTagsForUser($user, $tags) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Create array of TagTokenPair. |
||
| 697 | * |
||
| 698 | * @warning $tags 和 $tokens 一个是数组,另一个是字符串 |
||
| 699 | * |
||
| 700 | * @param string|string[] $tags |
||
| 701 | * @param string|string[] $tokens |
||
| 702 | * @return \ElfSundae\XgPush\TagTokenPair[] |
||
| 703 | */ |
||
| 704 | protected function createTagTokenPairs($tags, $tokens) |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Dynamically handle calls to the XingeApp instance. |
||
| 720 | * |
||
| 721 | * @param string $method |
||
| 722 | * @param array $parameters |
||
| 723 | * @return mixed |
||
| 724 | */ |
||
| 725 | public function __call($method, $parameters) |
||
| 729 | } |
||
| 730 |