Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RuleProductPricesPersistor.php
Go to the documentation of this file.
1 <?php
8 
12 
17 {
21  private $resource;
22 
26  private $dateFormat;
27 
31  private $activeTableSwitcher;
32 
36  private $tableSwapper;
37 
44  public function __construct(
45  \Magento\Framework\Stdlib\DateTime $dateFormat,
46  \Magento\Framework\App\ResourceConnection $resource,
47  ActiveTableSwitcher $activeTableSwitcher,
48  TableSwapper $tableSwapper = null
49  ) {
50  $this->dateFormat = $dateFormat;
51  $this->resource = $resource;
52  $this->activeTableSwitcher = $activeTableSwitcher;
53  $this->tableSwapper = $tableSwapper ??
54  ObjectManager::getInstance()->get(TableSwapper::class);
55  }
56 
65  public function execute(array $priceData, $useAdditionalTable = false)
66  {
67  if (empty($priceData)) {
68  return false;
69  }
70 
71  $connection = $this->resource->getConnection();
72  $indexTable = $this->resource->getTableName('catalogrule_product_price');
73  if ($useAdditionalTable) {
74  $indexTable = $this->resource->getTableName(
75  $this->tableSwapper->getWorkingTableName('catalogrule_product_price')
76  );
77  }
78 
79  $productIds = [];
80 
81  try {
82  foreach ($priceData as $key => $data) {
83  $productIds['product_id'] = $data['product_id'];
84  $priceData[$key]['rule_date'] = $this->dateFormat->formatDate($data['rule_date'], false);
85  $priceData[$key]['latest_start_date'] = $this->dateFormat->formatDate(
86  $data['latest_start_date'],
87  false
88  );
89  $priceData[$key]['earliest_end_date'] = $this->dateFormat->formatDate(
90  $data['earliest_end_date'],
91  false
92  );
93  }
94  $connection->insertOnDuplicate($indexTable, $priceData);
95  } catch (\Exception $e) {
96  throw $e;
97  }
98  return true;
99  }
100 }
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\Stdlib\DateTime $dateFormat, \Magento\Framework\App\ResourceConnection $resource, ActiveTableSwitcher $activeTableSwitcher, TableSwapper $tableSwapper=null)
$connection
Definition: bulk.php:13