Completed
Push — master ( ec1dbf...510cdb )
by Gabriel
04:59 queued 51s
created

RevertQuoteInventoryObserver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 10.81 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
dl 8
loc 74
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
D execute() 8 44 9

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\CatalogInventory\Observer\ProductQty;
7
use Magento\CatalogInventory\Api\StockManagementInterface;
8
use Magento\CatalogInventory\Model\Indexer\Stock\Processor as IndexStockProcessor;
9
use Magento\Catalog\Model\Indexer\Product\Price\Processor as ProductPriceProcessor;
10
use Magento\Framework\Event\Observer as EventObserver;
11
use Stockbase\Integration\Model\Inventory\StockbaseStockManagement;
12
13
/**
14
 * Class RevertQuoteInventoryObserver
15
 */
16
class RevertQuoteInventoryObserver extends \Magento\CatalogInventory\Observer\RevertQuoteInventoryObserver
17
{
18
    /**
19
     * @var StockbaseStockManagement
20
     */
21
    private $stockbaseStockManagement;
22
23
    /**
24
     * RevertQuoteInventoryObserver constructor.
25
     * @param ProductQty               $productQty
26
     * @param StockManagementInterface $stockManagement
27
     * @param IndexStockProcessor      $stockIndexerProcessor
28
     * @param ProductPriceProcessor    $priceIndexer
29
     * @param StockbaseStockManagement $stockbaseStockManagement
30
     */
31 3
    public function __construct(
32
        ProductQty $productQty,
33
        StockManagementInterface $stockManagement,
34
        IndexStockProcessor $stockIndexerProcessor,
35
        ProductPriceProcessor $priceIndexer,
36
        StockbaseStockManagement $stockbaseStockManagement
37
    ) {
38 3
        parent::__construct($productQty, $stockManagement, $stockIndexerProcessor, $priceIndexer);
39 3
        $this->stockbaseStockManagement = $stockbaseStockManagement;
40 3
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function execute(EventObserver $observer)
46
    {
47
        /** @var \Magento\Quote\Model\Quote $quote */
48 3
        $quote = $observer->getEvent()->getQuote();
49
50 3
        $items = [];
51 3
        foreach ($quote->getAllItems() as $item) {
52 2
            $productId = $item->getProductId();
53 2
            if (!$productId) {
54
                continue;
55
            }
56 2
            $children = $item->getChildrenItems() ?: [$item];
57 2
            foreach ($children as $childItem) {
58 2
                $productId = $childItem->getProductId();
59 2
                if (!$productId) {
60
                    continue;
61
                }
62
63 2
                $amount = $childItem->getTotalQty();
64 2
                $reserve = $this->stockbaseStockManagement->getReserveForQuoteItem($childItem->getId());
65 2 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...
66 2
                    $reserve = reset($reserve);
67 2
                    $amount -= $reserve->getAmount();
68
                    //if ($amount != $reserve->getMagentoStockAmount()) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
69
                    //    throw new \Exception('Invalid quote inventory revert.'); //TODO: Enable additional checks
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
                    //}
71 2
                    $this->stockbaseStockManagement->releaseReserve($reserve);
0 ignored issues
show
Security Bug introduced by
It seems like $reserve defined by reset($reserve) on line 66 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...
72
                }
73 2
                if (!isset($items[$productId])) {
74 2
                    $items[$productId] = 0;
75
                }
76 2
                $items[$productId] += $amount;
77
            }
78
        }
79
        
80 3
        $this->stockManagement->revertProductsSale($items, $quote->getStore()->getWebsiteId());
81 3
        $productIds = array_keys($items);
82 3
        if (!empty($productIds)) {
83 2
            $this->stockIndexerProcessor->reindexList($productIds);
84 2
            $this->priceIndexer->reindexList($productIds);
85
        }
86
        // Clear flag, so if order placement retried again with success - it will be processed
87 3
        $quote->setInventoryProcessed(false);
88 3
    }
89
}
90