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