8
8
namespace Magento \QuoteGraphQl \Model \Resolver ;
9
9
10
10
use Magento \Checkout \Api \Data \TotalsInformationInterface ;
11
- use Magento \Checkout \Api \Data \TotalsInformationInterfaceFactory ;
12
11
use Magento \Checkout \Api \TotalsInformationManagementInterface ;
13
12
use Magento \Framework \Exception \NoSuchEntityException ;
14
13
use Magento \Framework \GraphQl \Config \Element \Field ;
15
14
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
16
15
use Magento \Framework \GraphQl \Query \ResolverInterface ;
17
16
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
18
17
use Magento \Quote \Api \CartRepositoryInterface ;
19
- use Magento \Quote \Api \Data \AddressInterface ;
20
18
use Magento \Quote \Model \MaskedQuoteIdToQuoteIdInterface ;
21
- use Magento \Quote \Model \Quote \Address ;
22
- use Magento \Quote \Model \Quote \AddressFactory ;
19
+ use Magento \QuoteGraphQl \Model \Cart \AssignShippingMethodToCart ;
23
20
use Magento \QuoteGraphQl \Model \ErrorMapper ;
21
+ use Magento \QuoteGraphQl \Model \TotalsBuilder ;
22
+ use Psr \Log \LoggerInterface ;
24
23
25
24
/**
26
25
* Apply address and shipping method to totals estimate and return the quote
27
26
*/
28
27
class EstimateTotals implements ResolverInterface
29
28
{
30
29
/**
30
+ * EstimateTotals Constructor
31
+ *
31
32
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
32
33
* @param CartRepositoryInterface $cartRepository
33
- * @param AddressFactory $addressFactory
34
34
* @param TotalsInformationManagementInterface $totalsInformationManagement
35
- * @param TotalsInformationInterfaceFactory $totalsInformationFactory
36
35
* @param ErrorMapper $errorMapper
36
+ * @param AssignShippingMethodToCart $assignShippingMethodToCart
37
+ * @param LoggerInterface $logger
38
+ * @param TotalsBuilder $totalsBuilder
37
39
*/
38
40
public function __construct (
39
41
private readonly MaskedQuoteIdToQuoteIdInterface $ maskedQuoteIdToQuoteId ,
40
42
private readonly CartRepositoryInterface $ cartRepository ,
41
- private readonly AddressFactory $ addressFactory ,
42
43
private readonly TotalsInformationManagementInterface $ totalsInformationManagement ,
43
- private readonly TotalsInformationInterfaceFactory $ totalsInformationFactory ,
44
- private readonly ErrorMapper $ errorMapper
44
+ private readonly ErrorMapper $ errorMapper ,
45
+ private readonly AssignShippingMethodToCart $ assignShippingMethodToCart ,
46
+ private readonly LoggerInterface $ logger ,
47
+ private readonly TotalsBuilder $ totalsBuilder
45
48
) {
46
49
}
47
50
48
51
/**
49
52
* @inheritdoc
50
- *
51
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
52
53
*/
53
54
public function resolve (Field $ field , $ context , ResolveInfo $ info , ?array $ value = null , ?array $ args = null )
54
55
{
55
- if (empty ($ args ['input ' ]['cart_id ' ])) {
56
+ $ input = $ args ['input ' ] ?? [];
57
+
58
+ if (empty ($ input ['cart_id ' ])) {
56
59
throw new GraphQlInputException (__ ('Required parameter "cart_id" is missing ' ));
57
60
}
58
61
59
62
try {
60
- $ cartId = $ this ->maskedQuoteIdToQuoteId ->execute ($ args [ ' input ' ] ['cart_id ' ]);
63
+ $ cartId = $ this ->maskedQuoteIdToQuoteId ->execute ($ input ['cart_id ' ]);
61
64
} catch (NoSuchEntityException $ exception ) {
62
65
throw new GraphQlInputException (
63
66
__ (
64
67
'Could not find a cart with ID "%masked_id" ' ,
65
68
[
66
- 'masked_id ' => $ args [ ' input ' ] ['cart_id ' ]
69
+ 'masked_id ' => $ input ['cart_id ' ]
67
70
]
68
71
),
69
72
$ exception ,
70
73
$ this ->errorMapper ->getErrorMessageId ('Could not find a cart with ID ' )
71
74
);
72
75
}
73
76
74
- if (empty ($ args ['input ' ]['address ' ]['country_code ' ])) {
77
+ $ addressData = $ input ['address ' ] ?? [];
78
+ if (empty ($ addressData ['country_code ' ])) {
75
79
throw new GraphQlInputException (__ ('Required parameter "country_code" is missing ' ));
76
80
}
77
81
78
- $ data = $ this ->getTotalsInformation ($ args ['input ' ]);
79
- $ this ->totalsInformationManagement ->calculate ($ cartId , $ data );
82
+ $ totalsInfo = $ this ->totalsBuilder ->execute ($ addressData , $ input ['shipping_method ' ] ?? []);
83
+ $ this ->totalsInformationManagement ->calculate ($ cartId , $ totalsInfo );
84
+ $ this ->updateShippingMethod ($ totalsInfo , $ cartId );
80
85
81
86
return [
82
87
'cart ' => [
@@ -86,41 +91,26 @@ public function resolve(Field $field, $context, ResolveInfo $info, ?array $value
86
91
}
87
92
88
93
/**
89
- * Retrieve an instance of totals information based on input data
94
+ * Update shipping method if provided
90
95
*
91
- * @param array $input
92
- * @return TotalsInformationInterface
96
+ * @param TotalsInformationInterface $totalsInfo
97
+ * @param int $cartId
98
+ * @return void
99
+ * @throws GraphQlInputException
93
100
*/
94
- private function getTotalsInformation ( array $ input ): TotalsInformationInterface
101
+ private function updateShippingMethod ( TotalsInformationInterface $ totalsInfo , int $ cartId ): void
95
102
{
96
- $ data = [TotalsInformationInterface::ADDRESS => $ this ->getAddress ($ input ['address ' ])];
97
-
98
- $ shippingMethod = $ input ['shipping_method ' ] ?? [];
99
-
100
- if (isset ($ shippingMethod ['carrier_code ' ]) && isset ($ shippingMethod ['method_code ' ])) {
101
- $ data [TotalsInformationInterface::SHIPPING_CARRIER_CODE ] = $ shippingMethod ['carrier_code ' ];
102
- $ data [TotalsInformationInterface::SHIPPING_METHOD_CODE ] = $ shippingMethod ['method_code ' ];
103
+ try {
104
+ if ($ totalsInfo ->getShippingCarrierCode () && $ totalsInfo ->getShippingMethodCode ()) {
105
+ $ this ->assignShippingMethodToCart ->execute (
106
+ $ this ->cartRepository ->get ($ cartId ),
107
+ $ totalsInfo ->getAddress (),
108
+ $ totalsInfo ->getShippingCarrierCode (),
109
+ $ totalsInfo ->getShippingMethodCode ()
110
+ );
111
+ }
112
+ } catch (NoSuchEntityException $ e ) {
113
+ $ this ->logger ->error ($ e ->getMessage ());
103
114
}
104
-
105
- return $ this ->totalsInformationFactory ->create (['data ' => $ data ]);
106
- }
107
-
108
- /**
109
- * Retrieve an instance of address based on address data
110
- *
111
- * @param array $data
112
- * @return AddressInterface
113
- */
114
- private function getAddress (array $ data ): AddressInterface
115
- {
116
- /** @var Address $address */
117
- $ address = $ this ->addressFactory ->create ();
118
- $ address ->setCountryId ($ data ['country_code ' ]);
119
- $ address ->setRegion ($ data ['region ' ][AddressInterface::KEY_REGION ] ?? null );
120
- $ address ->setRegionId ($ data ['region ' ][AddressInterface::KEY_REGION_ID ] ?? null );
121
- $ address ->setRegionCode ($ data ['region ' ][AddressInterface::KEY_REGION_CODE ] ?? null );
122
- $ address ->setPostcode ($ data [AddressInterface::KEY_POSTCODE ] ?? null );
123
-
124
- return $ address ;
125
115
}
126
116
}
0 commit comments