Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FillSalesRuleProductAttributeTable.php
Go to the documentation of this file.
1 <?php
8 
13 
19 {
23  private $ruleColletionFactory;
24 
28  private $serializer;
29 
33  private $resourceModelRule;
34 
38  private $moduleDataSetup;
39 
43  private $appState;
44 
53  public function __construct(
54  \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $ruleColletionFactory,
55  \Magento\Framework\Serialize\SerializerInterface $serializer,
56  \Magento\SalesRule\Model\ResourceModel\Rule $resourceModelRule,
57  \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
58  State $appState
59  ) {
60  $this->ruleColletionFactory = $ruleColletionFactory;
61  $this->serializer = $serializer;
62  $this->resourceModelRule = $resourceModelRule;
63  $this->moduleDataSetup = $moduleDataSetup;
64  $this->appState = $appState;
65  }
66 
70  public function apply()
71  {
72  $this->moduleDataSetup->getConnection()->startSetup();
73  $this->appState->emulateAreaCode(
74  \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
75  [$this, 'fillSalesRuleProductAttributeTable']
76  );
77  $this->fillSalesRuleProductAttributeTable();
78  $this->moduleDataSetup->getConnection()->endSetup();
79  }
80 
84  public function fillSalesRuleProductAttributeTable()
85  {
87  $ruleCollection = $this->ruleColletionFactory->create();
89  foreach ($ruleCollection as $rule) {
90  // Save product attributes used in rule
91  $conditions = $rule->getConditions()->asArray();
92  $actions = $rule->getActions()->asArray();
93  $serializedConditions = $this->serializer->serialize($conditions);
94  $serializedActions = $this->serializer->serialize($actions);
95  $conditionAttributes = $this->resourceModelRule->getProductAttributes($serializedConditions);
96  $actionAttributes = $this->resourceModelRule->getProductAttributes($serializedActions);
97  $ruleProductAttributes = array_merge($conditionAttributes, $actionAttributes);
98  if ($ruleProductAttributes) {
99  $this->resourceModelRule->setActualProductAttributes($rule, $ruleProductAttributes);
100  }
101  }
102  }
103 
107  public static function getDependencies()
108  {
109  return [
110  ConvertSerializedDataToJson::class
111  ];
112  }
113 
117  public static function getVersion()
118  {
119  return '2.0.3';
120  }
121 
125  public function getAliases()
126  {
127  return [];
128  }
129 }
__construct(\Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $ruleColletionFactory, \Magento\Framework\Serialize\SerializerInterface $serializer, \Magento\SalesRule\Model\ResourceModel\Rule $resourceModelRule, \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup, State $appState)