Conditions | 27 |
Paths | 18433 |
Total Lines | 191 |
Code Lines | 123 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
300 | public function onAfterWrite(): void |
||
301 | { |
||
302 | parent::onAfterWrite(); |
||
303 | $config = $this->getOwner()->config(); |
||
304 | $defaults = Defaults::config(); |
||
305 | |||
306 | if ($defaults->get('send_sales_orders_to_unleashed') |
||
307 | && $this->getOwner()->Status == 'Paid' |
||
308 | && !$this->getOwner()->OrderSentToUnleashed) { |
||
309 | // Definitions |
||
310 | $order = $this->getOwner(); |
||
311 | $member = $order->Member(); |
||
312 | $date_paid = new DateTime($order->Paid); |
||
313 | $date_placed = new DateTime($order->Placed); |
||
314 | $body = [ |
||
315 | 'Addresses' => [], |
||
316 | 'Currency' => [], |
||
317 | 'Customer' => [], |
||
318 | 'DiscountRate' => 0, |
||
319 | 'Guid' => $order->Guid, |
||
320 | 'OrderDate' => $date_placed->format('Y-m-d\TH:i:s'), |
||
321 | 'OrderNumber' => $order->Reference, |
||
322 | 'OrderStatus' => $defaults->get('order_status'), |
||
323 | 'PaymentDueDate' => $date_paid->format('Y-m-d\TH:i:s'), |
||
324 | 'PaymentTerm' => $defaults->get('payment_term'), |
||
325 | 'PrintPackingSlipInsteadOfInvoice' => $defaults->get('print_packingslip_instead_of_invoice'), |
||
326 | 'ReceivedDate' => $date_placed->format('Y-m-d\TH:i:s'), |
||
327 | 'SalesOrderLines' => [], |
||
328 | 'SellPriceTier' => ShopConfigExtension::current()->CustomerGroup()->Title, |
||
329 | 'Taxable' => false, |
||
330 | 'Tax' => [], |
||
331 | 'Total' => round(floatval($order->Total()), $config->get('rounding_precision')), |
||
332 | ]; |
||
333 | |||
334 | $body = $this->setBodyAddress($body, $order, 'Postal'); |
||
335 | $body = $this->setBodyAddress($body, $order, 'Physical'); |
||
336 | $body = $this->setBodyCurrencyCode($body, $order); |
||
337 | $body = $this->setBodyCustomerCodeAndName($body, $order); |
||
338 | $body = $this->setBodyDeliveryMethodAndDeliveryName($body, $order, $defaults->get('shipping_modifier_class_name')); |
||
339 | $body = $this->setBodyTaxCode($body, $order, $defaults->get('tax_modifier_class_name')); |
||
340 | $body = $this->setBodySalesOrderLines($body, $order, $defaults->get('tax_modifier_class_name'), $config->get('rounding_precision')); |
||
341 | $body = $this->setBodySubTotalAndTax($body, $order, $defaults->get('tax_modifier_class_name'), $config->get('rounding_precision')); |
||
342 | |||
343 | // Add optional defaults |
||
344 | if ($defaults->get('created_by')) { |
||
345 | $body['CreatedBy'] = $defaults->get('created_by'); |
||
346 | } |
||
347 | |||
348 | if ($defaults->get('customer_type')) { |
||
349 | $body['CustomerType'] = $defaults->get('customer_type'); |
||
350 | } |
||
351 | |||
352 | if ($defaults->get('sales_order_group')) { |
||
353 | $body['SalesOrderGroup'] = $defaults->get('sales_order_group'); |
||
354 | } |
||
355 | |||
356 | if ($defaults->get('source_id')) { |
||
357 | $body['SourceId'] = $defaults->get('source_id'); |
||
358 | } |
||
359 | |||
360 | // add phone number if available |
||
361 | if ($order->BillingAddress()->Phone) { |
||
362 | $body['PhoneNumber'] = $order->BillingAddress()->Phone; |
||
363 | } |
||
364 | |||
365 | // add required date |
||
366 | $date_required = new DateTime($order->Paid); |
||
367 | if ($defaults->get('expected_days_to_deliver')) { |
||
368 | $date_required->modify('+' . $defaults->get('expected_days_to_deliver') . 'day'); |
||
369 | } |
||
370 | $body['RequiredDate'] = $date_required->format('Y-m-d\TH:i:s'); |
||
371 | |||
372 | if ($order->Notes) { |
||
373 | $body['Comments'] = $order->Notes; |
||
374 | } |
||
375 | |||
376 | // Create Member for Guests |
||
377 | if (!$member->exists()) { |
||
378 | $member = Member::create(); |
||
379 | $member->FirstName = $order->FirstName; |
||
380 | $member->Surname = $order->Surname; |
||
381 | $member->Email = $order->getLatestEmail(); |
||
382 | } |
||
383 | |||
384 | // See if New Customer/Guest has previously purchased |
||
385 | if (!$member->Guid) { |
||
386 | $response = UnleashedAPI::sendCall( |
||
387 | 'GET', |
||
388 | 'https://api.unleashedsoftware.com/Customers?contactEmail=' . $member->Email |
||
389 | ); |
||
390 | |||
391 | if ($response->getStatusCode() == 200) { |
||
392 | $contents = (array) json_decode((string) $response->getBody(), true); |
||
393 | $items = $contents['Items']; |
||
394 | if ($items && $items[0]) { |
||
395 | // Email address exists |
||
396 | $member->Guid = $items[0]['Guid']; |
||
397 | } else { |
||
398 | // A Customer is not returned, we have a unique email address. |
||
399 | // Check to see if the Customer Code exists (note that the Customer Code cannot be doubled up) |
||
400 | $response = UnleashedAPI::sendCall( |
||
401 | 'GET', |
||
402 | 'https://api.unleashedsoftware.com/Customers?customerCode=' . $body['CustomerCode'] |
||
403 | ); |
||
404 | |||
405 | if ($response->getStatusCode() == 200) { |
||
406 | $contents = json_decode((string) $response->getBody()->getContents(), true); |
||
407 | $items = $contents['Items']; |
||
408 | if ($items && $items[0]) { |
||
409 | // A Customer Code already exists (and the email address is unique). |
||
410 | // If the address is the same then this is the Customer |
||
411 | if ($this->matchCustomerAddress($items, $order->ShippingAddress())) { |
||
412 | $member->Guid = $items[0]['Guid']; |
||
413 | |||
414 | //Note the existing email address in the Comment |
||
415 | //PUT Customer is not available in Unleashed |
||
416 | if ($body['Comments']) { |
||
417 | $body['Comments'] .= '. '; |
||
418 | } |
||
419 | $body['Comments'] .= _t( |
||
420 | 'UnleashedAPI.addEmailToCustomerComment', |
||
421 | 'Add email to Customer: {email_address}', |
||
422 | '', |
||
423 | ['email_address' => $member->Email] |
||
424 | ); |
||
425 | } else { |
||
426 | // The Customer Code already exists, we have a unique email address, but |
||
427 | // the delivery address is new. |
||
428 | // Therefore, we need to create a new Customer with a unique Customer Code. |
||
429 | $body['CustomerCode'] .= random_int(10000000, 99999999); |
||
430 | } |
||
431 | } |
||
432 | } |
||
433 | } |
||
434 | } |
||
435 | } |
||
436 | |||
437 | if (!$member->Guid) { |
||
438 | // The Customer Code does not exists in Unleashed and the email address is unique |
||
439 | // therefore create in Unleashed |
||
440 | $member->Guid = Utils::createGuid(); |
||
441 | $body_member = [ |
||
442 | 'Addresses' => $body['Addresses'], |
||
443 | 'ContactFirstName' => $member->FirstName, |
||
444 | 'ContactLastName' => $member->Surname, |
||
445 | 'CreatedBy' => $body['CreatedBy'], |
||
446 | 'Currency' => $body['Currency'], |
||
447 | 'CustomerCode' => $body['CustomerCode'], |
||
448 | 'CustomerName' => $body['CustomerName'], |
||
449 | 'CustomerType' => $body['CustomerType'], |
||
450 | 'Email' => $member->Email, |
||
451 | 'Guid' => $member->Guid, |
||
452 | 'PaymentTerm' => $body['PaymentTerm'], |
||
453 | 'PhoneNumber' => $body['PhoneNumber'], |
||
454 | 'PrintPackingSlipInsteadOfInvoice' => $body['PrintPackingSlipInsteadOfInvoice'], |
||
455 | 'SellPriceTier' => $body['SellPriceTier'], |
||
456 | 'SourceId' => $body['SourceId'], |
||
457 | 'Taxable' => $body['Taxable'], |
||
458 | 'TaxCode' => $body['Tax']['TaxCode'] |
||
459 | ]; |
||
460 | |||
461 | foreach ($body_member['Addresses'] as $index => $value) { |
||
462 | $body_member['Addresses'][$index]['IsDefault'] = true; |
||
463 | } |
||
464 | |||
465 | $response = UnleashedAPI::sendCall( |
||
466 | 'POST', |
||
467 | 'https://api.unleashedsoftware.com/Customers/' . $member->Guid, |
||
468 | ['json' => $body_member ] |
||
469 | ); |
||
470 | |||
471 | if ($response->getReasonPhrase() == 'Created' && $order->Member()->exists()) { |
||
472 | $member->write(); |
||
473 | } |
||
474 | } |
||
475 | |||
476 | // Prepare Sales Order data |
||
477 | // Skip if previous calls to Customer have failed and the Guid has not been set |
||
478 | if ($member->Guid) { |
||
479 | $body['Customer']['Guid'] = $member->Guid; |
||
480 | |||
481 | $this->getOwner()->extend('updateUnleashedSalesOrder', $body); |
||
482 | |||
483 | $response = UnleashedAPI::sendCall( |
||
484 | 'POST', |
||
485 | 'https://api.unleashedsoftware.com/SalesOrders/' . $order->Guid, |
||
486 | ['json' => $body] |
||
487 | ); |
||
488 | if ($response->getReasonPhrase() == 'Created') { |
||
489 | $this->getOwner()->OrderSentToUnleashed = DBDatetime::now()->Rfc2822(); |
||
490 | $this->getOwner()->write(); |
||
491 | } |
||
496 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths