Complex classes like GcmComponent 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 GcmComponent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class GcmComponent extends Component |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Default config |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $_defaultConfig = [ |
||
| 23 | 'api' => [ |
||
| 24 | 'key' => null, |
||
| 25 | 'url' => 'https://gcm-http.googleapis.com/gcm/send' |
||
| 26 | ], |
||
| 27 | 'parameters' => [ |
||
| 28 | 'collapse_key' => null, |
||
| 29 | 'priority' => 'normal', |
||
| 30 | 'delay_while_idle' => false, |
||
| 31 | 'dry_run' => false, |
||
| 32 | 'time_to_live' => 0, |
||
| 33 | 'restricted_package_name' => null |
||
| 34 | ] |
||
| 35 | ]; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * List of parameters available to use in notification messages. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $_allowedNotificationParameters = [ |
||
| 43 | 'title', |
||
| 44 | 'body', |
||
| 45 | 'icon', |
||
| 46 | 'sound', |
||
| 47 | 'badge', |
||
| 48 | 'tag', |
||
| 49 | 'color', |
||
| 50 | 'click_action', |
||
| 51 | 'body_loc_key', |
||
| 52 | 'body_loc_args', |
||
| 53 | 'title_loc_key', |
||
| 54 | 'title_loc_args' |
||
| 55 | ]; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Error code and message. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $_errorMessages = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Response of the request |
||
| 66 | * |
||
| 67 | * @var object |
||
| 68 | */ |
||
| 69 | protected $_response = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Constructor |
||
| 73 | * |
||
| 74 | * @param ComponentRegistry $registry A ComponentRegistry |
||
| 75 | * @param array $config Array of configuration settings |
||
| 76 | */ |
||
| 77 | public function __construct(ComponentRegistry $registry, array $config = []) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * send method |
||
| 90 | * |
||
| 91 | * @param string|array $ids |
||
| 92 | * @param array $payload |
||
| 93 | * @param array $parameters |
||
| 94 | * @throws Exception |
||
| 95 | * @return boolean |
||
| 96 | */ |
||
| 97 | public function send($ids = null, array $payload = [], array $parameters = []) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * sendNotification method |
||
| 122 | * |
||
| 123 | * @param string|array $ids |
||
| 124 | * @param array $notification |
||
| 125 | * @param array $parameters |
||
| 126 | * @return boolean |
||
| 127 | */ |
||
| 128 | public function sendNotification($ids = null, array $notification = [], array $parameters = []) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * sendData method |
||
| 135 | * |
||
| 136 | * @param string|array $ids |
||
| 137 | * @param array $data |
||
| 138 | * @param array $parameters |
||
| 139 | * @return boolean |
||
| 140 | */ |
||
| 141 | public function sendData($ids = null, array $data = [], array $parameters = []) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * response method |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function response() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * _executePush method |
||
| 162 | * |
||
| 163 | * @param string $message |
||
| 164 | * @throws Exception |
||
| 165 | * @return boolean |
||
| 166 | */ |
||
| 167 | protected function _executePush($message) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * _buildMessage method |
||
| 191 | * |
||
| 192 | * @param array|string $ids |
||
| 193 | * @param array $payload |
||
| 194 | * @param array $parameters |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | protected function _buildMessage($ids, $payload, $parameters) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * _checkIds method |
||
| 214 | * |
||
| 215 | * @param null|string|array $ids |
||
| 216 | * @throws Exception |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | protected function _checkIds($ids) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * _checkNotification method |
||
| 238 | * |
||
| 239 | * @param array $notification |
||
| 240 | * @throws Exception |
||
| 241 | * @return array $notification |
||
| 242 | */ |
||
| 243 | protected function _checkNotification(array $notification = []) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * _checkData method |
||
| 268 | * |
||
| 269 | * @param array $data |
||
| 270 | * @throws Exception |
||
| 271 | * @return array $data |
||
| 272 | */ |
||
| 273 | protected function _checkData(array $data = []) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * _checkParameters method |
||
| 293 | * |
||
| 294 | * @param array $parameters |
||
| 295 | * @return array $parameters |
||
| 296 | */ |
||
| 297 | protected function _checkParameters(array $parameters = []) |
||
| 320 | } |
||
| 321 |