ReindexRuleGroupWebsite.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogRule\Model\Indexer;
  7. use Magento\CatalogRule\Model\Indexer\IndexerTableSwapperInterface as TableSwapper;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher;
  10. /**
  11. * Reindex information about rule relations with customer groups and websites.
  12. */
  13. class ReindexRuleGroupWebsite
  14. {
  15. /**
  16. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  17. */
  18. private $dateTime;
  19. /**
  20. * @var \Magento\Framework\App\ResourceConnection
  21. */
  22. private $resource;
  23. /**
  24. * @var array
  25. */
  26. private $catalogRuleGroupWebsiteColumnsList = ['rule_id', 'customer_group_id', 'website_id'];
  27. /**
  28. * @var ActiveTableSwitcher
  29. */
  30. private $activeTableSwitcher;
  31. /**
  32. * @var TableSwapper
  33. */
  34. private $tableSwapper;
  35. /**
  36. * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
  37. * @param \Magento\Framework\App\ResourceConnection $resource
  38. * @param ActiveTableSwitcher $activeTableSwitcher
  39. * @param TableSwapper|null $tableSwapper
  40. */
  41. public function __construct(
  42. \Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
  43. \Magento\Framework\App\ResourceConnection $resource,
  44. ActiveTableSwitcher $activeTableSwitcher,
  45. TableSwapper $tableSwapper = null
  46. ) {
  47. $this->dateTime = $dateTime;
  48. $this->resource = $resource;
  49. $this->activeTableSwitcher = $activeTableSwitcher;
  50. $this->tableSwapper = $tableSwapper ??
  51. ObjectManager::getInstance()->get(TableSwapper::class);
  52. }
  53. /**
  54. * Prepare and persist information about rule relations with customer groups and websites to index table.
  55. *
  56. * @param bool $useAdditionalTable
  57. * @return bool
  58. */
  59. public function execute($useAdditionalTable = false)
  60. {
  61. $connection = $this->resource->getConnection();
  62. $timestamp = $this->dateTime->gmtTimestamp();
  63. $indexTable = $this->resource->getTableName('catalogrule_group_website');
  64. $ruleProductTable = $this->resource->getTableName('catalogrule_product');
  65. if ($useAdditionalTable) {
  66. $indexTable = $this->resource->getTableName(
  67. $this->tableSwapper->getWorkingTableName('catalogrule_group_website')
  68. );
  69. $ruleProductTable = $this->resource->getTableName(
  70. $this->tableSwapper->getWorkingTableName('catalogrule_product')
  71. );
  72. }
  73. $connection->delete($indexTable);
  74. $select = $connection->select()->distinct(
  75. true
  76. )->from(
  77. $ruleProductTable,
  78. $this->catalogRuleGroupWebsiteColumnsList
  79. )->where(
  80. "{$timestamp} >= from_time AND (({$timestamp} <= to_time AND to_time > 0) OR to_time = 0)"
  81. );
  82. $query = $select->insertFromSelect($indexTable, $this->catalogRuleGroupWebsiteColumnsList);
  83. $connection->query($query);
  84. return true;
  85. }
  86. }