7
7
8
8
namespace Magento \CustomerGraphQl \Model \Resolver ;
9
9
10
- use Magento \Authorization \Model \UserContextInterface ;
11
10
use Magento \Customer \Api \AddressRepositoryInterface ;
12
11
use Magento \Customer \Api \AddressMetadataManagementInterface ;
13
12
use Magento \Customer \Api \Data \AddressInterfaceFactory ;
14
13
use Magento \Customer \Api \Data \AddressInterface ;
14
+ use Magento \CustomerGraphQl \Model \Customer \AddressDataProvider ;
15
+ use Magento \CustomerGraphQl \Model \Customer \CheckCustomerAccount ;
16
+ use Magento \Eav \Model \Config ;
17
+ use Magento \Framework \Api \DataObjectHelper ;
15
18
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
16
19
use Magento \Framework \GraphQl \Config \Element \Field ;
17
20
use Magento \Framework \GraphQl \Query \ResolverInterface ;
18
- use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
19
21
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
20
- use Magento \CustomerGraphQl \Model \Resolver \Address \AddressDataProvider ;
21
- use Magento \CustomerGraphQl \Model \Resolver \Address \AddressConfigProvider ;
22
22
23
23
/**
24
24
* Customers address create, used for GraphQL request processing.
25
25
*/
26
- class AddressCreate implements ResolverInterface
26
+ class CreateCustomerAddress implements ResolverInterface
27
27
{
28
+ /**
29
+ * @var CheckCustomerAccount
30
+ */
31
+ private $ checkCustomerAccount ;
32
+
28
33
/**
29
34
* @var AddressRepositoryInterface
30
35
*/
31
- private $ addressRepositoryInterface ;
36
+ private $ addressRepository ;
32
37
33
38
/**
34
39
* @var AddressInterfaceFactory
@@ -41,26 +46,37 @@ class AddressCreate implements ResolverInterface
41
46
private $ addressDataProvider ;
42
47
43
48
/**
44
- * @var AddressConfigProvider
49
+ * @var Config
50
+ */
51
+ private $ eavConfig ;
52
+
53
+ /**
54
+ * @var DataObjectHelper
45
55
*/
46
- public $ addressConfigProvider ;
56
+ private $ dataObjectHelper ;
47
57
48
58
/**
49
- * @param AddressRepositoryInterface $addressRepositoryInterface
59
+ * @param CheckCustomerAccount $checkCustomerAccount
60
+ * @param AddressRepositoryInterface $addressRepository
50
61
* @param AddressInterfaceFactory $addressInterfaceFactory
51
62
* @param AddressDataProvider $addressDataProvider
52
- * @param AddressConfigProvider $addressConfigProvider
63
+ * @param Config $eavConfig
64
+ * @param DataObjectHelper $dataObjectHelper
53
65
*/
54
66
public function __construct (
55
- AddressRepositoryInterface $ addressRepositoryInterface ,
67
+ CheckCustomerAccount $ checkCustomerAccount ,
68
+ AddressRepositoryInterface $ addressRepository ,
56
69
AddressInterfaceFactory $ addressInterfaceFactory ,
57
70
AddressDataProvider $ addressDataProvider ,
58
- AddressConfigProvider $ addressConfigProvider
71
+ Config $ eavConfig ,
72
+ DataObjectHelper $ dataObjectHelper
59
73
) {
60
- $ this ->addressRepositoryInterface = $ addressRepositoryInterface ;
74
+ $ this ->checkCustomerAccount = $ checkCustomerAccount ;
75
+ $ this ->addressRepository = $ addressRepository ;
61
76
$ this ->addressInterfaceFactory = $ addressInterfaceFactory ;
62
77
$ this ->addressDataProvider = $ addressDataProvider ;
63
- $ this ->addressConfigProvider = $ addressConfigProvider ;
78
+ $ this ->eavConfig = $ eavConfig ;
79
+ $ this ->dataObjectHelper = $ dataObjectHelper ;
64
80
}
65
81
66
82
/**
@@ -73,18 +89,13 @@ public function resolve(
73
89
array $ value = null ,
74
90
array $ args = null
75
91
) {
76
- /** @var \Magento\Framework\GraphQl\Query\Resolver\ContextInterface $context */
77
- if ((!$ context ->getUserId ()) || $ context ->getUserType () == UserContextInterface::USER_TYPE_GUEST ) {
78
- throw new GraphQlAuthorizationException (
79
- __ (
80
- 'Current customer does not have access to the resource "%1" ' ,
81
- [AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS ]
82
- )
83
- );
84
- }
85
- $ customerId = $ context ->getUserId ();
92
+ $ currentUserId = $ context ->getUserId ();
93
+ $ currentUserType = $ context ->getUserType ();
94
+
95
+ $ this ->checkCustomerAccount ->execute ($ currentUserId , $ currentUserType );
96
+
86
97
return $ this ->addressDataProvider ->processCustomerAddress (
87
- $ this ->processCustomerAddressCreate ($ customerId , $ args ['input ' ])
98
+ $ this ->processCustomerAddressCreate ($ currentUserId , $ args ['input ' ])
88
99
);
89
100
}
90
101
@@ -94,9 +105,11 @@ public function resolve(
94
105
* @param array $addressInput
95
106
* @return bool|string
96
107
*/
97
- public function getInputError (array $ addressInput )
108
+ private function getInputError (array $ addressInput )
98
109
{
99
- $ attributes = $ this ->addressConfigProvider ->getAddressAttributes ();
110
+ $ attributes = $ this ->eavConfig ->getEntityAttributes (
111
+ AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS
112
+ );
100
113
foreach ($ attributes as $ attributeName => $ attributeInfo ) {
101
114
if ($ attributeInfo ->getIsRequired ()
102
115
&& (!isset ($ addressInput [$ attributeName ]) || empty ($ addressInput [$ attributeName ]))) {
@@ -123,11 +136,13 @@ private function processCustomerAddressCreate($customerId, array $addressInput)
123
136
);
124
137
}
125
138
/** @var AddressInterface $newAddress */
126
- $ newAddress = $ this ->addressConfigProvider ->fillAddress (
127
- $ this ->addressInterfaceFactory ->create (),
128
- $ addressInput
139
+ $ newAddress = $ this ->addressInterfaceFactory ->create ();
140
+ $ this ->dataObjectHelper ->populateWithArray (
141
+ $ newAddress ,
142
+ $ addressInput ,
143
+ AddressInterface::class
129
144
);
130
145
$ newAddress ->setCustomerId ($ customerId );
131
- return $ this ->addressRepositoryInterface ->save ($ newAddress );
146
+ return $ this ->addressRepository ->save ($ newAddress );
132
147
}
133
148
}
0 commit comments