-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Copy pathAbstractCustomer.php
284 lines (249 loc) · 9.13 KB
/
AbstractCustomer.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CustomerImportExport\Model\Import;
use Magento\ImportExport\Model\Import;
use Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\Storage;
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
/**
* Import entity abstract customer model
*
* @api
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @since 100.0.2
*/
abstract class AbstractCustomer extends \Magento\ImportExport\Model\Import\Entity\AbstractEav
{
/**#@+
* Permanent column names
*
* Names that begins with underscore is not an attribute. This name convention is for
* to avoid interference with same attribute name.
*/
const COLUMN_WEBSITE = '_website';
const COLUMN_EMAIL = '_email';
const COLUMN_DEFAULT_BILLING = 'default_billing';
const COLUMN_DEFAULT_SHIPPING = 'default_shipping';
/**#@-*/
/**#@+
* Error codes
*/
const ERROR_WEBSITE_IS_EMPTY = 'websiteIsEmpty';
const ERROR_EMAIL_IS_EMPTY = 'emailIsEmpty';
const ERROR_INVALID_WEBSITE = 'invalidWebsite';
const ERROR_INVALID_EMAIL = 'invalidEmail';
const ERROR_VALUE_IS_REQUIRED = 'valueIsRequired';
const ERROR_CUSTOMER_NOT_FOUND = 'customerNotFound';
/**#@-*/
/**#@-*/
protected $_ignoredAttributes = ['website_id', 'store_id',
self::COLUMN_DEFAULT_BILLING, self::COLUMN_DEFAULT_SHIPPING];
/**
* Customer collection wrapper
*
* @var Storage
*/
protected $_customerStorage;
/**
* @var \Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\StorageFactory
*/
protected $_storageFactory;
/**
* If we should check column names
*
* @var bool
*/
protected $needColumnCheck = true;
/**
* {@inheritdoc}
*/
protected $masterAttributeCode = '_email';
/**
* @param \Magento\Framework\Stdlib\StringUtils $string
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\ImportExport\Model\ImportFactory $importFactory
* @param \Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper
* @param \Magento\Framework\App\ResourceConnection $resource
* @param ProcessingErrorAggregatorInterface $errorAggregator
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\ImportExport\Model\Export\Factory $collectionFactory
* @param \Magento\Eav\Model\Config $eavConfig
* @param \Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\StorageFactory $storageFactory
* @param array $data
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\Stdlib\StringUtils $string,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\ImportExport\Model\ImportFactory $importFactory,
\Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper,
\Magento\Framework\App\ResourceConnection $resource,
ProcessingErrorAggregatorInterface $errorAggregator,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\ImportExport\Model\Export\Factory $collectionFactory,
\Magento\Eav\Model\Config $eavConfig,
\Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\StorageFactory $storageFactory,
array $data = []
) {
$this->_storageFactory = $storageFactory;
parent::__construct(
$string,
$scopeConfig,
$importFactory,
$resourceHelper,
$resource,
$errorAggregator,
$storeManager,
$collectionFactory,
$eavConfig,
$data
);
$this->addMessageTemplate(self::ERROR_WEBSITE_IS_EMPTY, __('Please specify a website.'));
$this->addMessageTemplate(
self::ERROR_EMAIL_IS_EMPTY,
__("An email wasn't specified. Enter the email and try again.")
);
$this->addMessageTemplate(self::ERROR_INVALID_WEBSITE, __('We found an invalid value in a website column.'));
$this->addMessageTemplate(self::ERROR_INVALID_EMAIL, __('Please enter a valid email.'));
$this->addMessageTemplate(self::ERROR_VALUE_IS_REQUIRED, __('Please make sure attribute "%s" is not empty.'));
$this->addMessageTemplate(
self::ERROR_CUSTOMER_NOT_FOUND,
__('We can\'t find a customer who matches this email and website code.')
);
$this->_initCustomers($data)->_initWebsites(true);
}
/**
* Initialize existent customers data
*
* @param array $data
* @return $this
*/
protected function _initCustomers(array $data)
{
if (!isset($data['page_size'])) {
$data['page_size'] = $this->_pageSize;
}
$this->_customerStorage = isset(
$data['customer_storage']
) ? $data['customer_storage'] : $this->_storageFactory->create(
['data' => $data]
);
return $this;
}
/**
* Get customer id if customer is present in database
*
* @param string $email
* @param string $websiteCode
* @return bool|int
*/
protected function _getCustomerId($email, $websiteCode)
{
$email = strtolower(trim($email));
if (isset($this->_websiteCodeToId[$websiteCode])) {
$websiteId = $this->_websiteCodeToId[$websiteCode];
return $this->_customerStorage->getCustomerId($email, $websiteId);
}
return false;
}
/**
* Validate data row
*
* @param array $rowData
* @param int $rowNumber
* @return bool
*/
public function validateRow(array $rowData, $rowNumber)
{
if (isset($this->_validatedRows[$rowNumber])) {
// check that row is already validated
return !$this->getErrorAggregator()->isRowInvalid($rowNumber);
}
$this->_validatedRows[$rowNumber] = true;
$this->_processedEntitiesCount++;
if ($this->getBehavior($rowData) == \Magento\ImportExport\Model\Import::BEHAVIOR_ADD_UPDATE) {
$this->_validateRowForUpdate($rowData, $rowNumber);
} elseif ($this->getBehavior($rowData) == \Magento\ImportExport\Model\Import::BEHAVIOR_DELETE) {
$this->_validateRowForDelete($rowData, $rowNumber);
}
return !$this->getErrorAggregator()->isRowInvalid($rowNumber);
}
/**
* Validate data row for add/update behaviour
*
* @param array $rowData
* @param int $rowNumber
* @return null
*/
abstract protected function _validateRowForUpdate(array $rowData, $rowNumber);
/**
* Validate data row for delete behaviour
*
* @param array $rowData
* @param int $rowNumber
* @return null
*/
abstract protected function _validateRowForDelete(array $rowData, $rowNumber);
/**
* General check of unique key
*
* @param array $rowData
* @param int $rowNumber
* @return bool
*/
protected function _checkUniqueKey(array $rowData, $rowNumber)
{
if (empty($rowData[static::COLUMN_WEBSITE])) {
$this->addRowError(static::ERROR_WEBSITE_IS_EMPTY, $rowNumber, static::COLUMN_WEBSITE);
} elseif (empty($rowData[static::COLUMN_EMAIL])) {
$this->addRowError(static::ERROR_EMAIL_IS_EMPTY, $rowNumber, static::COLUMN_EMAIL);
} else {
$email = strtolower($rowData[static::COLUMN_EMAIL]);
$website = $rowData[static::COLUMN_WEBSITE];
if (!\Zend_Validate::is($email, \Magento\Framework\Validator\EmailAddress::class)) {
$this->addRowError(static::ERROR_INVALID_EMAIL, $rowNumber, static::COLUMN_EMAIL);
} elseif (!isset($this->_websiteCodeToId[$website])) {
$this->addRowError(static::ERROR_INVALID_WEBSITE, $rowNumber, static::COLUMN_WEBSITE);
}
}
return !$this->getErrorAggregator()->isRowInvalid($rowNumber);
}
/**
* Get customer storage
*
* @return Storage
*/
public function getCustomerStorage()
{
return $this->_customerStorage;
}
/**
* Returns id of option by value for select and multiselect attributes
*
* @param array $attributeParameters Parameters of an attribute
* @param int|string $value A value of an attribute
* @return int An option id of attribute
* @since 100.2.0
*/
protected function getSelectAttrIdByValue(array $attributeParameters, $value)
{
return isset($attributeParameters['options'][strtolower($value)])
? $attributeParameters['options'][strtolower($value)]
: 0;
}
/**
* Returns multiple value separator
*
* @return string
* @since 100.2.0
*/
protected function getMultipleValueSeparator()
{
return isset($this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR])
? $this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]
: Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR;
}
}