Conditions | 8 |
Paths | 7 |
Total Lines | 54 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
60 | private function _sendFirebase($deviceToken, $alert, $challenge, $apiKey, $retry=false) |
||
61 | { |
||
62 | $msg = array( |
||
63 | 'challenge' => $challenge, |
||
64 | 'text' => $alert, |
||
65 | ); |
||
66 | |||
67 | $fields = array( |
||
68 | 'registration_ids' => array($deviceToken), |
||
69 | 'data' => $msg, |
||
70 | 'time_to_live' => 300, |
||
71 | ); |
||
72 | |||
73 | $headers = array( |
||
74 | 'Authorization: key=' . $apiKey, |
||
75 | 'Content-Type: application/json', |
||
76 | ); |
||
77 | |||
78 | $ch = curl_init(); |
||
79 | curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send'); |
||
80 | curl_setopt($ch, CURLOPT_POST, true); |
||
81 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||
82 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
83 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); |
||
84 | $result = curl_exec($ch); |
||
85 | $errors = curl_error($ch); |
||
86 | $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
87 | $remoteip = curl_getinfo($ch,CURLINFO_PRIMARY_IP); |
||
88 | curl_close($ch); |
||
89 | |||
90 | if ($result === false) { |
||
91 | throw new Tiqr_Message_Exception_SendFailure("Server unavailable", true); |
||
92 | } |
||
93 | |||
94 | if (!empty($errors)) { |
||
95 | throw new Tiqr_Message_Exception_SendFailure("Http error occurred: ". $errors, true); |
||
96 | } |
||
97 | |||
98 | // Wait and retry once in case of a 502 Bad Gateway error |
||
99 | if ($statusCode === 502 && !($retry)) { |
||
100 | sleep(2); |
||
101 | $this->_sendFirebase($deviceToken, $alert, $challenge, $apiKey, true); |
||
102 | return; |
||
103 | } |
||
104 | |||
105 | if ($statusCode !== 200) { |
||
106 | throw new Tiqr_Message_Exception_SendFailure(sprintf('Invalid status code : %s. Server : %s. Response : "%s".', $statusCode, $remoteip, $result), true); |
||
|
|||
107 | } |
||
108 | |||
109 | // handle errors, ignoring registration_id's |
||
110 | $response = json_decode($result, true); |
||
111 | foreach ($response['results'] as $k => $v) { |
||
112 | if (isset($v['error'])) { |
||
113 | throw new Tiqr_Message_Exception_SendFailure("Error in GCM response: " . $v['error'], true); |
||
114 | } |
||
118 |