DeleteMultiple.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\InventoryLowQuantityNotification\Model\ResourceModel\SourceItemConfiguration;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  10. use Magento\InventoryLowQuantityNotificationApi\Api\Data\SourceItemConfigurationInterface;
  11. /**
  12. * Implementation of SourceItem Configuration delete operation for specific db layer
  13. */
  14. class DeleteMultiple
  15. {
  16. /**
  17. * @var ResourceConnection
  18. */
  19. private $resourceConnection;
  20. /**
  21. * DeleteMultiple constructor.
  22. * @param ResourceConnection $resourceConnection
  23. */
  24. public function __construct(
  25. ResourceConnection $resourceConnection
  26. ) {
  27. $this->resourceConnection = $resourceConnection;
  28. }
  29. /**
  30. * Delete multiple notification configurations
  31. *
  32. * @param SourceItemInterface[] $sourceItems
  33. * @return void
  34. * @SuppressWarnings(PHPMD.LongVariable)
  35. */
  36. public function execute(array $sourceItems)
  37. {
  38. if (!empty($sourceItems)) {
  39. $connection = $this->resourceConnection->getConnection();
  40. $tableName = $this->resourceConnection
  41. ->getTableName('inventory_low_stock_notification_configuration');
  42. $whereSql = $this->buildWhereSqlPart($sourceItems);
  43. $connection->delete($tableName, $whereSql);
  44. }
  45. }
  46. /**
  47. * Build a SQL where from an array of source items
  48. *
  49. * @param SourceItemInterface[] $sourceItems
  50. * @return string
  51. */
  52. private function buildWhereSqlPart(array $sourceItems): string
  53. {
  54. $connection = $this->resourceConnection->getConnection();
  55. $condition = [];
  56. foreach ($sourceItems as $sourceItem) {
  57. $skuCondition = $connection->quoteInto(
  58. SourceItemConfigurationInterface::SKU . ' = ?',
  59. $sourceItem->getSku()
  60. );
  61. $sourceCodeCondition = $connection->quoteInto(
  62. SourceItemConfigurationInterface::SOURCE_CODE . ' = ?',
  63. $sourceItem->getSourceCode()
  64. );
  65. $condition[] = '(' . $skuCondition . ' AND ' . $sourceCodeCondition . ')';
  66. }
  67. return implode(' OR ', $condition);
  68. }
  69. }