CancelOrderItemObserver   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 17.02 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 8
loc 47
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B execute() 8 21 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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