13
13
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
14
14
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
15
15
use Magento \Store \Model \StoreManagerInterface ;
16
+ use Magento \Customer \Api \Data \CustomerInterface ;
17
+ use Magento \Customer \Api \Data \CustomerInterfaceFactory ;
18
+ use Magento \Framework \Api \DataObjectHelper ;
19
+ use Magento \Framework \Reflection \DataObjectProcessor ;
16
20
17
21
/**
18
22
* Update customer data
@@ -34,19 +38,51 @@ class UpdateCustomerData
34
38
*/
35
39
private $ checkCustomerPassword ;
36
40
41
+ /**
42
+ * @var CustomerInterfaceFactory
43
+ */
44
+ private $ customerFactory ;
45
+
46
+ /**
47
+ * @var DataObjectHelper
48
+ */
49
+ private $ dataObjectHelper ;
50
+
51
+ /**
52
+ * @var DataObjectProcessor
53
+ */
54
+ private $ dataObjectProcessor ;
55
+
56
+ /**
57
+ * @var array
58
+ */
59
+ private $ restrictedKeys ;
60
+
37
61
/**
38
62
* @param CustomerRepositoryInterface $customerRepository
39
63
* @param StoreManagerInterface $storeManager
40
64
* @param CheckCustomerPassword $checkCustomerPassword
65
+ * @param CustomerInterfaceFactory $customerFactory
66
+ * @param DataObjectHelper $dataObjectHelper
67
+ * @param DataObjectProcessor $dataObjectProcessor
68
+ * @param array $restrictedKeys
41
69
*/
42
70
public function __construct (
43
71
CustomerRepositoryInterface $ customerRepository ,
44
72
StoreManagerInterface $ storeManager ,
45
- CheckCustomerPassword $ checkCustomerPassword
73
+ CheckCustomerPassword $ checkCustomerPassword ,
74
+ CustomerInterfaceFactory $ customerFactory ,
75
+ DataObjectHelper $ dataObjectHelper ,
76
+ DataObjectProcessor $ dataObjectProcessor ,
77
+ array $ restrictedKeys = []
46
78
) {
47
79
$ this ->customerRepository = $ customerRepository ;
48
80
$ this ->storeManager = $ storeManager ;
49
81
$ this ->checkCustomerPassword = $ checkCustomerPassword ;
82
+ $ this ->customerFactory = $ customerFactory ;
83
+ $ this ->dataObjectHelper = $ dataObjectHelper ;
84
+ $ this ->dataObjectProcessor = $ dataObjectProcessor ;
85
+ $ this ->restrictedKeys = $ restrictedKeys ;
50
86
}
51
87
52
88
/**
@@ -62,14 +98,17 @@ public function __construct(
62
98
public function execute (int $ customerId , array $ data ): void
63
99
{
64
100
$ customer = $ this ->customerRepository ->getById ($ customerId );
101
+ $ newData = array_diff_key ($ data , array_flip ($ this ->restrictedKeys ));
65
102
66
- if (isset ($ data ['firstname ' ])) {
67
- $ customer ->setFirstname ($ data ['firstname ' ]);
68
- }
103
+ $ oldData = $ this ->dataObjectProcessor ->buildOutputDataArray ($ customer , CustomerInterface::class);
104
+ $ newData = array_merge ($ oldData , $ newData );
69
105
70
- if (isset ($ data ['lastname ' ])) {
71
- $ customer ->setLastname ($ data ['lastname ' ]);
72
- }
106
+ $ customer = $ this ->customerFactory ->create ();
107
+ $ this ->dataObjectHelper ->populateWithArray (
108
+ $ customer ,
109
+ $ newData ,
110
+ CustomerInterface::class
111
+ );
73
112
74
113
if (isset ($ data ['email ' ]) && $ customer ->getEmail () !== $ data ['email ' ]) {
75
114
if (!isset ($ data ['password ' ]) || empty ($ data ['password ' ])) {
0 commit comments