Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockItemValidator.php
Go to the documentation of this file.
1 <?php
8 
14 
19 {
23  private $stockConfiguration;
24 
28  private $stockRegistry;
29 
34  public function __construct(
35  StockConfigurationInterface $stockConfiguration,
36  StockRegistryInterface $stockRegistry
37  ) {
38  $this->stockConfiguration = $stockConfiguration;
39  $this->stockRegistry = $stockRegistry;
40  }
41 
51  {
52  $defaultScopeId = $this->stockConfiguration->getDefaultScopeId();
53  $defaultStockId = $this->stockRegistry->getStock($defaultScopeId)->getStockId();
54  $stockId = $stockItem->getStockId();
55  if ($stockId !== null && $stockId != $defaultStockId) {
56  throw new LocalizedException(
57  __(
58  'The "%1" value is invalid for stock ID. Enter stock with a default value of %2 to try again.',
59  $stockId,
60  $defaultStockId
61  )
62  );
63  }
64 
65  $stockItemId = $stockItem->getItemId();
66  if ($stockItemId !== null && (!is_numeric($stockItemId) || $stockItemId <= 0)) {
67  throw new LocalizedException(
68  __(
69  'The "%1" value is invalid for stock item ID. '
70  . 'Enter either zero or a number than zero to try again.',
71  $stockItemId
72  )
73  );
74  }
75 
76  $defaultStockItemId = $this->stockRegistry->getStockItem($product->getId())->getItemId();
77  if ($defaultStockItemId && $stockItemId !== null && $defaultStockItemId != $stockItemId) {
78  throw new LocalizedException(
79  __(
80  'The "%1" value is invalid for stock item ID. '
81  . 'Use the stock item ID\'s assigned "%2" value and try again.',
82  $stockItemId,
83  $defaultStockItemId
84  )
85  );
86  }
87  }
88 }
__construct(StockConfigurationInterface $stockConfiguration, StockRegistryInterface $stockRegistry)
__()
Definition: __.php:13
validate(ProductInterface $product, StockItemInterface $stockItem)