Skip to content

Commit 47dd9bd

Browse files
committed
MC-18954: Nightly build jobs were failing lately after un-skipping tests in MC-5777
1 parent b977212 commit 47dd9bd

File tree

1 file changed

+32
-49
lines changed

1 file changed

+32
-49
lines changed

app/code/Magento/CatalogRule/Model/Indexer/ReindexRuleProductPrice.php

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,82 +6,72 @@
66

77
namespace Magento\CatalogRule\Model\Indexer;
88

9+
use Magento\Catalog\Model\Product;
10+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
913
/**
1014
* Reindex product prices according rule settings.
1115
*/
1216
class ReindexRuleProductPrice
1317
{
1418
/**
15-
* @var \Magento\Store\Model\StoreManagerInterface
19+
* @var StoreManagerInterface
1620
*/
1721
private $storeManager;
1822

1923
/**
20-
* @var \Magento\CatalogRule\Model\Indexer\RuleProductsSelectBuilder
24+
* @var RuleProductsSelectBuilder
2125
*/
2226
private $ruleProductsSelectBuilder;
2327

2428
/**
25-
* @var \Magento\CatalogRule\Model\Indexer\ProductPriceCalculator
29+
* @var ProductPriceCalculator
2630
*/
2731
private $productPriceCalculator;
2832

2933
/**
30-
* @var \Magento\Framework\Stdlib\DateTime\DateTime
34+
* @var TimezoneInterface
3135
*/
32-
private $dateTime;
36+
private $localeDate;
3337

3438
/**
35-
* @var \Magento\CatalogRule\Model\Indexer\RuleProductPricesPersistor
39+
* @var RuleProductPricesPersistor
3640
*/
3741
private $pricesPersistor;
3842

3943
/**
40-
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
41-
*/
42-
private $localeDate;
43-
44-
/**
45-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
44+
* @param StoreManagerInterface $storeManager
4645
* @param RuleProductsSelectBuilder $ruleProductsSelectBuilder
4746
* @param ProductPriceCalculator $productPriceCalculator
48-
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
49-
* @param \Magento\CatalogRule\Model\Indexer\RuleProductPricesPersistor $pricesPersistor
50-
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
47+
* @param TimezoneInterface $localeDate
48+
* @param RuleProductPricesPersistor $pricesPersistor
5149
*/
5250
public function __construct(
53-
\Magento\Store\Model\StoreManagerInterface $storeManager,
54-
\Magento\CatalogRule\Model\Indexer\RuleProductsSelectBuilder $ruleProductsSelectBuilder,
55-
\Magento\CatalogRule\Model\Indexer\ProductPriceCalculator $productPriceCalculator,
56-
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
57-
\Magento\CatalogRule\Model\Indexer\RuleProductPricesPersistor $pricesPersistor,
58-
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
51+
StoreManagerInterface $storeManager,
52+
RuleProductsSelectBuilder $ruleProductsSelectBuilder,
53+
ProductPriceCalculator $productPriceCalculator,
54+
TimezoneInterface $localeDate,
55+
RuleProductPricesPersistor $pricesPersistor
5956
) {
6057
$this->storeManager = $storeManager;
6158
$this->ruleProductsSelectBuilder = $ruleProductsSelectBuilder;
6259
$this->productPriceCalculator = $productPriceCalculator;
63-
$this->dateTime = $dateTime;
64-
$this->pricesPersistor = $pricesPersistor;
6560
$this->localeDate = $localeDate;
61+
$this->pricesPersistor = $pricesPersistor;
6662
}
6763

6864
/**
6965
* Reindex product prices.
7066
*
7167
* @param int $batchCount
72-
* @param \Magento\Catalog\Model\Product|null $product
68+
* @param Product|null $product
7369
* @param bool $useAdditionalTable
7470
* @return bool
7571
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
7672
*/
77-
public function execute(
78-
$batchCount,
79-
\Magento\Catalog\Model\Product $product = null,
80-
$useAdditionalTable = false
81-
) {
82-
$fromDate = mktime(0, 0, 0, date('m'), date('d') - 1);
83-
$toDate = mktime(0, 0, 0, date('m'), date('d') + 1);
84-
73+
public function execute($batchCount, Product $product = null, $useAdditionalTable = false)
74+
{
8575
/**
8676
* Update products rules prices per each website separately
8777
* because for each website date in website's timezone should be used
@@ -91,8 +81,13 @@ public function execute(
9181
$dayPrices = [];
9282
$stopFlags = [];
9383
$prevKey = null;
84+
9485
$storeGroup = $this->storeManager->getGroup($website->getDefaultGroupId());
95-
$storeId = $storeGroup->getDefaultStoreId();
86+
$currentDate = $this->localeDate->scopeDate($storeGroup->getDefaultStoreId(), null, true);
87+
$previousDate = (clone $currentDate)->modify('-1 day');
88+
$previousDate->setTime(23, 59, 59);
89+
$nextDate = (clone $currentDate)->modify('+1 day');
90+
$nextDate->setTime(0, 0, 0);
9691

9792
while ($ruleData = $productsStmt->fetch()) {
9893
$ruleProductId = $ruleData['product_id'];
@@ -110,24 +105,24 @@ public function execute(
110105
}
111106
}
112107

113-
$ruleData['from_time'] = $this->roundTime($ruleData['from_time']);
114-
$ruleData['to_time'] = $this->roundTime($ruleData['to_time']);
115108
/**
116109
* Build prices for each day
117110
*/
118-
for ($time = $fromDate; $time <= $toDate; $time += IndexBuilder::SECONDS_IN_DAY) {
111+
foreach ([$previousDate, $currentDate, $nextDate] as $date) {
112+
$time = $date->getTimestamp();
119113
if (($ruleData['from_time'] == 0 ||
120114
$time >= $ruleData['from_time']) && ($ruleData['to_time'] == 0 ||
121115
$time <= $ruleData['to_time'])
122116
) {
123117
$priceKey = $time . '_' . $productKey;
118+
124119
if (isset($stopFlags[$priceKey])) {
125120
continue;
126121
}
127122

128123
if (!isset($dayPrices[$priceKey])) {
129124
$dayPrices[$priceKey] = [
130-
'rule_date' => $this->localeDate->scopeDate($storeId, $time),
125+
'rule_date' => $date,
131126
'website_id' => $ruleData['website_id'],
132127
'customer_group_id' => $ruleData['customer_group_id'],
133128
'product_id' => $ruleProductId,
@@ -163,16 +158,4 @@ public function execute(
163158

164159
return true;
165160
}
166-
167-
/**
168-
* @param int $timeStamp
169-
* @return int
170-
*/
171-
private function roundTime($timeStamp)
172-
{
173-
if (is_numeric($timeStamp) && $timeStamp != 0) {
174-
$timeStamp = $this->dateTime->timestamp($this->dateTime->date('Y-m-d 00:00:00', $timeStamp));
175-
}
176-
return $timeStamp;
177-
}
178161
}

0 commit comments

Comments
 (0)