UpdateSalesChannelWebsiteCode.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\InventorySales\Model\ResourceModel;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
  10. /**
  11. * This class handles website code change and should not be used directly, but only
  12. * from \Magento\InventorySales\Plugin\Store\WebsiteResourcePlugin to keep a soft integrity
  13. * between 'store_website' table and 'inventory_stock_sales_channel' table on changes.
  14. */
  15. class UpdateSalesChannelWebsiteCode
  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. * @param string $oldCode
  31. * @param string $newCode
  32. * @return void
  33. */
  34. public function execute(
  35. string $oldCode,
  36. string $newCode
  37. ): void {
  38. $connection = $this->resourceConnection->getConnection();
  39. $tableName = $this->resourceConnection->getTableName('inventory_stock_sales_channel');
  40. $connection->update(
  41. $tableName,
  42. [
  43. SalesChannelInterface::CODE => $newCode,
  44. ],
  45. [
  46. SalesChannelInterface::TYPE . ' = ?' => SalesChannelInterface::TYPE_WEBSITE,
  47. SalesChannelInterface::CODE . ' = ?' => $oldCode,
  48. ]
  49. );
  50. }
  51. }