|
| 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\Catalog\Model\Product; |
| 20 | +use Magento\Framework\Exception\LocalizedException; |
| 21 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 22 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 23 | +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; |
| 24 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 25 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 26 | +use Magento\Quote\Model\Quote; |
| 27 | +use Magento\QuoteGraphQl\Model\CartItem\GetPaginatedCartItems; |
| 28 | +use Magento\Framework\GraphQl\Query\Uid; |
| 29 | + |
| 30 | +/** |
| 31 | + * @inheritdoc |
| 32 | + */ |
| 33 | +class CartItemsPaginated implements ResolverInterface |
| 34 | +{ |
| 35 | + /** |
| 36 | + * @param GetPaginatedCartItems $pagination |
| 37 | + * @param Uid $uidEncoder |
| 38 | + */ |
| 39 | + public function __construct( |
| 40 | + private readonly GetPaginatedCartItems $pagination, |
| 41 | + private readonly Uid $uidEncoder |
| 42 | + ) { |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritdoc |
| 47 | + */ |
| 48 | + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) |
| 49 | + { |
| 50 | + if (!isset($value['model'])) { |
| 51 | + throw new LocalizedException(__('"model" value should be specified')); |
| 52 | + } |
| 53 | + /** @var Quote $cart */ |
| 54 | + $cart = $value['model']; |
| 55 | + |
| 56 | + if (isset($args['currentPage']) && $args['currentPage'] < 1) { |
| 57 | + throw new GraphQlInputException(__('currentPage value must be greater than 0.')); |
| 58 | + } |
| 59 | + if (isset($args['pageSize']) && $args['pageSize'] < 1) { |
| 60 | + throw new GraphQlInputException(__('pageSize value must be greater than 0.')); |
| 61 | + } |
| 62 | + |
| 63 | + $pageSize = $args['pageSize']; |
| 64 | + $currentPage = $args['currentPage']; |
| 65 | + $offset = ($currentPage - 1) * $pageSize; |
| 66 | + $itemsData = []; |
| 67 | + |
| 68 | + try { |
| 69 | + $paginatedCartItems = $this->pagination->execute($cart, $pageSize, $offset); |
| 70 | + $cartProductsData = $this->getCartProductsData($paginatedCartItems['products']); |
| 71 | + |
| 72 | + foreach ($paginatedCartItems['items'] as $cartItem) { |
| 73 | + $productId = $cartItem->getProduct()->getId(); |
| 74 | + if (!isset($cartProductsData[$productId])) { |
| 75 | + $itemsData[] = new GraphQlNoSuchEntityException( |
| 76 | + __("The product that was requested doesn't exist. Verify the product and try again.") |
| 77 | + ); |
| 78 | + continue; |
| 79 | + } |
| 80 | + $cartItem->setQuote($cart); |
| 81 | + $itemsData[] = [ |
| 82 | + 'id' => $cartItem->getItemId(), |
| 83 | + 'uid' => $this->uidEncoder->encode((string) $cartItem->getItemId()), |
| 84 | + 'quantity' => $cartItem->getQty(), |
| 85 | + 'product' => $cartProductsData[$productId], |
| 86 | + 'model' => $cartItem, |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + return [ |
| 91 | + 'items' => $itemsData, |
| 92 | + 'total_count' => $paginatedCartItems['total'], |
| 93 | + 'page_info' => [ |
| 94 | + 'page_size' => $pageSize, |
| 95 | + 'current_page' => $currentPage, |
| 96 | + 'total_pages' => (int)ceil($paginatedCartItems['total'] / $pageSize) |
| 97 | + ], |
| 98 | + ]; |
| 99 | + } catch (\Exception $e) { |
| 100 | + throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Get product data for cart items |
| 106 | + * |
| 107 | + * @param Product[] $products |
| 108 | + * @return array |
| 109 | + */ |
| 110 | + private function getCartProductsData(array $products): array |
| 111 | + { |
| 112 | + $productsData = []; |
| 113 | + foreach ($products as $product) { |
| 114 | + $productsData[$product->getId()] = $product->getData(); |
| 115 | + $productsData[$product->getId()]['model'] = $product; |
| 116 | + $productsData[$product->getId()]['uid'] = $this->uidEncoder->encode((string) $product->getId()); |
| 117 | + } |
| 118 | + return $productsData; |
| 119 | + } |
| 120 | +} |
0 commit comments