Skip to content

Commit

Permalink
3.6.1: MAG-470: Magento Code Standard Validation
Browse files Browse the repository at this point in the history
Fix all warnings on MCSV Tool
  • Loading branch information
ebanolopes committed Mar 11, 2020
1 parent 0724b88 commit 009e89a
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 164 deletions.
155 changes: 155 additions & 0 deletions Helper/OrderHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace Signifyd\Connect\Helper;

use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Invoice;

class OrderHelper
{
/**
* @param Order $order
* @return string
*/
public function getCannotHoldReason(Order $order)
{
$notHoldableStates = [
Order::STATE_CANCELED,
Order::STATE_PAYMENT_REVIEW,
Order::STATE_COMPLETE,
Order::STATE_CLOSED,
Order::STATE_HOLDED
];

if ($order->getState() == Order::STATE_HOLDED) {
$completeCase = true;
}

if (in_array($order->getState(), $notHoldableStates)) {
$reason = "order is on {$order->getState()} state";
} elseif ($order->getActionFlag(Order::ACTION_FLAG_HOLD) === false) {
$reason = "order action flag is set to do not hold";
} else {
$reason = "unknown reason";
}

return $reason;
}

/**
* @param Order $order
* @return string
*/
public function getCannotUnholdReason(Order $order)
{
if ($order->getState() != Order::STATE_HOLDED && $order->isPaymentReview() == false) {
$reason = "order is not holded";
$completeCase = true;
} elseif ($order->isPaymentReview()) {
$reason = 'order is in payment review';
} elseif ($order->getActionFlag(Order::ACTION_FLAG_UNHOLD) === false) {
$reason = "order action flag is set to do not unhold";
} else {
$reason = "unknown reason";
}

return $reason;
}

/**
* @param Order $order
* @return string
*/
public function getCannotCancelReason(Order $order)
{
$notCancelableStates = [
Order::STATE_CANCELED,
Order::STATE_PAYMENT_REVIEW,
Order::STATE_COMPLETE,
Order::STATE_CLOSED,
Order::STATE_HOLDED
];

if (in_array($order->getState(), $notCancelableStates)) {
$reason = "order is on {$order->getState()} state";
} elseif (!$order->canReviewPayment() && $order->canFetchPaymentReviewUpdate()) {
$reason = "conflict with payment review";
} elseif ($order->getActionFlag(Order::ACTION_FLAG_CANCEL) === false) {
$reason = "order action flag is set to do not cancel";
} else {
$allInvoiced = true;
foreach ($order->getAllItems() as $item) {
if ($item->getQtyToInvoice()) {
$allInvoiced = false;
break;
}
}
if ($allInvoiced) {
$reason = "all order items are invoiced";
$completeCase = true;
} else {
$reason = "unknown reason";
}
}

return $reason;
}

/**
* @param Order $order
* @return string
*/
public function getCannotInvoiceReason(Order $order)
{
$notInvoiceableStates = [
Order::STATE_CANCELED,
Order::STATE_PAYMENT_REVIEW,
Order::STATE_COMPLETE,
Order::STATE_CLOSED,
Order::STATE_HOLDED
];

if (in_array($order->getState(), $notInvoiceableStates)) {
$reason = "order is on {$order->getState()} state";
} elseif ($order->getActionFlag(Order::ACTION_FLAG_INVOICE) === false) {
$reason = "order action flag is set to do not invoice";
} else {
$canInvoiceAny = false;

foreach ($order->getAllItems() as $item) {
if ($item->getQtyToInvoice() > 0 && !$item->getLockedDoInvoice()) {
$canInvoiceAny = true;
break;
}
}

if ($canInvoiceAny) {
$reason = "unknown reason";
} else {
$reason = "no items can be invoiced";
$completeCase = true;
}
}

return $reason;
}

/**
* @param Invoice $invoice
* @return bool
* @throws LocalizedException
*/
public function isInvoiceValid(Invoice $invoice)
{
if ($invoice->isEmpty()) {
throw new LocalizedException(__('failed to generate invoice'));
}

if ($invoice->getTotalQty() == 0) {
throw new LocalizedException(__('no items found to invoice'));
}

return true;
}
}
128 changes: 18 additions & 110 deletions Model/Casedata.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\Sales\Model\Order;
use Signifyd\Connect\Logger\Logger;
use Magento\Framework\Serialize\SerializerInterface;
use Signifyd\Connect\Helper\OrderHelper;

