CleanupReservations.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\InventoryReservations\Model\ResourceModel;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\InventoryReservationsApi\Model\ReservationInterface;
  10. use Magento\InventoryReservationsApi\Model\CleanupReservationsInterface;
  11. /**
  12. * @inheritdoc
  13. */
  14. class CleanupReservations implements CleanupReservationsInterface
  15. {
  16. /**
  17. * @var ResourceConnection
  18. */
  19. private $resource;
  20. /**
  21. * @var int
  22. */
  23. private $groupConcatMaxLen;
  24. /**
  25. * @param ResourceConnection $resource
  26. * @param int $groupConcatMaxLen
  27. */
  28. public function __construct(
  29. ResourceConnection $resource,
  30. int $groupConcatMaxLen
  31. ) {
  32. $this->resource = $resource;
  33. $this->groupConcatMaxLen = $groupConcatMaxLen;
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function execute(): void
  39. {
  40. $connection = $this->resource->getConnection();
  41. $reservationTable = $this->resource->getTableName('inventory_reservation');
  42. $select = $connection->select()
  43. ->from(
  44. $reservationTable,
  45. ['GROUP_CONCAT(' . ReservationInterface::RESERVATION_ID . ')']
  46. )
  47. ->group([ReservationInterface::STOCK_ID, ReservationInterface::SKU])
  48. ->having('SUM(' . ReservationInterface::QUANTITY . ') = 0');
  49. $connection->query('SET group_concat_max_len = ' . $this->groupConcatMaxLen);
  50. $groupedReservationIds = implode(',', $connection->fetchCol($select));
  51. $condition = [ReservationInterface::RESERVATION_ID . ' IN (?)' => explode(',', $groupedReservationIds)];
  52. $connection->delete($reservationTable, $condition);
  53. }
  54. }