-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
WishlistDataCleanUp.php
154 lines (141 loc) · 4.05 KB
/
WishlistDataCleanUp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Quote\Setup\Patch\Data;
use Magento\Framework\DB\Query\Generator;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Quote\Setup\QuoteSetupFactory;
use Psr\Log\LoggerInterface;
/**
* Class Clean Up Data Removes unused data
*/
class WishlistDataCleanUp implements DataPatchInterface
{
/**
* Batch size for query
*/
private const BATCH_SIZE = 1000;
/**
* @var QuoteSetupFactory
*/
private $quoteSetupFactory;
/**
* @var Generator
*/
private $queryGenerator;
/**
* @var Json
*/
private $json;
/**
* @var LoggerInterface
*/
private $logger;
/**
* RemoveData constructor.
* @param Json $json
* @param Generator $queryGenerator
* @param QuoteSetupFactory $quoteSetupFactory
* @param LoggerInterface $logger
*/
public function __construct(
Json $json,
Generator $queryGenerator,
QuoteSetupFactory $quoteSetupFactory,
LoggerInterface $logger
) {
$this->json = $json;
$this->queryGenerator = $queryGenerator;
$this->quoteSetupFactory = $quoteSetupFactory;
$this->logger = $logger;
}
/**
* @inheritdoc
*/
public function apply()
{
try {
$this->cleanQuoteItemOptionTable();
} catch (\Throwable $e) {
$this->logger->warning(
'Quote module WishlistDataCleanUp patch experienced an error and could not be completed.'
. ' Please submit a support ticket or email us at security@magento.com.'
);
return $this;
}
return $this;
}
/**
* Remove login data from quote_item_option table.
*
* @throws LocalizedException
*/
private function cleanQuoteItemOptionTable()
{
$quoteSetup = $this->quoteSetupFactory->create();
$tableName = $quoteSetup->getTable('quote_item_option');
$select = $quoteSetup
->getConnection()
->select()
->from(
$tableName,
['option_id', 'value']
)
->where(
'value LIKE ?',
'%login%'
);
$iterator = $this->queryGenerator->generate('option_id', $select, self::BATCH_SIZE);
$rowErrorFlag = false;
foreach ($iterator as $selectByRange) {
$optionRows = $quoteSetup->getConnection()->fetchAll($selectByRange);
foreach ($optionRows as $optionRow) {
try {
$rowValue = $this->json->unserialize($optionRow['value']);
if (is_array($rowValue)
&& array_key_exists('login', $rowValue)
) {
unset($rowValue['login']);
}
$rowValue = $this->json->serialize($rowValue);
$quoteSetup->getConnection()->update(
$tableName,
['value' => $rowValue],
['option_id = ?' => $optionRow['option_id']]
);
} catch (\Throwable $e) {
$rowErrorFlag = true;
continue;
}
}
}
if ($rowErrorFlag) {
$this->logger->warning(
'Data clean up could not be completed due to unexpected data format in the table "'
. $tableName
. '". Please submit a support ticket or email us at security@magento.com.'
);
}
}
/**
* @inheritdoc
*/
public static function getDependencies()
{
return [
ConvertSerializedDataToJson::class
];
}
/**
* @inheritdoc
*/
public function getAliases()
{
return [];
}
}