|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\WishlistGraphQl\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 11 | +use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; |
| 12 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 13 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 14 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 15 | +use Magento\Wishlist\Model\ResourceModel\ClearWishlist as ClearWishlistResource; |
| 16 | +use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResource; |
| 17 | +use Magento\Wishlist\Model\Wishlist; |
| 18 | +use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig; |
| 19 | +use Magento\Wishlist\Model\Wishlist\Data\Error; |
| 20 | +use Magento\Wishlist\Model\WishlistFactory; |
| 21 | +use Magento\WishlistGraphQl\Mapper\WishlistDataMapper; |
| 22 | + |
| 23 | +/** |
| 24 | + * Resolver to clear all the products from the specified wishlist |
| 25 | + */ |
| 26 | +class ClearWishlist implements ResolverInterface |
| 27 | +{ |
| 28 | + /** |
| 29 | + * ClearWishlist Constructor |
| 30 | + * |
| 31 | + * @param WishlistConfig $wishlistConfig |
| 32 | + * @param ClearWishlistResource $clearWishlistResource |
| 33 | + * @param WishlistFactory $wishlistFactory |
| 34 | + * @param WishlistResource $wishlistResource |
| 35 | + * @param WishlistDataMapper $wishlistDataMapper |
| 36 | + */ |
| 37 | + public function __construct( |
| 38 | + private readonly WishlistConfig $wishlistConfig, |
| 39 | + private readonly ClearWishlistResource $clearWishlistResource, |
| 40 | + private readonly WishlistFactory $wishlistFactory, |
| 41 | + private readonly WishlistResource $wishlistResource, |
| 42 | + private readonly WishlistDataMapper $wishlistDataMapper |
| 43 | + ) { |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @inheritdoc |
| 48 | + */ |
| 49 | + public function resolve( |
| 50 | + Field $field, |
| 51 | + $context, |
| 52 | + ResolveInfo $info, |
| 53 | + ?array $value = null, |
| 54 | + ?array $args = null |
| 55 | + ): array { |
| 56 | + if (!$this->wishlistConfig->isEnabled()) { |
| 57 | + throw new GraphQlInputException(__('The wishlist configuration is currently disabled.')); |
| 58 | + } |
| 59 | + |
| 60 | + $customerId = (int)$context->getUserId(); |
| 61 | + if (!$customerId) { |
| 62 | + throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); |
| 63 | + } |
| 64 | + |
| 65 | + $wishlistId = ((int)$args['wishlistId']) ?: null; |
| 66 | + if (!$wishlistId) { |
| 67 | + throw new GraphQlInputException(__('The wishlistId is required.')); |
| 68 | + } |
| 69 | + |
| 70 | + $wishlist = $this->getWishlist($wishlistId); |
| 71 | + if (!$wishlist->getId() || $customerId !== (int)$wishlist->getCustomerId()) { |
| 72 | + throw new GraphQlInputException(__('The wishlist was not found.')); |
| 73 | + } |
| 74 | + |
| 75 | + $wishlistOutput = $this->clearWishlistResource->execute($wishlist); |
| 76 | + |
| 77 | + return [ |
| 78 | + 'user_errors' => \array_map( |
| 79 | + function (Error $error) { |
| 80 | + return [ |
| 81 | + 'code' => $error->getCode(), |
| 82 | + 'message' => $error->getMessage(), |
| 83 | + ]; |
| 84 | + }, |
| 85 | + $wishlistOutput->getErrors() |
| 86 | + ), |
| 87 | + 'wishlist' => $this->wishlistDataMapper->map($wishlistOutput->getWishlist()) |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Get customer wishlist |
| 93 | + * |
| 94 | + * @param int $wishlistId |
| 95 | + * @return Wishlist |
| 96 | + */ |
| 97 | + private function getWishlist(int $wishlistId): Wishlist |
| 98 | + { |
| 99 | + $wishlist = $this->wishlistFactory->create(); |
| 100 | + $this->wishlistResource->load($wishlist, $wishlistId); |
| 101 | + |
| 102 | + return $wishlist; |
| 103 | + } |
| 104 | +} |
0 commit comments