Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockItemImporter.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
11 use Magento\CatalogInventory\Model\ResourceModel\Stock\ItemFactory;
13 use Psr\Log\LoggerInterface;
14 
16 {
22  private $stockResourceItemFactory;
23 
27  private $logger;
28 
35  public function __construct(
36  ItemFactory $stockResourceItemFactory,
37  LoggerInterface $logger
38  ) {
39  $this->stockResourceItemFactory = $stockResourceItemFactory;
40  $this->logger = $logger;
41  }
42 
46  public function import(array $stockData)
47  {
49  $stockItemResource = $this->stockResourceItemFactory->create();
50  $entityTable = $stockItemResource->getMainTable();
51  try {
52  $stockImportData = array_map(
53  function ($stockItemData) {
54  unset($stockItemData['sku']);
55  return $stockItemData;
56  },
57  array_values($stockData)
58  );
59  $stockItemResource->getConnection()->insertOnDuplicate($entityTable, $stockImportData);
60  } catch (\Exception $e) {
61  $this->logger->error($e->getMessage());
62  throw new CouldNotSaveException(__('Invalid Stock data for insert'), $e);
63  }
64  }
65 }
__()
Definition: __.php:13
$logger
__construct(ItemFactory $stockResourceItemFactory, LoggerInterface $logger)
$entityTable
Definition: tablerates.php:11
$stockData
Definition: products.php:27