Issues (52)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

Model/Inventory/StockbaseStockManagement.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
     * @param ObjectManagerInterface     $objectManager
50
     */
51 9
    public function __construct(
52
        StockRegistryInterface $stockRegistry,
53
        StockbaseConfiguration $config,
54
        ProductRepositoryInterface $productRepository,
55
        StockItemResource $stockItemResource,
56
        ObjectManagerInterface $objectManager
57
    ) {
58
59 9
        $this->stockRegistry = $stockRegistry;
60 9
        $this->config = $config;
61 9
        $this->productRepository = $productRepository;
62 9
        $this->stockItemResource = $stockItemResource;
63 9
        $this->objectManager = $objectManager;
64 9
    }
65
66
    /**
67
     * Gets the amount of items available in the Stockbase stock.
68
     *
69
     * @param int $productId
70
     * @return float
71
     */
72 1
    public function getStockbaseStockAmount($productId)
73
    {
74 1
        $qty = 0;
75
        
76 1
        $ean = $this->getStockbaseEan($productId);
77 1
        if (!empty($ean)) {
78 1
            $qty += max($this->stockItemResource->getNotReservedStockAmount($ean), 0);
79
        }
80
        
81 1
        return $qty;
82
    }
83
84
    /**
85
     * Gets the Stockbase EAN for given product (if any).
86
     *
87
     * @param int $productId
88
     * @return string|null
89
     */
90 4
    public function getStockbaseEan($productId)
91
    {
92 4
        if ($this->config->isModuleEnabled()) {
93 4
            $stockItem = $this->stockRegistry->getStockItem($productId);
94 4
            if ($stockItem->getManageStock() &&
95 4
                $stockItem->getBackorders() == \Magento\CatalogInventory\Model\Stock::BACKORDERS_NO
96
            ) {
97
                try {
98
                    /** @var Product $product */
99 4
                    $product = $this->productRepository->getById($productId);
100
101 4
                    if (!$product->isComposite() && !$product->isVirtual()) {
102 4
                        $ean = $product->getData($this->config->getEanFieldName());
103 4
                        if (!empty($ean) && $product->getData('stockbase_product')) {
104 4
                            return $ean;
105
                        }
106
                    }
107
                } catch (NoSuchEntityException $e) {
0 ignored issues
show
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
        return null;
114
    }
115
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 1
    public function isStockbaseProduct($productId)
123
    {
124 1
        return !empty($this->getStockbaseEan($productId));
125
    }
126
127
    /**
128
     * Increments/decrements the amount of items in stock.
129
     *
130
     * @param string $ean
131
     * @param float  $amount
132
     * @param string $operation
133
     */
134 2
    public function updateStockAmount($ean, $amount, $operation = '-')
135
    {
136 2
        $this->stockItemResource->updateStockAmount($ean, $amount, $operation);
137 2
    }
138
139
    /**
140
     * Creates a stock reserve for given item.
141
     *
142
     * @param QuoteItem $quoteItem
143
     * @param float     $stockbaseAmount
144
     * @param float     $magentoStockAmount
145
     * @return StockItemReserve
146
     */
147 1
    public function createReserve(QuoteItem $quoteItem, $stockbaseAmount, $magentoStockAmount)
148
    {
149 1
        $ean = $this->getStockbaseEan($quoteItem->getProductId());
150
        
151
        /** @var StockItemReserve $stockItemReserve */
152 1
        $stockItemReserve = $this->objectManager->create(StockItemReserve::class);
153 1
        $stockItemReserve->setData([
154 1
            'ean' => $ean,
155 1
            'amount' => $stockbaseAmount,
156 1
            'magento_stock_amount' => $magentoStockAmount,
157 1
            'quote_item_id' => $quoteItem->getId(),
158 1
            'product_id' => $quoteItem->getProductId(),
159 1
            'created_at' => (new \DateTime())->format('Y-m-d H:i:s'),
160
        ]);
161 1
        $stockItemReserve->save();
162
        
163 1
        return $stockItemReserve;
164
    }
165
    
166
    /**
167
     * Releases the stock reserve.
168
     *
169
     * @param StockItemReserve $reserve
170
     */
171 1
    public function releaseReserve(StockItemReserve $reserve)
172
    {
173 1
        $reserve->delete();
174 1
    }
175
176
    /**
177
     * Gets stock reserve entries for given products.
178
     *
179
     * @param int|int[] $productIds
180
     * @return StockItemReserve[]
181
     */
182 1 View Code Duplication
    public function getReserveForProduct($productIds)
0 ignored issues
show
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 1
        $reserveCollection = $this->objectManager->create(StockItemReserveCollection::class);
188 1
        $reserveCollection->addFieldToFilter('product_id', ['in' => $productIds]);
189
190
        /** @var StockItemReserve[] $reservedStockbaseItems */
191 1
        $reservedStockbaseItems = $reserveCollection->getItems();
192
193 1
        return $reservedStockbaseItems;
194
    }
195
196
    /**
197
     * Gets stock reserve entries for given quote items.
198
     *
199
     * @param int|int[] $quoteItemIds
200
     * @return StockItemReserve[]
201
     */
202 1 View Code Duplication
    public function getReserveForQuoteItem($quoteItemIds)
0 ignored issues
show
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 1
        $reserveCollection = $this->objectManager->create(StockItemReserveCollection::class);
208 1
        $reserveCollection->addFieldToFilter('quote_item_id', ['in' => $quoteItemIds]);
209
210
        /** @var StockItemReserve[] $reservedStockbaseItems */
211 1
        $reservedStockbaseItems = $reserveCollection->getItems();
212
        
213 1
        return $reservedStockbaseItems;
214
    }
215
}
216