|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\QuoteGraphQl\Model\Cart; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Framework\DataObject; |
| 12 | +use Magento\Framework\DataObjectFactory; |
| 13 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 14 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 15 | +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; |
| 16 | +use Magento\Framework\Stdlib\ArrayManager; |
| 17 | +use Magento\Quote\Model\Quote; |
| 18 | + |
| 19 | +/** |
| 20 | + * Add simple product to cart |
| 21 | + * |
| 22 | + * TODO: should be replaced for different types resolver |
| 23 | + */ |
| 24 | +class AddSimpleProductToCart |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var ArrayManager |
| 28 | + */ |
| 29 | + private $arrayManager; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var DataObjectFactory |
| 33 | + */ |
| 34 | + private $dataObjectFactory; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var ProductRepositoryInterface |
| 38 | + */ |
| 39 | + private $productRepository; |
| 40 | + |
| 41 | + /** |
| 42 | + * @param ArrayManager $arrayManager |
| 43 | + * @param DataObjectFactory $dataObjectFactory |
| 44 | + * @param ProductRepositoryInterface $productRepository |
| 45 | + */ |
| 46 | + public function __construct( |
| 47 | + ArrayManager $arrayManager, |
| 48 | + DataObjectFactory $dataObjectFactory, |
| 49 | + ProductRepositoryInterface $productRepository |
| 50 | + ) { |
| 51 | + $this->arrayManager = $arrayManager; |
| 52 | + $this->dataObjectFactory = $dataObjectFactory; |
| 53 | + $this->productRepository = $productRepository; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Add simple product to cart |
| 58 | + * |
| 59 | + * @param Quote $cart |
| 60 | + * @param array $cartItemData |
| 61 | + * @return void |
| 62 | + * @throws GraphQlNoSuchEntityException |
| 63 | + * @throws GraphQlInputException |
| 64 | + */ |
| 65 | + public function execute(Quote $cart, array $cartItemData): void |
| 66 | + { |
| 67 | + $sku = $this->extractSku($cartItemData); |
| 68 | + $qty = $this->extractQty($cartItemData); |
| 69 | + $customizableOptions = $this->extractCustomizableOptions($cartItemData); |
| 70 | + |
| 71 | + try { |
| 72 | + $product = $this->productRepository->get($sku); |
| 73 | + } catch (NoSuchEntityException $e) { |
| 74 | + throw new GraphQlNoSuchEntityException(__('Could not find a product with SKU "%sku"', ['sku' => $sku])); |
| 75 | + } |
| 76 | + |
| 77 | + $result = $cart->addProduct($product, $this->createBuyRequest($qty, $customizableOptions)); |
| 78 | + |
| 79 | + if (is_string($result)) { |
| 80 | + throw new GraphQlInputException(__($result)); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Extract SKU from cart item data |
| 86 | + * |
| 87 | + * @param array $cartItemData |
| 88 | + * @return string |
| 89 | + * @throws GraphQlInputException |
| 90 | + */ |
| 91 | + private function extractSku(array $cartItemData): string |
| 92 | + { |
| 93 | + $sku = $this->arrayManager->get('data/sku', $cartItemData); |
| 94 | + if (!isset($sku)) { |
| 95 | + throw new GraphQlInputException(__('Missing key "sku" in cart item data')); |
| 96 | + } |
| 97 | + return (string)$sku; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Extract Qty from cart item data |
| 102 | + * |
| 103 | + * @param array $cartItemData |
| 104 | + * @return float |
| 105 | + * @throws GraphQlInputException |
| 106 | + */ |
| 107 | + private function extractQty(array $cartItemData): float |
| 108 | + { |
| 109 | + $qty = $this->arrayManager->get('data/qty', $cartItemData); |
| 110 | + if (!isset($qty)) { |
| 111 | + throw new GraphQlInputException(__('Missing key "qty" in cart item data')); |
| 112 | + } |
| 113 | + return (float)$qty; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Extract Customizable Options from cart item data |
| 118 | + * |
| 119 | + * @param array $cartItemData |
| 120 | + * @return array |
| 121 | + */ |
| 122 | + private function extractCustomizableOptions(array $cartItemData): array |
| 123 | + { |
| 124 | + $customizableOptions = $this->arrayManager->get('customizable_options', $cartItemData, []); |
| 125 | + |
| 126 | + $customizableOptionsData = []; |
| 127 | + foreach ($customizableOptions as $customizableOption) { |
| 128 | + $customizableOptionsData[$customizableOption['id']] = $customizableOption['value']; |
| 129 | + } |
| 130 | + return $customizableOptionsData; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Format GraphQl input data to a shape that buy request has |
| 135 | + * |
| 136 | + * @param float $qty |
| 137 | + * @param array $customOptions |
| 138 | + * @return DataObject |
| 139 | + */ |
| 140 | + private function createBuyRequest(float $qty, array $customOptions): DataObject |
| 141 | + { |
| 142 | + return $this->dataObjectFactory->create([ |
| 143 | + 'data' => [ |
| 144 | + 'qty' => $qty, |
| 145 | + 'options' => $customOptions, |
| 146 | + ], |
| 147 | + ]); |
| 148 | + } |
| 149 | +} |
0 commit comments