OrderPaymentPayObserver::execute()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 5.0046

Importance

Changes 0
Metric Value
cc 5
nc 7
nop 1
dl 0
loc 66
ccs 33
cts 35
cp 0.9429
crap 5.0046
rs 8.4307
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
namespace Stockbase\Integration\Model\Observer;
3
4
use Magento\Framework\Event\Observer;
5
use Magento\Framework\Event\ObserverInterface;
6
use Magento\Framework\ObjectManagerInterface;
7
use Magento\Sales\Api\Data\OrderInterface;
8
use Magento\Sales\Api\Data\OrderItemInterface;
9
use Magento\Sales\Api\OrderRepositoryInterface;
10
use Stockbase\Integration\StockbaseApi\Client\StockbaseClientFactory;
11
use Stockbase\Integration\Model\Config\StockbaseConfiguration;
12
use Stockbase\Integration\Model\Inventory\StockbaseStockManagement;
13
use Stockbase\Integration\Model\OrderedItem as StockbaseOrderedItem;
14
use Stockbase\Integration\Model\StockItemReserve;
15
16
/**
17
 * Class OrderPaymentPayObserver
18
 */
19
class OrderPaymentPayObserver implements ObserverInterface
20
{
21
    /**
22
     * @var StockbaseConfiguration
23
     */
24
    private $stockbaseConfiguration;
25
26
    /**
27
     * @var StockbaseClientFactory
28
     */
29
    private $stockbaseClientFactory;
30
31
    /**
32
     * @var StockbaseStockManagement
33
     */
34
    private $stockbaseStockManagement;
35
36
    /**
37
     * @var ObjectManagerInterface
38
     */
39
    private $objectManager;
40
41
    /**
42
     * OrderPaymentPayObserver constructor.
43
     * @param StockbaseConfiguration   $stockbaseConfiguration
44
     * @param StockbaseStockManagement $stockbaseStockManagement
45
     * @param StockbaseClientFactory   $stockbaseClientFactory
46
     * @param ObjectManagerInterface   $objectManager
47
     */
48 3
    public function __construct(
49
        StockbaseConfiguration $stockbaseConfiguration,
50
        StockbaseStockManagement $stockbaseStockManagement,
51
        StockbaseClientFactory $stockbaseClientFactory,
52
        ObjectManagerInterface $objectManager
53
    ) {
54 3
        $this->stockbaseConfiguration = $stockbaseConfiguration;
55 3
        $this->stockbaseClientFactory = $stockbaseClientFactory;
56 3
        $this->stockbaseStockManagement = $stockbaseStockManagement;
57 3
        $this->objectManager = $objectManager;
58 3
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 3
    public function execute(Observer $observer)
64
    {
65 3
        if (!$this->stockbaseConfiguration->isModuleEnabled()) {
66 1
            return $this;
67
        }
68
69
        /** @var \Magento\Sales\Api\Data\InvoiceInterface $invoice */
70 2
        $invoice = $observer->getData('invoice');
71
72
        // since InvoiceInterface doesn't explicitly define getOrder(), we have to do this...
73 2
        if (\method_exists($invoice, 'getOrder')) {
74
            /** @var \Magento\Sales\Api\Data\OrderInterface $order */
75 2
            $order = $invoice->getOrder();
76
        } else {
77
            /** @var OrderRepositoryInterface $repository */
78
            $repository = $this->objectManager->get(OrderRepositoryInterface::class);
79
            $order = $repository->get($invoice->getOrderId());
80
        }
81
82 2
        $quoteItemIds = \array_map(function (OrderItemInterface $item) {
83 2
            return $item->getQuoteItemId();
84 2
        }, (array) $order->getAllItems());
85
86
        /** @var StockItemReserve[] $reservedStockbaseItems */
87 2
        $reservedStockbaseItems = $this->stockbaseStockManagement->getReserveForQuoteItem($quoteItemIds);
88
89 2
        if (empty($reservedStockbaseItems)) {
90 1
            return $this;
91
        }
92
93 1
        $client = $this->stockbaseClientFactory->create();
94 1
        $result = $client->createOrder($order, $reservedStockbaseItems);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
95
96 1
        foreach ($reservedStockbaseItems as $reserve) {
97 1
            $this->addStatusHistoryComment($order, __(
98 1
                'Item with EAN "%1" (%2 pc.) has been ordered from Stockbase.',
99 1
                $reserve->getEan(),
100 1
                $reserve->getAmount()
101
            ));
102
            //$order->save();
103
104
            /** @var StockbaseOrderedItem $stockbaseOrderedItem */
105 1
            $stockbaseOrderedItem = $this->objectManager->create(StockbaseOrderedItem::class);
106 1
            $stockbaseOrderedItem->setOrderId($order->getId());
107 1
            $stockbaseOrderedItem->setOrderItemId($reserve->getOrderItemId());
108 1
            $stockbaseOrderedItem->setProductId($reserve->getProductId());
109 1
            $stockbaseOrderedItem->setEan($reserve->getEan());
110 1
            $stockbaseOrderedItem->setAmount($reserve->getAmount());
111 1
            $stockbaseOrderedItem->setCreatedAt((new \DateTime())->format('Y-m-d H:i:s'));
112 1
            $stockbaseOrderedItem->save();
113
114
            // Decrement local Stockbase stock amount
115 1
            $this->stockbaseStockManagement->updateStockAmount($reserve->getEan(), $reserve->getAmount(), '-');
116
117
            // Release the reserve
118 1
            $this->stockbaseStockManagement->releaseReserve($reserve);
119
120 1
            $this->addStatusHistoryComment($order, __(
121 1
                'Local Stockbase stock index for item with EAN "%1" has been updated.',
122 1
                $reserve->getEan()
123
            ));
124
            //$order->save();
125
        }
126
127 1
        return $this;
128
    }
129
130
    /**
131
     * Safe add status history comment.
132
     *
133
     * @param OrderInterface $order
134
     * @param string $comment
135
     * @param $status
136
     * @return void
137
     */
138 1
    private function addStatusHistoryComment(OrderInterface $order, string $comment, $status = false)
139
    {
140 1
        if (!$order instanceof \Magento\Sales\Model\Order) {
0 ignored issues
show
Bug introduced by
The class Magento\Sales\Model\Order does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
141
            return;
142
        }
143
144 1
        $order->addStatusHistoryComment($comment, $status);
145 1
    }
146
}
147