Complex classes like Gcm 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 Gcm, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Gcm |
||
| 10 | { |
||
| 11 | |||
| 12 | use InstanceConfigTrait; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Default config |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $_defaultConfig = [ |
||
| 20 | 'api' => [ |
||
| 21 | 'key' => null, |
||
| 22 | 'url' => 'https://gcm-http.googleapis.com/gcm/send' |
||
| 23 | ], |
||
| 24 | 'parameters' => [ |
||
| 25 | 'collapse_key' => null, |
||
| 26 | 'priority' => 'normal', |
||
| 27 | 'delay_while_idle' => false, |
||
| 28 | 'dry_run' => false, |
||
| 29 | 'time_to_live' => 0, |
||
| 30 | 'restricted_package_name' => null |
||
| 31 | ], |
||
| 32 | 'http' => [] |
||
| 33 | ]; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * List of parameters available to use in notification messages. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $_allowedNotificationParameters = [ |
||
| 41 | 'title', |
||
| 42 | 'body', |
||
| 43 | 'icon', |
||
| 44 | 'sound', |
||
| 45 | 'badge', |
||
| 46 | 'tag', |
||
| 47 | 'color', |
||
| 48 | 'click_action', |
||
| 49 | 'body_loc_key', |
||
| 50 | 'body_loc_args', |
||
| 51 | 'title_loc_key', |
||
| 52 | 'title_loc_args' |
||
| 53 | ]; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Error code and message. |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $_errorMessages = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Response of the request |
||
| 64 | * |
||
| 65 | * @var object |
||
| 66 | */ |
||
| 67 | protected $_response = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Constructor |
||
| 71 | * |
||
| 72 | * @param array $config Array of configuration settings |
||
| 73 | */ |
||
| 74 | public function __construct(array $config = []) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Send a downstream message to one or more devices |
||
| 88 | * |
||
| 89 | * @param mixed $ids Devices'ids |
||
| 90 | * @param array $payload The notification and/or some datas |
||
| 91 | * @param array $parameters Parameters for the GCM request |
||
| 92 | * @throws Exception |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | public function send($ids = null, array $payload = [], array $parameters = []) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Shortcut to send notification |
||
| 120 | * |
||
| 121 | * @param mixed $ids Devices'ids |
||
| 122 | * @param array $notification The notification |
||
| 123 | * @param array $parameters Parameters for the GCM request |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | public function sendNotification($ids = null, array $notification = [], array $parameters = []) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Shortcut to send datas |
||
| 133 | * |
||
| 134 | * @param mixed $ids Devices'ids |
||
| 135 | * @param array $data Some datas |
||
| 136 | * @param array $parameters Parameters for the GCM request |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | public function sendData($ids = null, array $data = [], array $parameters = []) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Return the response of the push |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | public function response() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Send the message throught a POST request to the GCM servers |
||
| 160 | * |
||
| 161 | * @param string $message The message to send |
||
| 162 | * @throws Exception |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | protected function _executePush($message) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Build the message from the ids, payload and parameters |
||
| 177 | * |
||
| 178 | * @param array|string $ids Devices'ids |
||
| 179 | * @param array $payload The notification and/or some datas |
||
| 180 | * @param array $parameters Parameters for the GCM request |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | protected function _buildMessage($ids, $payload, $parameters) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Check if the ids are correct |
||
| 200 | * |
||
| 201 | * @param mixed $ids Devices'ids |
||
| 202 | * @throws Exception |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | protected function _checkIds($ids) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Check if the notification array is correctly build |
||
| 224 | * |
||
| 225 | * @param array $notification The notification |
||
| 226 | * @throws Exception |
||
| 227 | * @return array $notification |
||
| 228 | */ |
||
| 229 | protected function _checkNotification(array $notification = []) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Check if the data array is correctly build |
||
| 254 | * |
||
| 255 | * @param array $data Some datas |
||
| 256 | * @throws Exception |
||
| 257 | * @return array $data |
||
| 258 | */ |
||
| 259 | protected function _checkData(array $data = []) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Check the parameters for the message |
||
| 279 | * |
||
| 280 | * @param array $parameters Parameters for the GCM request |
||
| 281 | * @throws Exception |
||
| 282 | * @return array $parameters |
||
| 283 | */ |
||
| 284 | protected function _checkParameters(array $parameters = []) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Return options for the HTTP request |
||
| 309 | * |
||
| 310 | * @throws Exception |
||
| 311 | * @return array $options |
||
| 312 | */ |
||
| 313 | protected function _getHttpOptions() |
||
| 329 | } |
||
| 330 |