IsProductAssignedToStock.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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\Inventory\Model\ResourceModel;
  8. use Magento\InventoryApi\Model\IsProductAssignedToStockInterface;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  11. use Magento\InventoryApi\Api\Data\StockSourceLinkInterface;
  12. class IsProductAssignedToStock implements IsProductAssignedToStockInterface
  13. {
  14. /**
  15. * @var ResourceConnection
  16. */
  17. private $resource;
  18. /**
  19. * @param ResourceConnection $resource
  20. */
  21. public function __construct(
  22. ResourceConnection $resource
  23. ) {
  24. $this->resource = $resource;
  25. }
  26. /**
  27. * @inheritdoc
  28. */
  29. public function execute(string $sku, int $stockId): bool
  30. {
  31. $connection = $this->resource->getConnection();
  32. $select = $connection->select()
  33. ->from(
  34. ['stock_source_link' => $this->resource->getTableName(StockSourceLink::TABLE_NAME_STOCK_SOURCE_LINK)]
  35. )->join(
  36. ['inventory_source_item' => $this->resource->getTableName(SourceItem::TABLE_NAME_SOURCE_ITEM)],
  37. 'inventory_source_item.' . SourceItemInterface::SOURCE_CODE . '
  38. = stock_source_link.' . SourceItemInterface::SOURCE_CODE,
  39. []
  40. )->where(
  41. 'stock_source_link.' . StockSourceLinkInterface::STOCK_ID . ' = ?',
  42. $stockId
  43. )->where(
  44. 'inventory_source_item.' . SourceItemInterface::SKU . ' = ?',
  45. $sku
  46. );
  47. return (bool)$connection->fetchOne($select);
  48. }
  49. }