1
1
<?php
2
2
namespace Ometria \Api \Controller \V1 ;
3
3
4
+ use Magento \Customer \Api \CustomerMetadataInterface ;
5
+ use Magento \Customer \Api \CustomerRepositoryInterface ;
4
6
use Magento \Customer \Api \Data \CustomerInterface ;
7
+ use Magento \Customer \Api \GroupRepositoryInterface ;
8
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
9
+ use Magento \Framework \App \Action \Context ;
10
+ use Magento \Framework \Controller \Result \JsonFactory ;
5
11
use Magento \Framework \Exception \LocalizedException ;
12
+ use Magento \Newsletter \Model \ResourceModel \Subscriber \Collection as SubscriberCollection ;
13
+ use Ometria \Api \Helper \CustomerData ;
6
14
use Ometria \Api \Helper \Format \V1 \Customers as Helper ;
7
- use Ometria \Api \Controller \V1 \Base ;
15
+ use Ometria \Api \Helper \Service \Filterable \Service as FilterableService ;
16
+ use Ometria \Core \Service \Customer \RewardPoints as RewardPointsService ;
8
17
9
18
class Customers extends Base
10
19
{
11
- protected $ resultJsonFactory ;
12
- protected $ repository ;
13
- protected $ customerMetadataInterface ;
14
- protected $ genderOptions ;
15
- protected $ subscriberCollection ;
16
- protected $ customerIdsOfNewsLetterSubscribers =[];
17
- protected $ customerDataHelper ;
18
- protected $ searchCriteriaBuilder ;
19
- protected $ groupRepository ;
20
- protected $ customerGroupNames ;
21
-
22
- public function __construct (
23
- \Magento \Framework \App \Action \Context $ context ,
24
- \Magento \Framework \Controller \Result \JsonFactory $ resultJsonFactory ,
25
- \Ometria \Api \Helper \Service \Filterable \Service $ apiHelperServiceFilterable ,
26
- \Magento \Customer \Api \CustomerRepositoryInterface $ customerRepository ,
27
- \Magento \Customer \Api \CustomerMetadataInterface $ customerMetadataInterface ,
28
- \Magento \Newsletter \Model \ResourceModel \Subscriber \Collection $ subscriberCollection ,
29
- \Ometria \Api \Helper \CustomerData $ customerDataHelper ,
30
- \Magento \Framework \Api \SearchCriteriaBuilder $ searchCriteriaBuilder ,
31
- \Magento \Customer \Api \GroupRepositoryInterface $ groupRepository
32
- ) {
33
- parent ::__construct ($ context );
34
- $ this ->resultJsonFactory = $ resultJsonFactory ;
35
- $ this ->apiHelperServiceFilterable = $ apiHelperServiceFilterable ;
36
- $ this ->repository = $ customerRepository ;
37
- $ this ->subscriberCollection = $ subscriberCollection ;
38
- $ this ->customerMetadataInterface = $ customerMetadataInterface ;
39
- $ this ->customerDataHelper = $ customerDataHelper ;
40
- $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
41
- $ this ->groupRepository = $ groupRepository ;
42
-
43
- $ this ->genderOptions = $ this ->customerMetadataInterface
20
+ /** @var JsonFactory */
21
+ private $ resultJsonFactory ;
22
+
23
+ /** @var CustomerRepositoryInterface */
24
+ private $ repository ;
25
+
26
+ /** @var CustomerMetadataInterface */
27
+ private $ customerMetadataInterface ;
28
+
29
+ /** @var SubscriberCollection */
30
+ private $ subscriberCollection ;
31
+
32
+ /** @var CustomerData */
33
+ private $ customerDataHelper ;
34
+
35
+ /** @var SearchCriteriaBuilder */
36
+ private $ searchCriteriaBuilder ;
37
+
38
+ /** @var GroupRepositoryInterface */
39
+ private $ groupRepository ;
40
+
41
+ /** @var RewardPointsService */
42
+ private $ rewardPointsService ;
43
+
44
+ /** @var array */
45
+ private $ customerIdsOfNewsLetterSubscribers = [];
46
+
47
+ /** @var null|array */
48
+ private $ customerGroupNames ;
49
+
50
+ /** @var array */
51
+ private $ genderOptions ;
52
+
53
+ public function __construct (
54
+ Context $ context ,
55
+ JsonFactory $ resultJsonFactory ,
56
+ FilterableService $ apiHelperServiceFilterable ,
57
+ CustomerRepositoryInterface $ customerRepository ,
58
+ CustomerMetadataInterface $ customerMetadataInterface ,
59
+ SubscriberCollection $ subscriberCollection ,
60
+ CustomerData $ customerDataHelper ,
61
+ SearchCriteriaBuilder $ searchCriteriaBuilder ,
62
+ GroupRepositoryInterface $ groupRepository ,
63
+ RewardPointsService $ rewardPointsService
64
+ ) {
65
+ parent ::__construct ($ context );
66
+
67
+ $ this ->resultJsonFactory = $ resultJsonFactory ;
68
+ $ this ->apiHelperServiceFilterable = $ apiHelperServiceFilterable ;
69
+ $ this ->repository = $ customerRepository ;
70
+ $ this ->subscriberCollection = $ subscriberCollection ;
71
+ $ this ->customerMetadataInterface = $ customerMetadataInterface ;
72
+ $ this ->customerDataHelper = $ customerDataHelper ;
73
+ $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
74
+ $ this ->groupRepository = $ groupRepository ;
75
+ $ this ->rewardPointsService = $ rewardPointsService ;
76
+ $ this ->genderOptions = $ this ->customerMetadataInterface
44
77
->getAttributeMetadata ('gender ' )
45
78
->getOptions ();
46
- }
79
+ }
47
80
48
81
public function execute ()
49
82
{
@@ -93,18 +126,18 @@ public function getItemsData($items)
93
126
94
127
$ new = Helper::getBlankArray ();
95
128
96
- $ new ["@type " ] = "contact " ;
97
- $ new ["id " ] = array_key_exists ('id ' , $ item ) ? $ item ['id ' ] : '' ;
98
- $ new ["email " ] = array_key_exists ('email ' , $ item ) ? $ item ['email ' ] : '' ;
99
- $ new ["prefix " ] = array_key_exists ('prefix ' , $ item ) ? $ item ['prefix ' ] : '' ;
100
- $ new ["firstname " ] = array_key_exists ('firstname ' , $ item ) ? $ item ['firstname ' ] : '' ;
101
- $ new ["middlename " ] = array_key_exists ('middlename ' , $ item ) ? $ item ['middlename ' ] : '' ;
102
- $ new ["lastname " ] = array_key_exists ('lastname ' , $ item ) ? $ item ['lastname ' ] : '' ;
103
- $ new ["gender " ] = $ this ->customerDataHelper ->getGenderLabel ($ item );
104
- $ new ["date_of_birth " ] = array_key_exists ('dob ' , $ item ) ? $ item ['dob ' ] : '' ;
129
+ $ new ["@type " ] = "contact " ;
130
+ $ new ["id " ] = array_key_exists ('id ' , $ item ) ? $ item ['id ' ] : '' ;
131
+ $ new ["email " ] = array_key_exists ('email ' , $ item ) ? $ item ['email ' ] : '' ;
132
+ $ new ["prefix " ] = array_key_exists ('prefix ' , $ item ) ? $ item ['prefix ' ] : '' ;
133
+ $ new ["firstname " ] = array_key_exists ('firstname ' , $ item ) ? $ item ['firstname ' ] : '' ;
134
+ $ new ["middlename " ] = array_key_exists ('middlename ' , $ item ) ? $ item ['middlename ' ] : '' ;
135
+ $ new ["lastname " ] = array_key_exists ('lastname ' , $ item ) ? $ item ['lastname ' ] : '' ;
136
+ $ new ["gender " ] = $ this ->customerDataHelper ->getGenderLabel ($ item );
137
+ $ new ["date_of_birth " ] = array_key_exists ('dob ' , $ item ) ? $ item ['dob ' ] : '' ;
105
138
$ new ["marketing_optin " ] = $ this ->getMarketingOption ($ item , $ subscriberCollection );
106
- $ new ["country_id " ] = $ this ->customerDataHelper ->getCountryId ($ item );
107
- $ new ["store_id " ] = array_key_exists ('store_id ' , $ item ) ? $ item ['store_id ' ] : null ;
139
+ $ new ["country_id " ] = $ this ->customerDataHelper ->getCountryId ($ item );
140
+ $ new ["store_id " ] = array_key_exists ('store_id ' , $ item ) ? $ item ['store_id ' ] : null ;
108
141
109
142
if ($ this ->_request ->getParam ('raw ' ) != null ) {
110
143
$ new ['_raw ' ] = $ item ;
@@ -117,44 +150,83 @@ public function getItemsData($items)
117
150
return $ new ;
118
151
}, $ items );
119
152
153
+ if ($ this ->rewardPointsService ->isRewardsAvailable ()) {
154
+ $ this ->appendRewardPoints ($ items );
155
+ }
156
+
120
157
return $ items ;
121
158
}
122
159
123
- public function getMarketingOption ($ item , $ subscriber_collection )
124
- {
125
- if (!array_key_exists ('id ' , $ item )) {
126
- return false ;
127
- }
128
-
129
- if (!$ this ->customerIdsOfNewsLetterSubscribers ) {
130
- foreach ($ subscriber_collection as $ subscriber ) {
131
- $ this ->customerIdsOfNewsLetterSubscribers [] = $ subscriber ->getCustomerId ();
132
- }
133
- }
134
-
135
- return in_array ($ item ['id ' ], $ this ->customerIdsOfNewsLetterSubscribers );
136
- }
137
-
138
- public function getSubscriberCollectionFromCustomerIds ($ customer_ids )
139
- {
140
- return $ this ->subscriberCollection
141
- ->addFieldToFilter ('customer_id ' , ['in ' => $ customer_ids ])
142
- ->addFieldToFilter ('subscriber_status ' , \Magento \Newsletter \Model \Subscriber::STATUS_SUBSCRIBED );
143
- }
144
-
145
- public function getSubscriberCollection ($ items )
146
- {
147
- $ customer_ids = array_map (function ($ item ){
148
- return $ item ['id ' ];
149
- }, $ items );
150
-
151
- return $ this ->getSubscriberCollectionFromCustomerIds ($ customer_ids );
152
- }
160
+ public function getMarketingOption ($ item , $ subscriber_collection )
161
+ {
162
+ if (!array_key_exists ('id ' , $ item )) {
163
+ return false ;
164
+ }
165
+
166
+ if (!$ this ->customerIdsOfNewsLetterSubscribers ) {
167
+ foreach ($ subscriber_collection as $ subscriber ) {
168
+ $ this ->customerIdsOfNewsLetterSubscribers [] = $ subscriber ->getCustomerId ();
169
+ }
170
+ }
171
+
172
+ return in_array ($ item ['id ' ], $ this ->customerIdsOfNewsLetterSubscribers );
173
+ }
174
+
175
+ /**
176
+ * @param $items
177
+ * @return SubscriberCollection
178
+ */
179
+ public function getSubscriberCollection ($ items )
180
+ {
181
+ $ customerIds = $ this ->getCustomerIds ($ items );
182
+
183
+ return $ this ->subscriberCollection
184
+ ->addFieldToFilter ('customer_id ' , ['in ' => $ customerIds ])
185
+ ->addFieldToFilter ('subscriber_status ' , \Magento \Newsletter \Model \Subscriber::STATUS_SUBSCRIBED );
186
+ }
153
187
154
188
/**
155
- * @param int $id
156
- * @return string|null
157
- * @throws \Magento\Framework\Exception\LocalizedException
189
+ * Add reward points to items
190
+ *
191
+ * @param $items
192
+ */
193
+ private function appendRewardPoints (&$ items )
194
+ {
195
+ $ customerIds = $ this ->getCustomerIds ($ items );
196
+
197
+ $ rewardPointsCollection = $ this ->rewardPointsService ->getRewardPointsCollection ($ customerIds )
198
+ ->addFieldToFilter ('customer_id ' , ['in ' => $ customerIds ]);
199
+
200
+ $ websiteIds = $ this ->_request ->getParam ('website_ids ' );
201
+ if ($ websiteIds ) {
202
+ $ rewardPointsCollection ->addWebsiteFilter ($ websiteIds );
203
+ }
204
+
205
+ foreach ($ items as &$ item ) {
206
+ $ reward = $ rewardPointsCollection ->getItemByColumnValue ('customer_id ' , $ item ['id ' ]);
207
+ if ($ reward ) {
208
+ $ item ['reward_points ' ] = $ reward ->getPointsBalance ();
209
+ }
210
+ }
211
+ }
212
+
213
+ /**
214
+ * @param $items
215
+ * @return array
216
+ */
217
+ private function getCustomerIds ($ items )
218
+ {
219
+ $ customerIds = array_map (function ($ item ){
220
+ return $ item ['id ' ];
221
+ }, $ items );
222
+
223
+ return $ customerIds ;
224
+ }
225
+
226
+ /**
227
+ * @param $id
228
+ * @return mixed|string|null
229
+ * @throws LocalizedException
158
230
*/
159
231
protected function getCustomerGroupName ($ id )
160
232
{
0 commit comments