/**
* ORM model declaration for case data
Expand Down Expand Up @@ -76,6 +77,11 @@ class Casedata extends AbstractModel
*/
protected $serializer;

/**
* @var OrderHelper
*/
protected $orderHelper;

/**
* Casedata constructor.
* @param Context $context
Expand All @@ -84,6 +90,7 @@ class Casedata extends AbstractModel
* @param InvoiceService $invoiceService
* @param Logger
* @param SerializerInterface $serializer
* @param OrderHelper $orderHelper
*/
public function __construct(
Context $context,
Expand All @@ -94,7 +101,8 @@ public function __construct(
ObjectManagerInterface $objectManager,
\Magento\Sales\Model\OrderFactory $orderFactory,
Logger $logger,
SerializerInterface $serializer
SerializerInterface $serializer,
OrderHelper $orderHelper
) {
$this->configHelper = $configHelper;
$this->invoiceService = $invoiceService;
Expand All @@ -103,6 +111,7 @@ public function __construct(
$this->orderFactory = $orderFactory;
$this->logger = $logger;
$this->serializer = $serializer;
$this->orderHelper = $orderHelper;

parent::__construct($context, $registry);
}
Expand Down Expand Up @@ -230,31 +239,10 @@ public function updateOrder($caseData, $orderAction, $case)
$order->addCommentToStatusHistory($message);
}
} else {
$notHoldableStates = [
Order::STATE_CANCELED,
Order::STATE_PAYMENT_REVIEW,
Order::STATE_COMPLETE,
Order::STATE_CLOSED,
Order::STATE_HOLDED
];

if ($order->getState() == Order::STATE_HOLDED) {
$completeCase = true;
}

if (in_array($order->getState(), $notHoldableStates)) {
$reason = "order is on {$order->getState()} state";
} elseif ($order->getActionFlag(Order::ACTION_FLAG_HOLD) === false) {
$reason = "order action flag is set to do not hold";
} else {
$reason = "unknown reason";
}

$reason = $this->orderHelper->getCannotHoldReason($order);
$message = "Order {$order->getIncrementId()} can not be held because {$reason}";
$this->logger->debug($message, ['entity' => $case]);

$orderAction['action'] = false;

$order->addStatusHistoryComment("Signifyd: order cannot be updated to on hold, {$reason}");
}
break;
Expand All @@ -276,16 +264,7 @@ public function updateOrder($caseData, $orderAction, $case)
$order->addStatusHistoryComment("Signifyd: order status cannot be updated, {$e->getMessage()}");
}
} else {
if ($order->getState() != Order::STATE_HOLDED && $order->isPaymentReview() == false) {
$reason = "order is not holded";
$completeCase = true;
} elseif ($order->isPaymentReview()) {
$reason = 'order is in payment review';
} elseif ($order->getActionFlag(Order::ACTION_FLAG_UNHOLD) === false) {
$reason = "order action flag is set to do not unhold";
} else {
$reason = "unknown reason";
}
$reason = $this->orderHelper->getCannotUnholdReason($order);

$message = "Order {$order->getIncrementId()} ({$order->getState()} > {$order->getStatus()}) " .
"can not be removed from hold because {$reason}. " .
Expand Down Expand Up @@ -318,41 +297,10 @@ public function updateOrder($caseData, $orderAction, $case)
$order->addStatusHistoryComment("Signifyd: order cannot be canceled, {$e->getMessage()}");
}
} else {
$notCancelableStates = [
Order::STATE_CANCELED,
Order::STATE_PAYMENT_REVIEW,
Order::STATE_COMPLETE,
Order::STATE_CLOSED,
Order::STATE_HOLDED
];

if (in_array($order->getState(), $notCancelableStates)) {
$reason = "order is on {$order->getState()} state";
} elseif (!$order->canReviewPayment() && $order->canFetchPaymentReviewUpdate()) {
$reason = "conflict with payment review";
} elseif ($order->getActionFlag(Order::ACTION_FLAG_CANCEL) === false) {
$reason = "order action flag is set to do not cancel";
} else {
$allInvoiced = true;
foreach ($order->getAllItems() as $item) {
if ($item->getQtyToInvoice()) {
$allInvoiced = false;
break;
}
}
if ($allInvoiced) {
$reason = "all order items are invoiced";
$completeCase = true;
} else {
$reason = "unknown reason";
}
}

$reason = $this->orderHelper->getCannotCancelReason($order);
$message = "Order {$order->getIncrementId()} cannot be canceled because {$reason}";
$this->logger->debug($message, ['entity' => $case]);

$orderAction['action'] = false;

$order->addStatusHistoryComment("Signifyd: order cannot be canceled, {$reason}");
}

