DeleteMultiple.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\StockSourceLink;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Inventory\Model\ResourceModel\StockSourceLink as StockSourceLinkResourceModel;
  10. use Magento\InventoryApi\Api\Data\StockSourceLinkInterface;
  11. /**
  12. * Implementation of StockSourceLink delete multiple operation for specific db layer
  13. * Delete Multiple used here for performance efficient purposes over single delete operation
  14. */
  15. class DeleteMultiple
  16. {
  17. /**
  18. * @var ResourceConnection
  19. */
  20. private $resourceConnection;
  21. /**
  22. * @param ResourceConnection $resourceConnection
  23. */
  24. public function __construct(
  25. ResourceConnection $resourceConnection
  26. ) {
  27. $this->resourceConnection = $resourceConnection;
  28. }
  29. /**
  30. * Multiple delete stock source links
  31. *
  32. * @param StockSourceLinkInterface[] $links
  33. * @return void
  34. */
  35. public function execute(array $links)
  36. {
  37. if (!count($links)) {
  38. return;
  39. }
  40. $connection = $this->resourceConnection->getConnection();
  41. $tableName = $this->resourceConnection->getTableName(
  42. StockSourceLinkResourceModel::TABLE_NAME_STOCK_SOURCE_LINK
  43. );
  44. $whereSql = $this->buildWhereSqlPart($links);
  45. $connection->delete($tableName, $whereSql);
  46. }
  47. /**
  48. * Build WHERE part of the delete SQL query
  49. *
  50. * @param StockSourceLinkInterface[] $links
  51. * @return string
  52. */
  53. private function buildWhereSqlPart(array $links): string
  54. {
  55. $connection = $this->resourceConnection->getConnection();
  56. $condition = [];
  57. foreach ($links as $link) {
  58. $skuCondition = $connection->quoteInto(
  59. StockSourceLinkInterface::SOURCE_CODE . ' = ?',
  60. $link->getSourceCode()
  61. );
  62. $sourceCodeCondition = $connection->quoteInto(
  63. StockSourceLinkInterface::STOCK_ID . ' = ?',
  64. $link->getStockId()
  65. );
  66. $condition[] = '(' . $skuCondition . ' AND ' . $sourceCodeCondition . ')';
  67. }
  68. return implode(' OR ', $condition);
  69. }
  70. }