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 a downstream message to one or more devices |
||
| 90 | * |
||
| 91 | * @param string|array $ids Devices'ids |
||
| 92 | * @param array $payload The notification and/or some datas |
||
| 93 | * @param array $parameters Parameters for the GCM request |
||
| 94 | * @throws Exception |
||
| 95 | * @return bool |
||
| 96 | */ |
||
| 97 | public function send($ids = null, array $payload = [], array $parameters = []) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Shortcut to send notification |
||
| 122 | * |
||
| 123 | * @param string|array $ids Devices'ids |
||
| 124 | * @param array $notification The notification |
||
| 125 | * @param array $parameters Parameters for the GCM request |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | public function sendNotification($ids = null, array $notification = [], array $parameters = []) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Shortcut to send datas |
||
| 135 | * |
||
| 136 | * @param string|array $ids Devices'ids |
||
| 137 | * @param array $data Some datas |
||
| 138 | * @param array $parameters Parameters for the GCM request |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | public function sendData($ids = null, array $data = [], array $parameters = []) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Return the response of the push |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function response() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Send the message throught a POST request to the GCM servers |
||
| 162 | * |
||
| 163 | * @param string $message The message to send |
||
| 164 | * @throws Exception |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | protected function _executePush($message) |
||
| 168 | { |
||
| 169 | if ($this->config('api.key') === null) { |
||
| 170 | throw new Exception(__('No API key set. Push not triggered')); |
||
| 171 | } |
||
| 172 | |||
| 173 | $http = new Client(); |
||
| 174 | $this->_response = $http->post($this->config('api.url'), $message, [ |
||
| 175 | 'type' => 'json', |
||
| 176 | 'headers' => [ |
||
| 177 | 'Authorization' => 'key=' . $this->config('api.key'), |
||
| 178 | 'Content-Type' => 'application/json' |
||
| 179 | ] |
||
| 180 | ]); |
||
| 181 | |||
| 182 | if ($this->_response->code === '200') { |
||
| 183 | return true; |
||
| 184 | } |
||
| 185 | |||
| 186 | return false; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Build the message from the ids, payload and parameters |
||
| 191 | * |
||
| 192 | * @param array|string $ids Devices'ids |
||
| 193 | * @param array $payload The notification and/or some datas |
||
| 194 | * @param array $parameters Parameters for the GCM request |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | protected function _buildMessage($ids, $payload, $parameters) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Check if the ids are correct |
||
| 214 | * |
||
| 215 | * @param mixed $ids Devices'ids |
||
| 216 | * @throws Exception |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | protected function _checkIds($ids) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Check if the notification array is correctly build |
||
| 238 | * |
||
| 239 | * @param array $notification The notification |
||
| 240 | * @throws Exception |
||
| 241 | * @return array $notification |
||
| 242 | */ |
||
| 243 | protected function _checkNotification(array $notification = []) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Check if the data array is correctly build |
||
| 268 | * |
||
| 269 | * @param array $data Some datas |
||
| 270 | * @throws Exception |
||
| 271 | * @return array $data |
||
| 272 | */ |
||
| 273 | protected function _checkData(array $data = []) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Check the parameters for the message |
||
| 293 | * |
||
| 294 | * @param array $parameters Parameters for the GCM request |
||
| 295 | * @throws Exception |
||
| 296 | * @return array $parameters |
||
| 297 | */ |
||
| 298 | protected function _checkParameters(array $parameters = []) |
||
| 321 | } |
||
| 322 |