Complex classes like FcmAdapter 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 FcmAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class FcmAdapter implements AdapterInterface |
||
11 | { |
||
12 | |||
13 | use InstanceConfigTrait; |
||
14 | |||
15 | /** |
||
16 | * Default config |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $_defaultConfig = [ |
||
21 | 'api' => [ |
||
22 | 'key' => null, |
||
23 | 'url' => 'https://fcm.googleapis.com/fcm/send', |
||
24 | ], |
||
25 | 'parameters' => [ |
||
26 | 'collapse_key' => null, |
||
27 | 'priority' => 'normal', |
||
28 | 'delay_while_idle' => false, |
||
29 | 'dry_run' => false, |
||
30 | 'time_to_live' => 0, |
||
31 | 'restricted_package_name' => null, |
||
32 | ], |
||
33 | 'http' => [], |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * List of parameters available to use in notification messages. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $_allowedNotificationParameters = [ |
||
42 | 'title', |
||
43 | 'body', |
||
44 | 'icon', |
||
45 | 'sound', |
||
46 | 'badge', |
||
47 | 'tag', |
||
48 | 'color', |
||
49 | 'click_action', |
||
50 | 'body_loc_key', |
||
51 | 'body_loc_args', |
||
52 | 'title_loc_key', |
||
53 | 'title_loc_args', |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * Error code and message. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $_errorMessages = []; |
||
62 | |||
63 | /** |
||
64 | * Response of the request |
||
65 | * |
||
66 | * @var object |
||
67 | */ |
||
68 | protected $_response = null; |
||
69 | |||
70 | /** |
||
71 | * Constructor |
||
72 | * |
||
73 | * @param array $config Array of configuration settings |
||
74 | */ |
||
75 | public function __construct(array $config = []) |
||
86 | |||
87 | /** |
||
88 | * @inheritdoc |
||
89 | */ |
||
90 | public function send($tokens = null, array $payload = [], array $parameters = []) |
||
112 | |||
113 | /** |
||
114 | * @inheritdoc |
||
115 | */ |
||
116 | public function response() |
||
128 | |||
129 | /** |
||
130 | * Send the message throught a POST request to the GCM servers |
||
131 | * |
||
132 | * @param string $message The message to send |
||
133 | * @throws Exception |
||
134 | * @return bool |
||
135 | */ |
||
136 | protected function _executePush($message) |
||
145 | |||
146 | /** |
||
147 | * Build the message from the ids, payload and parameters |
||
148 | * |
||
149 | * @param array|string $tokens Devices'ids |
||
150 | * @param array $payload The notification and/or some datas |
||
151 | * @param array $parameters Parameters for the GCM request |
||
152 | * @return string |
||
153 | */ |
||
154 | protected function _buildMessage($tokens, $payload, $parameters) |
||
168 | |||
169 | /** |
||
170 | * Check if the tokens are correct |
||
171 | * |
||
172 | * @param mixed $tokens Device's token |
||
173 | * @throws Exception |
||
174 | * @return array |
||
175 | */ |
||
176 | protected function _checkTokens($tokens) |
||
191 | |||
192 | /** |
||
193 | * Check if the notification array is correctly build |
||
194 | * |
||
195 | * @param array $notification The notification |
||
196 | * @throws Exception |
||
197 | * @return array $notification |
||
198 | */ |
||
199 | protected function _checkNotification(array $notification = []) |
||
221 | |||
222 | /** |
||
223 | * Check if the data array is correctly build |
||
224 | * |
||
225 | * @param array $data Some datas |
||
226 | * @throws Exception |
||
227 | * @return array $data |
||
228 | */ |
||
229 | protected function _checkData(array $data = []) |
||
246 | |||
247 | /** |
||
248 | * Check the parameters for the message |
||
249 | * |
||
250 | * @param array $parameters Parameters for the GCM request |
||
251 | * @throws Exception |
||
252 | * @return array $parameters |
||
253 | */ |
||
254 | protected function _checkParameters(array $parameters = []) |
||
276 | |||
277 | /** |
||
278 | * Return options for the HTTP request |
||
279 | * |
||
280 | * @throws Exception |
||
281 | * @return array $options |
||
282 | */ |
||
283 | protected function _getHttpOptions() |
||
299 | |||
300 | } |
||
301 |