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 mixed $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 mixed $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 mixed $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) |
||
177 | |||
178 | /** |
||
179 | * Build the message from the ids, payload and parameters |
||
180 | * |
||
181 | * @param array|string $ids Devices'ids |
||
182 | * @param array $payload The notification and/or some datas |
||
183 | * @param array $parameters Parameters for the GCM request |
||
184 | * @return string |
||
185 | */ |
||
186 | protected function _buildMessage($ids, $payload, $parameters) |
||
200 | |||
201 | /** |
||
202 | * Check if the ids are correct |
||
203 | * |
||
204 | * @param mixed $ids Devices'ids |
||
205 | * @throws Exception |
||
206 | * @return array |
||
207 | */ |
||
208 | protected function _checkIds($ids) |
||
224 | |||
225 | /** |
||
226 | * Check if the notification array is correctly build |
||
227 | * |
||
228 | * @param array $notification The notification |
||
229 | * @throws Exception |
||
230 | * @return array $notification |
||
231 | */ |
||
232 | protected function _checkNotification(array $notification = []) |
||
254 | |||
255 | /** |
||
256 | * Check if the data array is correctly build |
||
257 | * |
||
258 | * @param array $data Some datas |
||
259 | * @throws Exception |
||
260 | * @return array $data |
||
261 | */ |
||
262 | protected function _checkData(array $data = []) |
||
279 | |||
280 | /** |
||
281 | * Check the parameters for the message |
||
282 | * |
||
283 | * @param array $parameters Parameters for the GCM request |
||
284 | * @throws Exception |
||
285 | * @return array $parameters |
||
286 | */ |
||
287 | protected function _checkParameters(array $parameters = []) |
||
309 | |||
310 | /** |
||
311 | * Return options for the HTTP request |
||
312 | * |
||
313 | * @throws Exception |
||
314 | * @return array $options |
||
315 | */ |
||
316 | protected function _getHttpOptions() |
||
332 | } |
||
333 |