|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2023 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * NOTICE: All information contained herein is, and remains |
| 7 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 8 | + * and technical concepts contained herein are proprietary to Adobe |
| 9 | + * and its suppliers and are protected by all applicable intellectual |
| 10 | + * property laws, including trade secret and copyright laws. |
| 11 | + * Dissemination of this information or reproduction of this material |
| 12 | + * is strictly forbidden unless prior written permission is obtained from |
| 13 | + * Adobe. |
| 14 | + */ |
| 15 | +declare(strict_types=1); |
| 16 | + |
| 17 | +namespace Magento\QuoteGraphQl\Model\Resolver; |
| 18 | + |
| 19 | +use Magento\Checkout\Api\Data\TotalsInformationInterface; |
| 20 | +use Magento\Checkout\Api\Data\TotalsInformationInterfaceFactory; |
| 21 | +use Magento\Checkout\Api\TotalsInformationManagementInterface; |
| 22 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 23 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 24 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 25 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 26 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 27 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 28 | +use Magento\Quote\Api\Data\AddressInterface; |
| 29 | +use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface; |
| 30 | +use Magento\Quote\Model\Quote\AddressFactory; |
| 31 | + |
| 32 | +/** |
| 33 | + * Apply address and shipping method to totals estimate and return the quote |
| 34 | + */ |
| 35 | +class EstimateTotals implements ResolverInterface |
| 36 | +{ |
| 37 | + /** |
| 38 | + * @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId |
| 39 | + * @param CartRepositoryInterface $cartRepository |
| 40 | + * @param AddressFactory $addressFactory |
| 41 | + * @param TotalsInformationManagementInterface $totalsInformationManagement |
| 42 | + * @param TotalsInformationInterfaceFactory $totalsInformationFactory |
| 43 | + */ |
| 44 | + public function __construct( |
| 45 | + private readonly MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId, |
| 46 | + private readonly CartRepositoryInterface $cartRepository, |
| 47 | + private readonly AddressFactory $addressFactory, |
| 48 | + private readonly TotalsInformationManagementInterface $totalsInformationManagement, |
| 49 | + private readonly TotalsInformationInterfaceFactory $totalsInformationFactory |
| 50 | + ) { |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @inheritdoc |
| 55 | + * |
| 56 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 57 | + */ |
| 58 | + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) |
| 59 | + { |
| 60 | + if (empty($args['input']['cart_id'])) { |
| 61 | + throw new GraphQlInputException(__('Required parameter "cart_id" is missing')); |
| 62 | + } |
| 63 | + |
| 64 | + try { |
| 65 | + $cartId = $this->maskedQuoteIdToQuoteId->execute($args['input']['cart_id']); |
| 66 | + } catch (NoSuchEntityException $exception) { |
| 67 | + throw new GraphQlInputException( |
| 68 | + __( |
| 69 | + 'Could not find a cart with ID "%masked_id"', |
| 70 | + [ |
| 71 | + 'masked_id' => $args['input']['cart_id'] |
| 72 | + ] |
| 73 | + ) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + if (empty($args['input']['address']['country_code'])) { |
| 78 | + throw new GraphQlInputException(__('Required parameter "country_code" is missing')); |
| 79 | + } |
| 80 | + |
| 81 | + $this->totalsInformationManagement->calculate($cartId, $this->getTotalsInformation($args['input'])); |
| 82 | + |
| 83 | + return [ |
| 84 | + 'cart' => [ |
| 85 | + 'model' => $this->cartRepository->get($cartId) |
| 86 | + ] |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Retrieve an instance of totals information based on input data |
| 92 | + * |
| 93 | + * @param array $input |
| 94 | + * @return TotalsInformationInterface |
| 95 | + */ |
| 96 | + private function getTotalsInformation(array $input): TotalsInformationInterface |
| 97 | + { |
| 98 | + $data = [TotalsInformationInterface::ADDRESS => $this->getAddress($input['address'])]; |
| 99 | + |
| 100 | + $shippingMethod = $input['shipping_method'] ?? []; |
| 101 | + |
| 102 | + if (isset($shippingMethod['carrier_code']) && isset($shippingMethod['method_code'])) { |
| 103 | + $data[TotalsInformationInterface::SHIPPING_CARRIER_CODE] = $shippingMethod['carrier_code']; |
| 104 | + $data[TotalsInformationInterface::SHIPPING_METHOD_CODE] = $shippingMethod['method_code']; |
| 105 | + } |
| 106 | + |
| 107 | + return $this->totalsInformationFactory->create(['data' => $data]); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Retrieve an instance of address based on address data |
| 112 | + * |
| 113 | + * @param array $data |
| 114 | + * @return AddressInterface |
| 115 | + */ |
| 116 | + private function getAddress(array $data): AddressInterface |
| 117 | + { |
| 118 | + $data = [ |
| 119 | + AddressInterface::KEY_COUNTRY_ID => $data['country_code'], |
| 120 | + AddressInterface::KEY_REGION => $data['region'][AddressInterface::KEY_REGION] ?? null, |
| 121 | + AddressInterface::KEY_REGION_ID => $data['region'][AddressInterface::KEY_REGION_ID] ?? null, |
| 122 | + AddressInterface::KEY_REGION_CODE => $data['region'][AddressInterface::KEY_REGION_CODE] ?? null, |
| 123 | + AddressInterface::KEY_POSTCODE => $data[AddressInterface::KEY_POSTCODE] ?? null, |
| 124 | + ]; |
| 125 | + |
| 126 | + return $this->addressFactory->create(['data' => array_filter($data)]); |
| 127 | + } |
| 128 | +} |
0 commit comments