9
9
10
10
use Magento \Customer \Api \CustomerRepositoryInterface ;
11
11
use Magento \Framework \Exception \AlreadyExistsException ;
12
+ use Magento \Framework \Exception \LocalizedException ;
13
+ use Magento \Framework \Exception \NoSuchEntityException ;
12
14
use Magento \Framework \GraphQl \Exception \GraphQlAlreadyExistsException ;
15
+ use Magento \Framework \GraphQl \Exception \GraphQlAuthenticationException ;
13
16
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
14
17
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
15
18
use Magento \Store \Model \StoreManagerInterface ;
16
19
use Magento \Customer \Api \Data \CustomerInterface ;
17
- use Magento \Customer \Api \Data \CustomerInterfaceFactory ;
18
20
use Magento \Framework \Api \DataObjectHelper ;
19
- use Magento \Framework \Reflection \DataObjectProcessor ;
20
21
21
22
/**
22
23
* Update customer data
@@ -38,21 +39,11 @@ class UpdateCustomerData
38
39
*/
39
40
private $ checkCustomerPassword ;
40
41
41
- /**
42
- * @var CustomerInterfaceFactory
43
- */
44
- private $ customerFactory ;
45
-
46
42
/**
47
43
* @var DataObjectHelper
48
44
*/
49
45
private $ dataObjectHelper ;
50
46
51
- /**
52
- * @var DataObjectProcessor
53
- */
54
- private $ dataObjectProcessor ;
55
-
56
47
/**
57
48
* @var array
58
49
*/
@@ -62,26 +53,20 @@ class UpdateCustomerData
62
53
* @param CustomerRepositoryInterface $customerRepository
63
54
* @param StoreManagerInterface $storeManager
64
55
* @param CheckCustomerPassword $checkCustomerPassword
65
- * @param CustomerInterfaceFactory $customerFactory
66
56
* @param DataObjectHelper $dataObjectHelper
67
- * @param DataObjectProcessor $dataObjectProcessor
68
57
* @param array $restrictedKeys
69
58
*/
70
59
public function __construct (
71
60
CustomerRepositoryInterface $ customerRepository ,
72
61
StoreManagerInterface $ storeManager ,
73
62
CheckCustomerPassword $ checkCustomerPassword ,
74
- CustomerInterfaceFactory $ customerFactory ,
75
63
DataObjectHelper $ dataObjectHelper ,
76
- DataObjectProcessor $ dataObjectProcessor ,
77
64
array $ restrictedKeys = []
78
65
) {
79
66
$ this ->customerRepository = $ customerRepository ;
80
67
$ this ->storeManager = $ storeManager ;
81
68
$ this ->checkCustomerPassword = $ checkCustomerPassword ;
82
- $ this ->customerFactory = $ customerFactory ;
83
69
$ this ->dataObjectHelper = $ dataObjectHelper ;
84
- $ this ->dataObjectProcessor = $ dataObjectProcessor ;
85
70
$ this ->restrictedKeys = $ restrictedKeys ;
86
71
}
87
72
@@ -91,24 +76,23 @@ public function __construct(
91
76
* @param int $customerId
92
77
* @param array $data
93
78
* @return void
94
- * @throws GraphQlNoSuchEntityException
95
- * @throws GraphQlInputException
96
79
* @throws GraphQlAlreadyExistsException
80
+ * @throws GraphQlInputException
81
+ * @throws GraphQlNoSuchEntityException
82
+ * @throws GraphQlAuthenticationException
97
83
*/
98
84
public function execute (int $ customerId , array $ data ): void
99
85
{
100
- $ customer = $ this ->customerRepository ->getById ($ customerId );
101
- $ newData = array_diff_key ($ data , array_flip ($ this ->restrictedKeys ));
102
-
103
- $ oldData = $ this ->dataObjectProcessor ->buildOutputDataArray ($ customer , CustomerInterface::class);
104
- $ newData = array_merge ($ oldData , $ newData );
86
+ try {
87
+ $ customer = $ this ->customerRepository ->getById ($ customerId );
88
+ } catch (NoSuchEntityException $ e ) {
89
+ throw new GraphQlNoSuchEntityException (__ ($ e ->getMessage ()), $ e );
90
+ } catch (LocalizedException $ e ) {
91
+ throw new GraphQlInputException (__ ($ e ->getMessage ()), $ e );
92
+ }
105
93
106
- $ customer = $ this ->customerFactory ->create ();
107
- $ this ->dataObjectHelper ->populateWithArray (
108
- $ customer ,
109
- $ newData ,
110
- CustomerInterface::class
111
- );
94
+ $ filteredData = array_diff_key ($ data , array_flip ($ this ->restrictedKeys ));
95
+ $ this ->dataObjectHelper ->populateWithArray ($ customer , $ filteredData , CustomerInterface::class);
112
96
113
97
if (isset ($ data ['email ' ]) && $ customer ->getEmail () !== $ data ['email ' ]) {
114
98
if (!isset ($ data ['password ' ]) || empty ($ data ['password ' ])) {
@@ -128,6 +112,8 @@ public function execute(int $customerId, array $data): void
128
112
__ ('A customer with the same email address already exists in an associated website. ' ),
129
113
$ e
130
114
);
115
+ } catch (LocalizedException $ e ) {
116
+ throw new GraphQlInputException (__ ($ e ->getMessage ()), $ e );
131
117
}
132
118
}
133
119
}
0 commit comments