Conditions | 10 |
Paths | 16 |
Total Lines | 70 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 110 |
Changes | 7 | ||
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
100 | private function _sendFirebase(string $deviceToken, string $alert, array $properties, string $projectId, string $credentialsFile, bool $cacheTokens, string $tokenCacheDir, bool $retry=false) |
||
101 | { |
||
102 | $apiurl = sprintf('https://fcm.googleapis.com/v1/projects/%s/messages:send',$projectId); |
||
103 | |||
104 | $fields = [ |
||
105 | 'message' => [ |
||
106 | 'token' => $deviceToken, |
||
107 | 'data' => array(), |
||
108 | "android" => [ |
||
109 | "ttl" => "300s", |
||
110 | ], |
||
111 | ], |
||
112 | ]; |
||
113 | |||
114 | // Add custom properties |
||
115 | foreach ($properties as $k => $v) { |
||
116 | $fields['message']['data'][(string)$k] = (string)$v; |
||
117 | } |
||
118 | // Add message |
||
119 | $fields['message']['data']['text'] = $alert; |
||
120 | |||
121 | try { |
||
122 | $headers = array( |
||
123 | 'Authorization: Bearer ' . $this->getGoogleAccessToken($credentialsFile, $cacheTokens, $tokenCacheDir), |
||
124 | 'Content-Type: application/json', |
||
125 | ); |
||
126 | } catch (\Google\Exception $e) { |
||
127 | throw new Tiqr_Message_Exception_SendFailure(sprintf("Error getting Google access token : %s", $e->getMessage()), true); |
||
128 | } |
||
129 | |||
130 | $payload = json_encode($fields); |
||
131 | $this->logger->debug(sprintf("JSON payload: %s", $payload)); |
||
132 | |||
133 | $ch = curl_init(); |
||
134 | curl_setopt($ch, CURLOPT_URL, $apiurl); |
||
135 | curl_setopt($ch, CURLOPT_POST, true); |
||
136 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||
137 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
138 | curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); |
||
139 | $result = curl_exec($ch); |
||
140 | $errors = curl_error($ch); |
||
141 | $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
142 | $remoteip = curl_getinfo($ch, CURLINFO_PRIMARY_IP); |
||
143 | curl_close($ch); |
||
144 | |||
145 | if ($result === false) { |
||
146 | throw new Tiqr_Message_Exception_SendFailure("Server unavailable", true); |
||
147 | } |
||
148 | |||
149 | if (!empty($errors)) { |
||
150 | throw new Tiqr_Message_Exception_SendFailure("Http error occurred: ". $errors, true); |
||
151 | } |
||
152 | |||
153 | // Wait and retry once in case of a 502 Bad Gateway error |
||
154 | if ($statusCode === 502 && !($retry)) { |
||
155 | $this->logger->warning("Received HTTP 502 Bad Gateway error, retrying once"); |
||
156 | sleep(2); |
||
157 | $this->_sendFirebase($deviceToken, $alert, $properties, $projectId, $credentialsFile, $cacheTokens, $tokenCacheDir, true); |
||
158 | return; |
||
159 | } |
||
160 | |||
161 | if ($statusCode !== 200) { |
||
162 | throw new Tiqr_Message_Exception_SendFailure(sprintf('Invalid status code : %s. Server : %s. Response : "%s".', $statusCode, $remoteip, $result), true); |
||
163 | } |
||
164 | |||
165 | // handle errors, ignoring registration_id's |
||
166 | $response = json_decode($result, true); |
||
167 | foreach ($response as $k => $v) { |
||
168 | if ($k=="error") { |
||
169 | throw new Tiqr_Message_Exception_SendFailure(sprintf("Error in FCM response: %s", $result), true); |
||
170 | } |
||
174 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.