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 | 'http' => [] |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * List of parameters available to use in notification messages. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $_allowedNotificationParameters = [ |
||
| 44 | 'title', |
||
| 45 | 'body', |
||
| 46 | 'icon', |
||
| 47 | 'sound', |
||
| 48 | 'badge', |
||
| 49 | 'tag', |
||
| 50 | 'color', |
||
| 51 | 'click_action', |
||
| 52 | 'body_loc_key', |
||
| 53 | 'body_loc_args', |
||
| 54 | 'title_loc_key', |
||
| 55 | 'title_loc_args' |
||
| 56 | ]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Error code and message. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $_errorMessages = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Response of the request |
||
| 67 | * |
||
| 68 | * @var object |
||
| 69 | */ |
||
| 70 | protected $_response = null; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Constructor |
||
| 74 | * |
||
| 75 | * @param ComponentRegistry $registry A ComponentRegistry |
||
| 76 | * @param array $config Array of configuration settings |
||
| 77 | */ |
||
| 78 | public function __construct(ComponentRegistry $registry, array $config = []) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Send a downstream message to one or more devices |
||
| 91 | * |
||
| 92 | * @param string|array $ids Devices'ids |
||
| 93 | * @param array $payload The notification and/or some datas |
||
| 94 | * @param array $parameters Parameters for the GCM request |
||
| 95 | * @throws Exception |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function send($ids = null, array $payload = [], array $parameters = []) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Shortcut to send notification |
||
| 123 | * |
||
| 124 | * @param string|array $ids Devices'ids |
||
| 125 | * @param array $notification The notification |
||
| 126 | * @param array $parameters Parameters for the GCM request |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | public function sendNotification($ids = null, array $notification = [], array $parameters = []) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Shortcut to send datas |
||
| 136 | * |
||
| 137 | * @param string|array $ids Devices'ids |
||
| 138 | * @param array $data Some datas |
||
| 139 | * @param array $parameters Parameters for the GCM request |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function sendData($ids = null, array $data = [], array $parameters = []) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Return the response of the push |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function response() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Send the message throught a POST request to the GCM servers |
||
| 163 | * |
||
| 164 | * @param string $message The message to send |
||
| 165 | * @throws Exception |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | protected function _executePush($message) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Build the message from the ids, payload and parameters |
||
| 195 | * |
||
| 196 | * @param array|string $ids Devices'ids |
||
| 197 | * @param array $payload The notification and/or some datas |
||
| 198 | * @param array $parameters Parameters for the GCM request |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | protected function _buildMessage($ids, $payload, $parameters) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Check if the ids are correct |
||
| 218 | * |
||
| 219 | * @param mixed $ids Devices'ids |
||
| 220 | * @throws Exception |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | protected function _checkIds($ids) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Check if the notification array is correctly build |
||
| 242 | * |
||
| 243 | * @param array $notification The notification |
||
| 244 | * @throws Exception |
||
| 245 | * @return array $notification |
||
| 246 | */ |
||
| 247 | protected function _checkNotification(array $notification = []) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Check if the data array is correctly build |
||
| 272 | * |
||
| 273 | * @param array $data Some datas |
||
| 274 | * @throws Exception |
||
| 275 | * @return array $data |
||
| 276 | */ |
||
| 277 | protected function _checkData(array $data = []) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Check the parameters for the message |
||
| 297 | * |
||
| 298 | * @param array $parameters Parameters for the GCM request |
||
| 299 | * @throws Exception |
||
| 300 | * @return array $parameters |
||
| 301 | */ |
||
| 302 | protected function _checkParameters(array $parameters = []) |
||
| 325 | } |
||
| 326 |