GetAssignedStockIdForWebsite.php 1.4 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\Model\GetAssignedStockIdForWebsiteInterface;
  10. use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
  11. /**
  12. * @inheritdoc
  13. */
  14. class GetAssignedStockIdForWebsite implements GetAssignedStockIdForWebsiteInterface
  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. * @inheritdoc
  30. */
  31. public function execute(string $websiteCode): ?int
  32. {
  33. $connection = $this->resourceConnection->getConnection();
  34. $tableName = $this->resourceConnection->getTableName('inventory_stock_sales_channel');
  35. $select = $connection->select()
  36. ->from($tableName, ['stock_id'])
  37. ->where('code = ?', $websiteCode)
  38. ->where('type = ?', SalesChannelInterface::TYPE_WEBSITE);
  39. $result = $connection->fetchCol($select);
  40. if (count($result) === 0) {
  41. return null;
  42. }
  43. return (int)reset($result);
  44. }
  45. }