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