forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractEntity.php
514 lines (455 loc) · 12.2 KB
/
AbstractEntity.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ImportExport\Model\Export;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\ImportExport\Model\Export\Adapter\AbstractAdapter;
use Magento\ImportExport\Model\Export;
/**
* Export entity abstract model
*
* @author Magento Core Team <core@magentocommerce.com>
* @SuppressWarnings(PHPMD.TooManyFields)
*/
abstract class AbstractEntity
{
/**#@+
* Attribute collection name
*/
const ATTRIBUTE_COLLECTION_NAME = 'Magento\Framework\Data\Collection';
/**#@-*/
/**#@+
* XML path to page size parameter
*/
const XML_PATH_PAGE_SIZE = '';
/**#@-*/
/**
* Store manager
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* Error codes with arrays of corresponding row numbers
*
* @var array
*/
protected $_errors = [];
/**
* Error counter
*
* @var int
*/
protected $_errorsCount = 0;
/**
* Limit of errors after which pre-processing will exit
*
* @var int
*/
protected $_errorsLimit = 100;
/**
* Validation information about processed rows
*
* @var array
*/
protected $_invalidRows = [];
/**
* Validation failure message template definitions
*
* @var array
*/
protected $_messageTemplates = [];
/**
* Parameters
*
* @var array
*/
protected $_parameters = [];
/**
* Number of entities processed by validation
*
* @var int
*/
protected $_processedEntitiesCount = 0;
/**
* Number of rows processed by validation
*
* @var int
*/
protected $_processedRowsCount = 0;
/**
* Source model
*
* @var AbstractAdapter
*/
protected $_writer;
/**
* Array of pairs store ID to its code
*
* @var array
*/
protected $_storeIdToCode = [];
/**
* Website ID-to-code
*
* @var array
*/
protected $_websiteIdToCode = [];
/**
* Disabled attributes
*
* @var string[]
*/
protected $_disabledAttributes = [];
/**
* Export file name
*
* @var string|null
*/
protected $_fileName = null;
/**
* Address attributes collection
*
* @var \Magento\Framework\Data\Collection
*/
protected $_attributeCollection;
/**
* Number of items to fetch from db in one query
*
* @var int
*/
protected $_pageSize;
/**
* Collection by pages iterator
*
* @var \Magento\ImportExport\Model\Resource\CollectionByPagesIterator
*/
protected $_byPagesIterator;
/**
* Core store config
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $_scopeConfig;
/**
* Attribute code to its values. Only attributes with options and only default store values used
*
* @var array
*/
protected $_attributeCodes = null;
/**
* Permanent entity columns
*
* @var string[]
*/
protected $_permanentAttributes = [];
/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\ImportExport\Model\Export\Factory $collectionFactory
* @param \Magento\ImportExport\Model\Resource\CollectionByPagesIteratorFactory $resourceColFactory
* @param array $data
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\ImportExport\Model\Export\Factory $collectionFactory,
\Magento\ImportExport\Model\Resource\CollectionByPagesIteratorFactory $resourceColFactory,
array $data = []
) {
$this->_scopeConfig = $scopeConfig;
$this->_storeManager = $storeManager;
$this->_attributeCollection = isset(
$data['attribute_collection']
) ? $data['attribute_collection'] : $collectionFactory->create(
static::ATTRIBUTE_COLLECTION_NAME
);
$this->_pageSize = isset(
$data['page_size']
) ? $data['page_size'] : (static::XML_PATH_PAGE_SIZE ? (int)$this->_scopeConfig->getValue(
static::XML_PATH_PAGE_SIZE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) : 0);
$this->_byPagesIterator = isset(
$data['collection_by_pages_iterator']
) ? $data['collection_by_pages_iterator'] : $resourceColFactory->create();
}
/**
* Initialize stores hash
*
* @return $this
*/
protected function _initStores()
{
/** @var $store \Magento\Store\Model\Store */
foreach ($this->_storeManager->getStores(true) as $store) {
$this->_storeIdToCode[$store->getId()] = $store->getCode();
}
ksort($this->_storeIdToCode);
// to ensure that 'admin' store (ID is zero) goes first
return $this;
}
/**
* Initialize website values
*
* @param bool $withDefault
* @return $this
*/
protected function _initWebsites($withDefault = false)
{
/** @var $website \Magento\Store\Model\Website */
foreach ($this->_storeManager->getWebsites($withDefault) as $website) {
$this->_websiteIdToCode[$website->getId()] = $website->getCode();
}
return $this;
}
/**
* Add error with corresponding current data source row number
*
* @param string $errorCode Error code or simply column name
* @param int $errorRowNum Row number
* @return $this
*/
public function addRowError($errorCode, $errorRowNum)
{
$errorCode = (string)$errorCode;
$this->_errors[$errorCode][] = $errorRowNum + 1;
// one added for human readability
$this->_invalidRows[$errorRowNum] = true;
$this->_errorsCount++;
return $this;
}
/**
* Add message template for specific error code from outside
*
* @param string $errorCode Error code
* @param string $message Message template
* @return $this
*/
public function addMessageTemplate($errorCode, $message)
{
$this->_messageTemplates[$errorCode] = $message;
return $this;
}
/**
* Export process
*
* @return string
*/
abstract public function export();
/**
* Export one item
*
* @param \Magento\Framework\Model\AbstractModel $item
* @return void
*/
abstract public function exportItem($item);
/**
* Iterate through given collection page by page and export items
*
* @param \Magento\Framework\Data\Collection\AbstractDb $collection
* @return void
*/
protected function _exportCollectionByPages(\Magento\Framework\Data\Collection\AbstractDb $collection)
{
$this->_byPagesIterator->iterate($collection, $this->_pageSize, [[$this, 'exportItem']]);
}
/**
* Get attributes codes which are appropriate for export
*
* @return array
*/
protected function _getExportAttributeCodes()
{
if (null === $this->_attributeCodes) {
if (!empty($this->_parameters[Export::FILTER_ELEMENT_SKIP])
&& is_array($this->_parameters[Export::FILTER_ELEMENT_SKIP])
) {
$skippedAttributes = array_flip(
$this->_parameters[Export::FILTER_ELEMENT_SKIP]
);
} else {
$skippedAttributes = [];
}
$attributeCodes = [];
/** @var $attribute AbstractAttribute */
foreach ($this->filterAttributeCollection($this->getAttributeCollection()) as $attribute) {
if (!isset($skippedAttributes[$attribute->getAttributeId()])
|| in_array($attribute->getAttributeCode(), $this->_permanentAttributes)
) {
$attributeCodes[] = $attribute->getAttributeCode();
}
}
$this->_attributeCodes = $attributeCodes;
}
return $this->_attributeCodes;
}
/**
* Entity type code getter
*
* @abstract
* @return string
*/
abstract public function getEntityTypeCode();
/**
* Get header columns
*
* @return array
*/
abstract protected function _getHeaderColumns();
/**
* Get entity collection
*
* @return \Magento\Framework\Data\Collection\AbstractDb
*/
abstract protected function _getEntityCollection();
/**
* Entity attributes collection getter
*
* @return \Magento\Framework\Data\Collection
*/
public function getAttributeCollection()
{
return $this->_attributeCollection;
}
/**
* Clean up attribute collection
*
* @param \Magento\Framework\Data\Collection $collection
* @return \Magento\Framework\Data\Collection
*/
public function filterAttributeCollection(\Magento\Framework\Data\Collection $collection)
{
/** @var $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */
foreach ($collection as $attribute) {
if (in_array($attribute->getAttributeCode(), $this->_disabledAttributes)) {
$collection->removeItemByKey($attribute->getId());
}
}
return $collection;
}
/**
* Returns error information
*
* @return array
*/
public function getErrorMessages()
{
$messages = [];
foreach ($this->_errors as $errorCode => $errorRows) {
$message = isset(
$this->_messageTemplates[$errorCode]
) ? __(
$this->_messageTemplates[$errorCode]
) : __(
"Please correct the value for '%1' column",
$errorCode
);
$message = (string)$message;
$messages[$message] = $errorRows;
}
return $messages;
}
/**
* Returns error counter value
*
* @return int
*/
public function getErrorsCount()
{
return $this->_errorsCount;
}
/**
* Returns invalid rows count
*
* @return int
*/
public function getInvalidRowsCount()
{
return count($this->_invalidRows);
}
/**
* Returns number of checked entities
*
* @return int
*/
public function getProcessedEntitiesCount()
{
return $this->_processedEntitiesCount;
}
/**
* Returns number of checked rows
*
* @return int
*/
public function getProcessedRowsCount()
{
return $this->_processedRowsCount;
}
/**
* Inner writer object getter
*
* @return AbstractAdapter
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getWriter()
{
if (!$this->_writer) {
throw new \Magento\Framework\Exception\LocalizedException(__('Please specify writer.'));
}
return $this->_writer;
}
/**
* Set parameters
*
* @param string[] $parameters
* @return $this
*/
public function setParameters(array $parameters)
{
$this->_parameters = $parameters;
return $this;
}
/**
* Writer model setter
*
* @param AbstractAdapter $writer
* @return $this
*/
public function setWriter(AbstractAdapter $writer)
{
$this->_writer = $writer;
return $this;
}
/**
* Set export file name
*
* @param null|string $fileName
* @return void
*/
public function setFileName($fileName)
{
$this->_fileName = $fileName;
}
/**
* Get export file name
*
* @return null|string
*/
public function getFileName()
{
return $this->_fileName;
}
/**
* Retrieve list of disabled attributes codes
*
* @return string[]
*/
public function getDisabledAttributes()
{
return $this->_disabledAttributes;
}
}