7
7
8
8
namespace Magento \QuoteGraphQl \Model \Cart ;
9
9
10
- use Magento \Authorization \Model \UserContextInterface ;
11
10
use Magento \Customer \Api \Data \AddressInterface ;
12
- use Magento \Framework \ GraphQl \ Exception \ GraphQlAuthorizationException ;
11
+ use Magento \CustomerGraphQl \ Model \ Customer \ CheckCustomerAccount ;
13
12
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
14
13
use Magento \Framework \GraphQl \Query \Resolver \ContextInterface ;
15
14
use Magento \Quote \Api \Data \CartInterface ;
@@ -37,19 +36,27 @@ class SetShippingAddressOnCart implements SetShippingAddressesOnCartInterface
37
36
*/
38
37
private $ addressModel ;
39
38
39
+ /**
40
+ * @var CheckCustomerAccount
41
+ */
42
+ private $ checkCustomerAccount ;
43
+
40
44
/**
41
45
* @param ShippingAddressManagementInterface $shippingAddressManagement
42
46
* @param AddressRepositoryInterface $addressRepository
43
47
* @param Address $addressModel
48
+ * @param CheckCustomerAccount $checkCustomerAccount
44
49
*/
45
50
public function __construct (
46
51
ShippingAddressManagementInterface $ shippingAddressManagement ,
47
52
AddressRepositoryInterface $ addressRepository ,
48
- Address $ addressModel
53
+ Address $ addressModel ,
54
+ CheckCustomerAccount $ checkCustomerAccount
49
55
) {
50
56
$ this ->shippingAddressManagement = $ shippingAddressManagement ;
51
57
$ this ->addressRepository = $ addressRepository ;
52
58
$ this ->addressModel = $ addressModel ;
59
+ $ this ->checkCustomerAccount = $ checkCustomerAccount ;
53
60
}
54
61
55
62
/**
@@ -66,7 +73,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
66
73
$ customerAddressId = $ shippingAddress ['customer_address_id ' ] ?? null ;
67
74
$ addressInput = $ shippingAddress ['address ' ] ?? null ;
68
75
69
- if (! $ customerAddressId && ! $ addressInput ) {
76
+ if (null === $ customerAddressId && null === $ addressInput ) {
70
77
throw new GraphQlInputException (
71
78
__ ('The shipping address must contain either "customer_address_id" or "address". ' )
72
79
);
@@ -76,19 +83,14 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
76
83
__ ('The shipping address cannot contain "customer_address_id" and "address" at the same time. ' )
77
84
);
78
85
}
79
- if ($ customerAddressId ) {
80
- if ((!$ context ->getUserId ()) || $ context ->getUserType () == UserContextInterface::USER_TYPE_GUEST ) {
81
- throw new GraphQlAuthorizationException (
82
- __ (
83
- 'Guest users cannot manage addresses. '
84
- )
85
- );
86
- }
86
+ if (null === $ customerAddressId ) {
87
+ $ shippingAddress = $ this ->addressModel ->addData ($ addressInput );
88
+ } else {
89
+ $ this ->checkCustomerAccount ->execute ($ context ->getUserId (), $ context ->getUserType ());
90
+
87
91
/** @var AddressInterface $customerAddress */
88
92
$ customerAddress = $ this ->addressRepository ->getById ($ customerAddressId );
89
93
$ shippingAddress = $ this ->addressModel ->importCustomerAddressData ($ customerAddress );
90
- } else {
91
- $ shippingAddress = $ this ->addressModel ->addData ($ addressInput );
92
94
}
93
95
94
96
$ this ->shippingAddressManagement ->assign ($ cart ->getId (), $ shippingAddress );
0 commit comments