CancelOrderItemObserver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 1
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
4
namespace Stockbase\Integration\Model\Observer;
5
6
use Magento\Catalog\Model\Indexer\Product\Price\Processor as ProductPriceProcessor;
7
use Magento\CatalogInventory\Api\StockManagementInterface;
8
use Magento\Framework\Event\Observer as EventObserver;
9
use Stockbase\Integration\Model\Inventory\StockbaseStockManagement;
10
11
/**
12
 * Class CancelOrderItemObserver
13
 */
14
class CancelOrderItemObserver extends \Magento\CatalogInventory\Observer\CancelOrderItemObserver
15
{
16
    /**
17
     * @var StockbaseStockManagement
18
     */
19
    private $stockbaseStockManagement;
20
21
    /**
22
     * @param StockManagementInterface $stockManagement
23
     * @param ProductPriceProcessor    $priceIndexer
24
     * @param StockbaseStockManagement $stockbaseStockManagement
25
     */
26 3
    public function __construct(
27
        StockManagementInterface $stockManagement,
28
        ProductPriceProcessor $priceIndexer,
29
        StockbaseStockManagement $stockbaseStockManagement
30
    ) {
31 3
        parent::__construct($stockManagement, $priceIndexer);
32
33 3
        $this->stockbaseStockManagement = $stockbaseStockManagement;
34 3
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 3
    public function execute(EventObserver $observer)
40
    {
41
        /** @var \Magento\Sales\Model\Order\Item $item */
42 3
        $item = $observer->getEvent()->getItem();
43
        
44 3
        $children = $item->getChildrenItems();
45 3
        $qty = $item->getQtyOrdered() - max($item->getQtyShipped(), $item->getQtyInvoiced()) - $item->getQtyCanceled();
46 3
        if ($item->getId() && $item->getProductId() && empty($children) && $qty) {
47 3
            $reserve = $this->stockbaseStockManagement->getReserveForQuoteItem($item->getQuoteItemId());
48 3 View Code Duplication
            if (!empty($reserve)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49 3
                $reserve = reset($reserve);
50 3
                $qty -= $reserve->getAmount();
51
                //if ($qty != $reserve->getMagentoStockAmount()) {
52
                //    throw new \Exception('Invalid quote inventory revert.'); //TODO: Enable additional checks
53
                //}
54 3
                $this->stockbaseStockManagement->releaseReserve($reserve);
0 ignored issues
show
Security Bug introduced by
It seems like $reserve defined by reset($reserve) on line 49 can also be of type false; however, Stockbase\Integration\Mo...ement::releaseReserve() does only seem to accept object<Stockbase\Integra...Model\StockItemReserve>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
55
            }
56 3
            $this->stockManagement->backItemQty($item->getProductId(), $qty, $item->getStore()->getWebsiteId());
57
        }
58 3
        $this->priceIndexer->reindexRow($item->getProductId());
59 3
    }
60
}
61