SaveMultiple.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. /**
  11. * Implementation of Reservation save multiple operation for specific db layer
  12. * Save Multiple used here for performance efficient purposes over single save operation
  13. */
  14. class SaveMultiple
  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. * @param ReservationInterface[] $reservations
  30. * @return void
  31. */
  32. public function execute(array $reservations)
  33. {
  34. $connection = $this->resourceConnection->getConnection();
  35. $tableName = $this->resourceConnection->getTableName('inventory_reservation');
  36. $columns = [
  37. ReservationInterface::STOCK_ID,
  38. ReservationInterface::SKU,
  39. ReservationInterface::QUANTITY,
  40. ReservationInterface::METADATA,
  41. ];
  42. $data = [];
  43. /** @var ReservationInterface $reservation */
  44. foreach ($reservations as $reservation) {
  45. $data[] = [
  46. $reservation->getStockId(),
  47. $reservation->getSku(),
  48. $reservation->getQuantity(),
  49. $reservation->getMetadata(),
  50. ];
  51. }
  52. $connection->insertArray($tableName, $columns, $data);
  53. }
  54. }