5
5
*/
6
6
namespace Magento \CustomerImportExport \Model \ResourceModel \Import \Customer ;
7
7
8
- use Magento \Customer \Api \CustomerRepositoryInterface ;
9
- use Magento \Framework \Api \SearchCriteriaBuilder ;
10
- use Magento \Framework \App \ObjectManager ;
11
8
use Magento \Framework \DataObject ;
12
- use Magento \Framework \Exception \NoSuchEntityException ;
13
- use Magento \Framework \Api \FilterBuilder ;
9
+ use Magento \Framework \DB \Select ;
10
+ use Magento \Customer \Model \ResourceModel \Customer \CollectionFactory as CustomerCollectionFactory ;
11
+ use Magento \Customer \Model \ResourceModel \Customer \Collection as CustomerCollection ;
14
12
15
13
class Storage
16
14
{
@@ -25,7 +23,7 @@ class Storage
25
23
/**
26
24
* Customer collection
27
25
*
28
- * @var \Magento\Customer\Model\ResourceModel\Customer\Collection
26
+ * @var CustomerCollection
29
27
* @deprecated
30
28
*/
31
29
protected $ _customerCollection ;
@@ -48,48 +46,30 @@ class Storage
48
46
* Number of items to fetch from db in one query
49
47
*
50
48
* @var int
51
- * @deprecated
52
49
*/
53
50
protected $ _pageSize ;
54
51
55
52
/**
56
53
* Collection by pages iterator
57
54
*
58
55
* @var \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIterator
59
- * @deprecated
60
56
*/
61
57
protected $ _byPagesIterator ;
62
58
63
59
/**
64
- * @var CustomerRepositoryInterface
65
- */
66
- private $ customerRepository ;
67
-
68
- /**
69
- * @var SearchCriteriaBuilder
70
- */
71
- private $ searchCriteriaBuilder ;
72
-
73
- /**
74
- * @var FilterBuilder
60
+ * @var CustomerCollectionFactory
75
61
*/
76
- private $ filterBuilder ;
62
+ private $ customerCollectionFactory ;
77
63
78
64
/**
79
- * @param \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $collectionFactory
65
+ * @param CustomerCollectionFactory $collectionFactory
80
66
* @param \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $colIteratorFactory
81
67
* @param array $data
82
- * @param CustomerRepositoryInterface|null $customerRepository
83
- * @param SearchCriteriaBuilder|null $searchCriteriaBuilder
84
- * @param FilterBuilder|null $filterBuilder
85
68
*/
86
69
public function __construct (
87
- \ Magento \ Customer \ Model \ ResourceModel \ Customer \ CollectionFactory $ collectionFactory ,
70
+ CustomerCollectionFactory $ collectionFactory ,
88
71
\Magento \ImportExport \Model \ResourceModel \CollectionByPagesIteratorFactory $ colIteratorFactory ,
89
- array $ data = [],
90
- CustomerRepositoryInterface $ customerRepository = null ,
91
- SearchCriteriaBuilder $ searchCriteriaBuilder = null ,
92
- FilterBuilder $ filterBuilder = null
72
+ array $ data = []
93
73
) {
94
74
$ this ->_customerCollection = isset (
95
75
$ data ['customer_collection ' ]
@@ -98,13 +78,7 @@ public function __construct(
98
78
$ this ->_byPagesIterator = isset (
99
79
$ data ['collection_by_pages_iterator ' ]
100
80
) ? $ data ['collection_by_pages_iterator ' ] : $ colIteratorFactory ->create ();
101
- $ this ->customerRepository = $ customerRepository
102
- ?: ObjectManager::getInstance ()
103
- ->get (CustomerRepositoryInterface::class);
104
- $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder
105
- ?: ObjectManager::getInstance ()->get (SearchCriteriaBuilder::class);
106
- $ this ->filterBuilder = $ filterBuilder
107
- ?: ObjectManager::getInstance ()->get (FilterBuilder::class);
81
+ $ this ->customerCollectionFactory = $ collectionFactory ;
108
82
}
109
83
110
84
/**
@@ -131,6 +105,49 @@ public function load()
131
105
}
132
106
}
133
107
108
+ /**
109
+ * Create new collection to load customer data with proper filters.
110
+ *
111
+ * @param array[] $customerIdentifiers With keys "email" and "website_id".
112
+ *
113
+ * @return CustomerCollection
114
+ */
115
+ private function prepareCollection (array $ customerIdentifiers )
116
+ {
117
+ /** @var CustomerCollection $collection */
118
+ $ collection = $ this ->customerCollectionFactory ->create ();
119
+ $ collection ->removeAttributeToSelect ();
120
+ $ select = $ collection ->getSelect ();
121
+ $ customerTableId = array_keys ($ select ->getPart (Select::FROM ))[0 ];
122
+ $ select ->where (
123
+ $ customerTableId .'.email in (?) ' ,
124
+ array_map (
125
+ function (array $ customer ) {
126
+ return $ customer ['email ' ];
127
+ },
128
+ $ customerIdentifiers
129
+ )
130
+ );
131
+
132
+ return $ collection ;
133
+ }
134
+
135
+ /**
136
+ * Load customers' data that can be found by given identifiers.
137
+ *
138
+ * @param array $customerIdentifiers With keys "email" and "website_id".
139
+ *
140
+ * @return void
141
+ */
142
+ private function loadCustomersData (array $ customerIdentifiers )
143
+ {
144
+ $ this ->_byPagesIterator ->iterate (
145
+ $ this ->prepareCollection ($ customerIdentifiers ),
146
+ $ this ->_pageSize ,
147
+ [[$ this , 'addCustomer ' ]]
148
+ );
149
+ }
150
+
134
151
/**
135
152
* Add customer to array
136
153
*
@@ -163,21 +180,9 @@ public function getCustomerId($email, $websiteId)
163
180
if (!array_key_exists ($ email , $ this ->_customerIds )
164
181
|| !array_key_exists ($ websiteId , $ this ->_customerIds [$ email ])
165
182
) {
166
- try {
167
- $ customer = $ this ->customerRepository ->get ($ email , $ websiteId );
168
- $ customerData = new DataObject ([
169
- 'id ' => $ customer ->getId (),
170
- 'email ' => $ customer ->getEmail (),
171
- 'website_id ' => $ customer ->getWebsiteId ()
172
- ]);
173
- } catch (NoSuchEntityException $ exception ) {
174
- $ customerData = new DataObject ([
175
- 'id ' => null ,
176
- 'email ' => $ email ,
177
- 'website_id ' => $ websiteId
178
- ]);
179
- }
180
- $ this ->addCustomer ($ customerData );
183
+ $ this ->loadCustomersData ([
184
+ ['email ' => $ email , 'website_id ' => $ websiteId ]
185
+ ]);
181
186
}
182
187
183
188
if (isset ($ this ->_customerIds [$ email ][$ websiteId ])) {
@@ -195,47 +200,31 @@ public function getCustomerId($email, $websiteId)
195
200
*/
196
201
public function prepareCustomers (array $ customersToFind )
197
202
{
198
- $ customersData = [];
199
- $ filters = [];
203
+ $ identifiers = [];
200
204
foreach ($ customersToFind as $ customerToFind ) {
201
205
$ email = mb_strtolower ($ customerToFind ['email ' ]);
202
206
$ websiteId = $ customerToFind ['website_id ' ];
203
207
if (!array_key_exists ($ email , $ this ->_customerIds )
204
208
|| !array_key_exists ($ websiteId , $ this ->_customerIds [$ email ])
205
209
) {
206
210
//Only looking for customers we don't already have ID for.
207
- $ customersData [] = [
211
+ $ uniqueKey = $ email .'_ ' .$ websiteId ;
212
+ $ identifiers [$ uniqueKey ] = [
208
213
'email ' => $ email ,
209
214
'website_id ' => $ websiteId
210
215
];
211
- $ filters [] = $ this ->filterBuilder
212
- ->setField ('email ' )
213
- ->setValue ($ email )
214
- ->setConditionType ('eq ' )
215
- ->create ();
216
216
}
217
217
}
218
- if (!$ customersData ) {
218
+ if (!$ identifiers ) {
219
219
return ;
220
220
}
221
+ //Loading customers data.
222
+ $ this ->loadCustomersData ($ identifiers );
221
223
222
- $ this ->searchCriteriaBuilder ->addFilters ($ filters );
223
-
224
- //Adding customers that we found.
225
- $ found = $ this ->customerRepository ->getList (
226
- $ this ->searchCriteriaBuilder ->create ()
227
- );
228
- foreach ($ found ->getItems () as $ customer ) {
229
- $ this ->addCustomer (new DataObject ([
230
- 'id ' => $ customer ->getId (),
231
- 'email ' => $ customer ->getEmail (),
232
- 'website_id ' => $ customer ->getWebsiteId ()
233
- ]));
234
- }
235
224
//Adding customers that don't exist.
236
- foreach ($ customersData as $ customerData ) {
237
- $ email = $ customerData ['email ' ];
238
- $ websiteId = $ customerData ['website_id ' ];
225
+ foreach ($ identifiers as $ customerIdentity ) {
226
+ $ email = $ customerIdentity ['email ' ];
227
+ $ websiteId = $ customerIdentity ['website_id ' ];
239
228
if (!array_key_exists ($ email , $ this ->_customerIds )
240
229
|| !array_key_exists ($ websiteId , $ this ->_customerIds [$ email ])
241
230
) {
0 commit comments