Conditions | 33 |
Paths | > 20000 |
Total Lines | 230 |
Code Lines | 152 |
Lines | 41 |
Ratio | 17.83 % |
Changes | 5 | ||
Bugs | 1 | 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 |
||
170 | function process_moodle($transaction_data, $instanceid, $cid) { |
||
171 | global $CFG,$USER,$DB,$course; |
||
172 | |||
173 | /// Read all the data from pagseguro and get it ready for later; |
||
174 | /// we expect only valid UTF-8 encoding, it is the responsibility |
||
175 | /// of user to set it up properly in pagseguro business account, |
||
176 | /// it is documented in docs wiki. |
||
177 | |||
178 | $data = new stdClass(); |
||
179 | $a = new stdClass(); |
||
180 | |||
181 | $transaction = array(); |
||
182 | |||
183 | $plugin = enrol_get_plugin('pagseguro'); |
||
184 | |||
185 | $userid = (int) isset($USER->id) && !empty($USER->id) ? $USER->id : null; |
||
186 | $courseid = (int) isset($course->id) && !empty($course->id) ? $course->id : $cid; |
||
187 | |||
188 | $transaction_xml = unserialize($transaction_data); |
||
189 | $transaction = json_decode(json_encode(simplexml_load_string($transaction_xml))); |
||
190 | |||
191 | if($transaction) { |
||
192 | foreach ($transaction as $trans_key => $trans_value) { |
||
193 | $trans_key = strtolower($trans_key); |
||
194 | if(!is_object($trans_value)) { |
||
195 | $data->$trans_key = $trans_value; |
||
196 | } else { |
||
197 | foreach($trans_value as $key => $value) { |
||
198 | $key = strtolower($key); |
||
199 | if(is_object($value)) { |
||
200 | foreach($value as $k => $v) { |
||
201 | $k = strtolower($k); |
||
202 | $k = $trans_key.'_'.$key.'_'.$k; |
||
203 | $data->$k = $v; |
||
204 | } |
||
205 | } else { |
||
206 | $key = $trans_key.'_'.$key; |
||
207 | $data->$key = $value; |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | } else { |
||
213 | return false; |
||
214 | } |
||
215 | |||
216 | $data->xmlstring = trim(htmlentities($transaction_xml)); |
||
217 | $data->business = $plugin->get_config('pagsegurobusiness'); |
||
218 | $data->receiver_email = $plugin->get_config('pagsegurobusiness'); |
||
219 | $data->userid = $userid; |
||
220 | $data->courseid = $courseid; |
||
221 | $data->instanceid = $instanceid; |
||
222 | $data->timeupdated = time(); |
||
223 | |||
224 | if(!isset($data->reference) && empty($data->reference)) { |
||
225 | $data->reference = $plugin->get_config('pagsegurobusiness'); |
||
226 | } |
||
227 | |||
228 | // Get the user and course records. |
||
229 | |||
230 | if (!$user = $DB->get_record("user", array("id" => $data->userid))) { |
||
231 | message_pagseguro_error_to_admin("Not a valid user id", $data); |
||
232 | return false; |
||
233 | } |
||
234 | |||
235 | if (!$course = $DB->get_record("course", array("id" => $data->courseid))) { |
||
236 | message_pagseguro_error_to_admin("Not a valid course id", $data); |
||
237 | return false; |
||
238 | } |
||
239 | |||
240 | if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) { |
||
241 | message_pagseguro_error_to_admin("Not a valid context id", $data); |
||
242 | return false; |
||
243 | } |
||
244 | |||
245 | if (!$plugin_instance = $DB->get_record("enrol", array("id" => $data->instanceid, "status" => 0))) { |
||
246 | message_pagseguro_error_to_admin("Not a valid instance id", $data); |
||
247 | return false; |
||
248 | } |
||
249 | |||
250 | /* |
||
251 | Transaction Status - |
||
252 | -- Waiting for Payment - 1 |
||
253 | -- In analysis - 2 |
||
254 | -- PAID - 3 |
||
255 | -- Available - 4 |
||
256 | -- In dispute - 5 |
||
257 | -- Returned - 6 |
||
258 | -- Cancelled - 7 |
||
259 | */ |
||
260 | |||
261 | switch ($data->status) { |
||
262 | case COMMERCE_PAGSEGURO_STATUS_AWAITING: // Awaiting payment. |
||
263 | $data->payment_status = COMMERCE_PAYMENT_STATUS_PENDING; |
||
264 | break; |
||
265 | case COMMERCE_PAGSEGURO_STATUS_IN_ANALYSIS: // Payment in analysis. |
||
266 | $data->payment_status = COMMERCE_PAYMENT_STATUS_PENDING; |
||
267 | break; |
||
268 | case COMMERCE_PAGSEGURO_STATUS_PAID: // Paid. |
||
269 | $data->payment_status = COMMERCE_PAYMENT_STATUS_SUCCESS; |
||
270 | break; |
||
271 | case COMMERCE_PAGSEGURO_STATUS_AVAILABLE: // Available. |
||
272 | $data->payment_status = COMMERCE_PAYMENT_STATUS_SUCCESS; |
||
273 | break; |
||
274 | case COMMERCE_PAGSEGURO_STATUS_DISPUTED: // Payment disputed. |
||
275 | $data->payment_status = COMMERCE_PAYMENT_STATUS_FAILURE; |
||
276 | break; |
||
277 | case COMMERCE_PAGSEGURO_STATUS_REFUNDED: // Payment refunded. |
||
278 | $data->payment_status = COMMERCE_PAYMENT_STATUS_FAILURE; |
||
279 | break; |
||
280 | case COMMERCE_PAGSEGURO_STATUS_CANCELED: // Payment canceled. |
||
281 | $data->payment_status = COMMERCE_PAYMENT_STATUS_FAILURE; |
||
282 | break; |
||
283 | } |
||
284 | |||
285 | if (!in_array($data->status, array(COMMERCE_PAGSEGURO_STATUS_IN_ANALYSIS, COMMERCE_PAGSEGURO_STATUS_PAID, COMMERCE_PAGSEGURO_STATUS_AVAILABLE))) { |
||
286 | $plugin->unenrol_user($plugin_instance, $data->userid); |
||
287 | message_pagseguro_error_to_admin("Status not completed or pending. User unenrolled from course", $data); |
||
288 | $error_returnurl .= "?id={$courseid}&waiting=1"; |
||
289 | header("Location: $error_returnurl"); |
||
290 | } |
||
291 | |||
292 | /*if ($existing = $DB->get_record("enrol_pagseguro", array("txn_id" => $data->txn_id))) { // Make sure this transaction doesn't exist already |
||
293 | message_pagseguro_error_to_admin("Transaction $data->txn_id is being repeated!", $data); |
||
294 | return false; |
||
295 | }*/ |
||
296 | |||
297 | $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); |
||
298 | |||
299 | // Check that amount paid is the correct amount |
||
300 | if ( (float) $plugin_instance->cost <= 0 ) { |
||
301 | $cost = (float) $plugin->get_config('cost'); |
||
302 | } else { |
||
303 | $cost = (float) $plugin_instance->cost; |
||
304 | } |
||
305 | |||
306 | if ($data->grossamount < $cost) { |
||
307 | $cost = format_float($cost, 2); |
||
308 | message_pagseguro_error_to_admin("Amount paid is not enough ($data->payment_gross < $cost))", $data); |
||
309 | return false; |
||
310 | } |
||
311 | |||
312 | // All clear! |
||
313 | $DB->insert_record("enrol_pagseguro", $data); |
||
314 | |||
315 | if ($plugin_instance->enrolperiod) { |
||
316 | $timestart = time(); |
||
317 | $timeend = $timestart + $plugin_instance->enrolperiod; |
||
318 | } else { |
||
319 | $timestart = 0; |
||
320 | $timeend = 0; |
||
321 | } |
||
322 | |||
323 | // Enrol user |
||
324 | $plugin->enrol_user($plugin_instance, $userid, $plugin_instance->roleid, $timestart, $timeend); |
||
325 | |||
326 | // Pass $view=true to filter hidden caps if the user cannot see them |
||
327 | View Code Duplication | if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC', |
|
328 | '', '', '', '', false, true)) { |
||
329 | $users = sort_by_roleassignment_authority($users, $context); |
||
330 | $teacher = array_shift($users); |
||
331 | } else { |
||
332 | $teacher = get_admin(); |
||
333 | } |
||
334 | |||
335 | $mailstudents = $plugin->get_config('mailstudents'); |
||
336 | $mailteachers = $plugin->get_config('mailteachers'); |
||
337 | $mailadmins = $plugin->get_config('mailadmins'); |
||
338 | $shortname = format_string($course->shortname, true, array('context' => $context)); |
||
339 | |||
340 | View Code Duplication | if (!empty($mailstudents)) { |
|
341 | $a->coursename = format_string($course->fullname, true, array('context' => $coursecontext)); |
||
342 | $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id"; |
||
343 | |||
344 | $eventdata = new stdClass(); |
||
345 | $eventdata->modulename = 'moodle'; |
||
346 | $eventdata->component = 'enrol_pagseguro'; |
||
347 | $eventdata->name = 'pagseguro_enrolment'; |
||
348 | $eventdata->userfrom = $teacher; |
||
349 | $eventdata->userto = $user; |
||
350 | $eventdata->subject = get_string("enrolmentnew", 'enrol', $shortname); |
||
351 | $eventdata->fullmessage = get_string('welcometocoursetext', '', $a); |
||
352 | $eventdata->fullmessageformat = FORMAT_PLAIN; |
||
353 | $eventdata->fullmessagehtml = ''; |
||
354 | $eventdata->smallmessage = ''; |
||
355 | message_send($eventdata); |
||
356 | } |
||
357 | |||
358 | View Code Duplication | if (!empty($mailteachers)) { |
|
359 | $a->course = format_string($course->fullname, true, array('context' => $coursecontext)); |
||
360 | $a->user = fullname($user); |
||
361 | |||
362 | $eventdata = new stdClass(); |
||
363 | $eventdata->modulename = 'moodle'; |
||
364 | $eventdata->component = 'enrol_pagseguro'; |
||
365 | $eventdata->name = 'pagseguro_enrolment'; |
||
366 | $eventdata->userfrom = $user; |
||
367 | $eventdata->userto = $teacher; |
||
368 | $eventdata->subject = get_string("enrolmentnew", 'enrol', $shortname); |
||
369 | $eventdata->fullmessage = get_string('enrolmentnewuser', 'enrol', $a); |
||
370 | $eventdata->fullmessageformat = FORMAT_PLAIN; |
||
371 | $eventdata->fullmessagehtml = ''; |
||
372 | $eventdata->smallmessage = ''; |
||
373 | message_send($eventdata); |
||
374 | } |
||
375 | |||
376 | if (!empty($mailadmins)) { |
||
377 | $a->course = format_string($course->fullname, true, array('context' => $coursecontext)); |
||
378 | $a->user = fullname($user); |
||
379 | $admins = get_admins(); |
||
380 | foreach ($admins as $admin) { |
||
381 | $eventdata = new stdClass(); |
||
382 | $eventdata->modulename = 'moodle'; |
||
383 | $eventdata->component = 'enrol_pagseguro'; |
||
384 | $eventdata->name = 'pagseguro_enrolment'; |
||
385 | $eventdata->userfrom = $user; |
||
386 | $eventdata->userto = $admin; |
||
387 | $eventdata->subject = get_string("enrolmentnew", 'enrol', $shortname); |
||
388 | $eventdata->fullmessage = get_string('enrolmentnewuser', 'enrol', $a); |
||
389 | $eventdata->fullmessageformat = FORMAT_PLAIN; |
||
390 | $eventdata->fullmessagehtml = ''; |
||
391 | $eventdata->smallmessage = ''; |
||
392 | |||
393 | message_send($eventdata); |
||
394 | } |
||
395 | } |
||
396 | |||
397 | $success_returnurl = $CFG->wwwroot.'/enrol/pagseguro/return.php?id='.$courseid; |
||
398 | header("Location: $success_returnurl"); |
||
399 | } |
||
400 | |||
422 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.