-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathData.php
198 lines (184 loc) · 6.61 KB
/
Data.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Migration\Step\Settings;
use Migration\App\Step\StageInterface;
use Migration\Reader\Settings as ReaderSettings;
use Migration\Logger\Logger;
use Migration\App\ProgressBar;
use Migration\ResourceModel\Destination;
use Migration\ResourceModel\Source;
use Migration\ResourceModel;
use Migration\ResourceModel\Document;
use Migration\ResourceModel\Record;
use Migration\Handler;
/**
* Class Data
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Data implements StageInterface
{
const CONFIG_TABLE_NAME_SOURCE = 'core_config_data';
const CONFIG_TABLE_NAME_DESTINATION = 'core_config_data';
const CONFIG_FIELD_CONFIG_ID = 'config_id';
const CONFIG_FIELD_SCOPE_ID = 'scope_id';
const CONFIG_FIELD_SCOPE = 'scope';
const CONFIG_FIELD_PATH = 'path';
const CONFIG_FIELD_VALUE = 'value';
const CONFIG_FIELD_UPDATED_AT = 'updated_at';
/**
* @var array
*/
protected $configTableSchema = [
self::CONFIG_FIELD_CONFIG_ID,
self::CONFIG_FIELD_SCOPE,
self::CONFIG_FIELD_SCOPE_ID,
self::CONFIG_FIELD_PATH,
self::CONFIG_FIELD_VALUE
];
/**
* @var Destination
*/
protected $destination;
/**
* @var Logger
*/
protected $logger;
/**
* @var Source
*/
protected $source;
/**
* @var ProgressBar\LogLevelProcessor
*/
protected $progress;
/**
* @var ReaderSettings
*/
protected $readerSettings;
/**
* @var ResourceModel\RecordFactory
*/
protected $recordFactory;
/**
* @var Handler\ManagerFactory
*/
protected $handlerManagerFactory;
/**
* @param Destination $destination
* @param Source $source
* @param Logger $logger
* @param ProgressBar\LogLevelProcessor $progress
* @param ResourceModel\RecordFactory $recordFactory
* @param ReaderSettings $readerSettings
* @param Handler\ManagerFactory $handlerManagerFactory
*/
public function __construct(
Destination $destination,
Source $source,
Logger $logger,
ProgressBar\LogLevelProcessor $progress,
ResourceModel\RecordFactory $recordFactory,
ReaderSettings $readerSettings,
Handler\ManagerFactory $handlerManagerFactory
) {
$this->destination = $destination;
$this->source = $source;
$this->logger = $logger;
$this->progress = $progress;
$this->recordFactory = $recordFactory;
$this->readerSettings = $readerSettings;
$this->handlerManagerFactory = $handlerManagerFactory;
}
/**
* @inheritdoc
*/
public function perform()
{
$destinationDocument = $this->destination->getDocument(self::CONFIG_TABLE_NAME_DESTINATION);
$recordsCountSource = $this->source->getRecordsCount(self::CONFIG_TABLE_NAME_SOURCE);
$recordsCountDestination = $this->destination->getRecordsCount(self::CONFIG_TABLE_NAME_DESTINATION);
$this->progress->start($recordsCountSource);
$sourceRecords = $this->source->getRecords(
self::CONFIG_TABLE_NAME_SOURCE,
0,
$recordsCountSource
);
$destinationRecords = $this->destination->getRecords(
self::CONFIG_TABLE_NAME_DESTINATION,
0,
$recordsCountDestination
);
foreach ($sourceRecords as $sourceRecord) {
$this->progress->advance();
$sourceRecord = array_intersect_key($sourceRecord, array_flip($this->configTableSchema));
if (!$this->readerSettings->isNodeIgnored($sourceRecord[self::CONFIG_FIELD_PATH])) {
$sourceRecordPathMapped = $this->readerSettings->getNodeMap($sourceRecord[self::CONFIG_FIELD_PATH]);
foreach ($destinationRecords as &$destinationRecord) {
if ($destinationRecord[self::CONFIG_FIELD_SCOPE] == $sourceRecord[self::CONFIG_FIELD_SCOPE]
&& $destinationRecord[self::CONFIG_FIELD_SCOPE_ID] == $sourceRecord[self::CONFIG_FIELD_SCOPE_ID]
&& $destinationRecord[self::CONFIG_FIELD_PATH] == $sourceRecordPathMapped
) {
$record = $this->applyHandler($destinationDocument, $sourceRecord, $destinationRecord);
$destinationRecord[self::CONFIG_FIELD_VALUE] = $record->getValue(self::CONFIG_FIELD_VALUE);
continue 2;
}
}
$record = $this->applyHandler($destinationDocument, $sourceRecord, []);
$record->setValue(self::CONFIG_FIELD_PATH, $sourceRecordPathMapped);
$destinationRecords[] = $record->getData();
}
}
foreach ($destinationRecords as &$destinationRecord) {
unset($destinationRecord[self::CONFIG_FIELD_CONFIG_ID]);
$destinationRecord[self::CONFIG_FIELD_UPDATED_AT] = null;
}
$this->destination->clearDocument(self::CONFIG_TABLE_NAME_DESTINATION);
$this->destination->saveRecords(self::CONFIG_TABLE_NAME_DESTINATION, $destinationRecords);
$this->progress->finish();
return true;
}
/**
* Apply handler
*
* @param Document $document
* @param array $sourceData
* @param array $destinationData
* @return Record
*/
protected function applyHandler(
\Migration\ResourceModel\Document $document,
array $sourceData,
array $destinationData
) {
/** @var Record $sourceRecord */
$sourceRecord = $this->recordFactory->create(['document' => $document, 'data' => $sourceData]);
/** @var Record $destinationData */
$destinationRecord = $this->recordFactory->create(['document' => $document, 'data' => $destinationData]);
$handler = $this->getHandler($sourceData[self::CONFIG_FIELD_PATH]);
if ($handler) {
$handler->handle($sourceRecord, $destinationRecord);
}
return $sourceRecord;
}
/**
* Get handler
*
* @param string $path
* @return bool|Handler\HandlerInterface|null
* @throws \Migration\Exception
*/
protected function getHandler($path)
{
$handlerConfig = $this->readerSettings->getValueHandler($path);
if (!$handlerConfig) {
return false;
}
/** @var Handler\Manager $handlerManager */
$handlerManager = $this->handlerManagerFactory->create();
$handlerManager->initHandler(self::CONFIG_FIELD_VALUE, $handlerConfig, $path);
return $handlerManager->getHandler($path);
}
}