GetStockItemData.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryIndexer\Model\ResourceModel;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface;
  11. use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
  12. use Magento\InventoryIndexer\Indexer\IndexStructure;
  13. /**
  14. * @inheritdoc
  15. */
  16. class GetStockItemData implements GetStockItemDataInterface
  17. {
  18. /**
  19. * @var ResourceConnection
  20. */
  21. private $resource;
  22. /**
  23. * @var StockIndexTableNameResolverInterface
  24. */
  25. private $stockIndexTableNameResolver;
  26. /**
  27. * @param ResourceConnection $resource
  28. * @param StockIndexTableNameResolverInterface $stockIndexTableNameResolver
  29. */
  30. public function __construct(
  31. ResourceConnection $resource,
  32. StockIndexTableNameResolverInterface $stockIndexTableNameResolver
  33. ) {
  34. $this->resource = $resource;
  35. $this->stockIndexTableNameResolver = $stockIndexTableNameResolver;
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function execute(string $sku, int $stockId): ?array
  41. {
  42. $stockItemTableName = $this->stockIndexTableNameResolver->execute($stockId);
  43. $connection = $this->resource->getConnection();
  44. $select = $connection->select()
  45. ->from(
  46. $stockItemTableName,
  47. [
  48. GetStockItemDataInterface::QUANTITY => IndexStructure::QUANTITY,
  49. GetStockItemDataInterface::IS_SALABLE => IndexStructure::IS_SALABLE,
  50. ]
  51. )
  52. ->where(IndexStructure::SKU . ' = ?', $sku);
  53. try {
  54. if ($connection->isTableExists($stockItemTableName)) {
  55. return $connection->fetchRow($select) ?: null;
  56. }
  57. return null;
  58. } catch (\Exception $e) {
  59. throw new LocalizedException(__(
  60. 'Could not receive Stock Item data'
  61. ), $e);
  62. }
  63. }
  64. }