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
19
23
*/
20
24
class UpdateCustomerData
21
25
{
22
- private const RESTRICTED_DATA_KEYS = ['email ' , 'password ' ];
23
-
24
26
/**
25
27
* @var CustomerRepositoryInterface
26
28
*/
@@ -36,19 +38,51 @@ class UpdateCustomerData
36
38
*/
37
39
private $ checkCustomerPassword ;
38
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
+
39
61
/**
40
62
* @param CustomerRepositoryInterface $customerRepository
41
63
* @param StoreManagerInterface $storeManager
42
64
* @param CheckCustomerPassword $checkCustomerPassword
65
+ * @param CustomerInterfaceFactory $customerFactory
66
+ * @param DataObjectHelper $dataObjectHelper
67
+ * @param DataObjectProcessor $dataObjectProcessor
68
+ * @param array $restrictedKeys
43
69
*/
44
70
public function __construct (
45
71
CustomerRepositoryInterface $ customerRepository ,
46
72
StoreManagerInterface $ storeManager ,
47
- CheckCustomerPassword $ checkCustomerPassword
73
+ CheckCustomerPassword $ checkCustomerPassword ,
74
+ CustomerInterfaceFactory $ customerFactory ,
75
+ DataObjectHelper $ dataObjectHelper ,
76
+ DataObjectProcessor $ dataObjectProcessor ,
77
+ array $ restrictedKeys = []
48
78
) {
49
79
$ this ->customerRepository = $ customerRepository ;
50
80
$ this ->storeManager = $ storeManager ;
51
81
$ this ->checkCustomerPassword = $ checkCustomerPassword ;
82
+ $ this ->customerFactory = $ customerFactory ;
83
+ $ this ->dataObjectHelper = $ dataObjectHelper ;
84
+ $ this ->dataObjectProcessor = $ dataObjectProcessor ;
85
+ $ this ->restrictedKeys = $ restrictedKeys ;
52
86
}
53
87
54
88
/**
@@ -64,15 +98,17 @@ public function __construct(
64
98
public function execute (int $ customerId , array $ data ): void
65
99
{
66
100
$ customer = $ this ->customerRepository ->getById ($ customerId );
67
- $ newCustomerData = array_diff_key ($ data , array_flip (static :: RESTRICTED_DATA_KEYS ));
101
+ $ newData = array_diff_key ($ data , array_flip ($ this -> restrictedKeys ));
68
102
69
- foreach ($ newCustomerData as $ key => $ value ) {
70
- $ setterMethod = 'set ' . ucwords ($ key , '_ ' );
71
- if (!method_exists ($ customer , $ setterMethod )) {
72
- continue ;
73
- }
74
- $ customer ->{$ setterMethod }($ value );
75
- }
103
+ $ oldData = $ this ->dataObjectProcessor ->buildOutputDataArray ($ customer , CustomerInterface::class);
104
+ $ newData = array_merge ($ oldData , $ newData );
105
+
106
+ $ customer = $ this ->customerFactory ->create ();
107
+ $ this ->dataObjectHelper ->populateWithArray (
108
+ $ customer ,
109
+ $ newData ,
110
+ CustomerInterface::class
111
+ );
76
112
77
113
if (isset ($ data ['email ' ]) && $ customer ->getEmail () !== $ data ['email ' ]) {
78
114
if (!isset ($ data ['password ' ]) || empty ($ data ['password ' ])) {
0 commit comments