Completed
Push — master ( f8a83a...9a4ef2 )
by Gabriel
14s
created

StockbaseStockManagement::getStockbaseEan()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 9.0608

Importance

Changes 0
Metric Value
cc 9
nc 9
nop 1
dl 0
loc 25
ccs 10
cts 11
cp 0.9091
crap 9.0608
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
4
namespace Stockbase\Integration\Model\Inventory;
5
6
use Magento\Catalog\Api\ProductRepositoryInterface;
7
use Magento\Catalog\Model\Product;
8
use Magento\CatalogInventory\Api\StockRegistryInterface;
9
use Magento\Framework\Exception\NoSuchEntityException;
10
use Magento\Framework\ObjectManagerInterface;
11
use Magento\Quote\Model\Quote\Item as QuoteItem;
12
use Stockbase\Integration\Model\Config\StockbaseConfiguration;
13
use Stockbase\Integration\Model\ResourceModel\StockItem as StockItemResource;
14
use Stockbase\Integration\Model\ResourceModel\StockItemReserve\Collection as StockItemReserveCollection;
15
use Stockbase\Integration\Model\StockItemReserve;
16
17
/**
18
 * Class StockbaseStockManagement
19
 */
20
class StockbaseStockManagement
21
{
22
    /**
23
     * @var StockRegistryInterface
24
     */
25
    private $stockRegistry;
26
    /**
27
     * @var StockbaseConfiguration
28
     */
29
    private $config;
30
    /**
31
     * @var ProductRepositoryInterface
32
     */
33
    private $productRepository;
34
    /**
35
     * @var StockItemResource
36
     */
37
    private $stockItemResource;
38
    /**
39
     * @var ObjectManagerInterface
40
     */
41
    private $objectManager;
42
43
    /**
44
     * StockbaseStockManagement constructor.
45
     * @param StockRegistryInterface     $stockRegistry
46
     * @param StockbaseConfiguration     $config
47
     * @param ProductRepositoryInterface $productRepository
48
     * @param StockItemResource          $stockItemResource
49 9
     * @param ObjectManagerInterface     $objectManager
50
     */
51
    public function __construct(
52
        StockRegistryInterface $stockRegistry,
53
        StockbaseConfiguration $config,
54
        ProductRepositoryInterface $productRepository,
55
        StockItemResource $stockItemResource,
56
        ObjectManagerInterface $objectManager
57 9
    ) {
58 9
59 9
        $this->stockRegistry = $stockRegistry;
60 9
        $this->config = $config;
61 9
        $this->productRepository = $productRepository;
62 9
        $this->stockItemResource = $stockItemResource;
63
        $this->objectManager = $objectManager;
64
    }
65
66
    /**
67
     * Gets the amount of items available in the Stockbase stock.
68
     *
69
     * @param int $productId
70 1
     * @return float
71
     */
72 1
    public function getStockbaseStockAmount($productId)
73
    {
74 1
        $qty = 0;
75 1
        
76 1
        $ean = $this->getStockbaseEan($productId);
77
        if (!empty($ean)) {
78
            $qty += max($this->stockItemResource->getNotReservedStockAmount($ean), 0);
79 1
        }
80
        
81
        return $qty;
82
    }
83
84
    /**
85
     * Gets the Stockbase EAN for given product (if any).
86
     *
87
     * @param int $productId
88 4
     * @return string|null
89
     */
90 4
    public function getStockbaseEan($productId)
91 4
    {
92 4
        if ($this->config->isModuleEnabled()) {
93 4
            $stockItem = $this->stockRegistry->getStockItem($productId);
94
            if ($stockItem->getManageStock() &&
95 4
                $stockItem->getBackorders() == \Magento\CatalogInventory\Model\Stock::BACKORDERS_NO
96 4
            ) {
97 4
                try {
98
                    /** @var Product $product */
99 4
                    $product = $this->productRepository->getById($productId);
100 4
101
                    if (!$product->isComposite() && !$product->isVirtual()) {
102
                        $ean = $product->getData($this->config->getEanFieldName());
103
                        if (!empty($ean) && $product->getData('stockbase_product')) {
104
                            return $ean;
105
                        }
106
                    }
107
                } catch (NoSuchEntityException $e) {
0 ignored issues
show
Bug introduced by
The class Magento\Framework\Exception\NoSuchEntityException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
108
                    return null;
109
                }
110
            }
111
        }
112
        
113 1
        return null;
114
    }
115 1
116
    /**
117
     * Checks if given product is properly configured to be processed as a Stockbase product.
118
     *
119
     * @param int $productId
120
     * @return bool
121
     */
122
    public function isStockbaseProduct($productId)
123
    {
124
        return !empty($this->getStockbaseEan($productId));
125 2
    }
126
127 2
    /**
128 2
     * Increments/decrements the amount of items in stock.
129
     *
130
     * @param string $ean
131
     * @param float  $amount
132
     * @param string $operation
133
     */
134
    public function updateStockAmount($ean, $amount, $operation = '-')
135
    {
136
        $this->stockItemResource->updateStockAmount($ean, $amount, $operation);
137
    }
138 1
139
    /**
140 1
     * Creates a stock reserve for given item.
141
     *
142
     * @param QuoteItem $quoteItem
143 1
     * @param float     $stockbaseAmount
144 1
     * @param float     $magentoStockAmount
145 1
     * @return StockItemReserve
146 1
     */
147 1
    public function createReserve(QuoteItem $quoteItem, $stockbaseAmount, $magentoStockAmount)
148 1
    {
149 1
        $ean = $this->getStockbaseEan($quoteItem->getProductId());
150 1
        
151
        /** @var StockItemReserve $stockItemReserve */
152 1
        $stockItemReserve = $this->objectManager->create(StockItemReserve::class);
153
        $stockItemReserve->setData([
154 1
            'ean' => $ean,
155
            'amount' => $stockbaseAmount,
156
            'magento_stock_amount' => $magentoStockAmount,
157
            'quote_item_id' => $quoteItem->getId(),
158
            'product_id' => $quoteItem->getProductId(),
159
            'created_at' => (new \DateTime())->format('Y-m-d H:i:s'),
160
        ]);
161
        $stockItemReserve->save();
162 1
        
163
        return $stockItemReserve;
164 1
    }
165 1
    
166
    /**
167
     * Releases the stock reserve.
168
     *
169
     * @param StockItemReserve $reserve
170
     */
171
    public function releaseReserve(StockItemReserve $reserve)
172
    {
173 1
        $reserve->delete();
174
    }
175 1
176
    /**
177
     * Gets stock reserve entries for given products.
178 1
     *
179 1
     * @param int|int[] $productIds
180
     * @return StockItemReserve[]
181
     */
182 1 View Code Duplication
    public function getReserveForProduct($productIds)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
183
    {
184 1
        $productIds = is_array($productIds) ? $productIds : [$productIds];
185
186
        /** @var StockItemReserveCollection $reserveCollection */
187
        $reserveCollection = $this->objectManager->create(StockItemReserveCollection::class);
188
        $reserveCollection->addFieldToFilter('product_id', ['in' => $productIds]);
189
190
        /** @var StockItemReserve[] $reservedStockbaseItems */
191
        $reservedStockbaseItems = $reserveCollection->getItems();
192
193 1
        return $reservedStockbaseItems;
194
    }
195 1
196
    /**
197
     * Gets stock reserve entries for given quote items.
198 1
     *
199 1
     * @param int|int[] $quoteItemIds
200
     * @return StockItemReserve[]
201
     */
202 1 View Code Duplication
    public function getReserveForQuoteItem($quoteItemIds)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
203
    {
204 1
        $quoteItemIds = is_array($quoteItemIds) ? $quoteItemIds : [$quoteItemIds];
205
        
206
        /** @var StockItemReserveCollection $reserveCollection */
207
        $reserveCollection = $this->objectManager->create(StockItemReserveCollection::class);
208
        $reserveCollection->addFieldToFilter('quote_item_id', ['in' => $quoteItemIds]);
209
210
        /** @var StockItemReserve[] $reservedStockbaseItems */
211
        $reservedStockbaseItems = $reserveCollection->getItems();
212
        
213
        return $reservedStockbaseItems;
214
    }
215
}
216