StockIdResolver.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\InventorySales\Model\ResourceModel;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
  10. /**
  11. * This resource model is responsible for retrieving Stock items by sales channel type and code
  12. * Used by Service Contracts that are agnostic to the Data Access Layer
  13. */
  14. class StockIdResolver
  15. {
  16. /**
  17. * @var ResourceConnection
  18. */
  19. private $resourceConnection;
  20. /**
  21. * @param ResourceConnection $resourceConnection
  22. */
  23. public function __construct(
  24. ResourceConnection $resourceConnection
  25. ) {
  26. $this->resourceConnection = $resourceConnection;
  27. }
  28. /**
  29. * Returns the linked stock id by given a sales channel type and code
  30. *
  31. * @param string $type
  32. * @param string $code
  33. * @return int|null
  34. */
  35. public function resolve(string $type, string $code)
  36. {
  37. $connection = $this->resourceConnection->getConnection();
  38. $tableName = $this->resourceConnection->getTableName('inventory_stock_sales_channel');
  39. $select = $connection->select()
  40. ->from($tableName, 'stock_id')
  41. ->where(SalesChannelInterface::TYPE . ' = ?', $type)
  42. ->where(SalesChannelInterface::CODE . ' = ?', $code);
  43. $stockId = $connection->fetchOne($select);
  44. return false === $stockId ? null : (int)$stockId;
  45. }
  46. }