Expand All @@ -371,13 +319,7 @@ public function updateOrder($caseData, $orderAction, $case)
/** @var \Magento\Sales\Model\Order\Invoice $invoice */
$invoice = $this->invoiceService->prepareInvoice($order);

if ($invoice->isEmpty()) {
throw new \Exception('failed to generate invoice');
}

if ($invoice->getTotalQty() == 0) {
throw new \Exception('no items found to invoice');
}
$this->orderHelper->isInvoiceValid($invoice);

$invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_ONLINE);
$invoice->addComment('Signifyd: Automatic invoice');
Expand Down Expand Up @@ -414,41 +356,10 @@ public function updateOrder($caseData, $orderAction, $case)

$completeCase = true;
} else {
$notInvoiceableStates = [
Order::STATE_CANCELED,
Order::STATE_PAYMENT_REVIEW,
Order::STATE_COMPLETE,
Order::STATE_CLOSED,
Order::STATE_HOLDED
];

if (in_array($order->getState(), $notInvoiceableStates)) {
$reason = "order is on {$order->getState()} state";
} elseif ($order->getActionFlag(Order::ACTION_FLAG_INVOICE) === false) {
$reason = "order action flag is set to do not invoice";
} else {
$canInvoiceAny = false;

foreach ($order->getAllItems() as $item) {
if ($item->getQtyToInvoice() > 0 && !$item->getLockedDoInvoice()) {
$canInvoiceAny = true;
break;
}
}

if ($canInvoiceAny) {
$reason = "unknown reason";
} else {
$reason = "no items can be invoiced";
$completeCase = true;
}
}

$reason = $this->orderHelper->getCannotInvoiceReason($order);
$message = "Order {$order->getIncrementId()} can not be invoiced because {$reason}";
$this->logger->debug($message, ['entity' => $case]);

$orderAction['action'] = false;

$order->addStatusHistoryComment("Signifyd: unable to create invoice: {$reason}");

if ($order->canHold()) {
Expand Down Expand Up @@ -545,6 +456,7 @@ public function getEntries($index = null)
try {
$entries = $this->serializer->unserialize($entries);
} catch (\InvalidArgumentException $e) {
$entries = [];
}
}

Expand All @@ -568,11 +480,7 @@ public function setEntries($index, $value = null)
$entries[$index] = $value;
}

try {
$entries = $this->serializer->serialize($entries);
} catch (\InvalidArgumentException $e) {
}

$entries = $this->serializer->serialize($entries);
$this->setData('entries_text', $entries);

return $this;
Expand All @@ -583,7 +491,7 @@ public function isHoldReleased()
$holdReleased = $this->getEntries('hold_released');
return ($holdReleased == 1) ? true : false;
}

public function getPositiveAction()
{
if ($this->isHoldReleased()) {
Expand Down
Loading

0 comments on commit 009e89a

Please sign in to comment.