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