|
| 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\WishlistGraphQl\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Framework\Exception\LocalizedException; |
| 11 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 12 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 13 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 14 | +use Magento\Store\Api\Data\StoreInterface; |
| 15 | +use Magento\Store\Model\StoreManagerInterface; |
| 16 | +use Magento\Wishlist\Model\ResourceModel\Item\Collection as WishlistItemCollection; |
| 17 | +use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistItemCollectionFactory; |
| 18 | +use Magento\Wishlist\Model\Item; |
| 19 | +use Magento\Wishlist\Model\Wishlist; |
| 20 | + |
| 21 | +/** |
| 22 | + * Fetches the Wishlist Items data according to the GraphQL schema |
| 23 | + */ |
| 24 | +class WishlistItemsResolver implements ResolverInterface |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var WishlistItemCollectionFactory |
| 28 | + */ |
| 29 | + private $wishlistItemCollectionFactory; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var StoreManagerInterface |
| 33 | + */ |
| 34 | + private $storeManager; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param WishlistItemCollectionFactory $wishlistItemCollectionFactory |
| 38 | + * @param StoreManagerInterface $storeManager |
| 39 | + */ |
| 40 | + public function __construct( |
| 41 | + WishlistItemCollectionFactory $wishlistItemCollectionFactory, |
| 42 | + StoreManagerInterface $storeManager |
| 43 | + ) { |
| 44 | + $this->wishlistItemCollectionFactory = $wishlistItemCollectionFactory; |
| 45 | + $this->storeManager = $storeManager; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @inheritdoc |
| 50 | + */ |
| 51 | + public function resolve( |
| 52 | + Field $field, |
| 53 | + $context, |
| 54 | + ResolveInfo $info, |
| 55 | + array $value = null, |
| 56 | + array $args = null |
| 57 | + ) { |
| 58 | + if (!isset($value['model'])) { |
| 59 | + throw new LocalizedException(__('Missing key "model" in Wishlist value data')); |
| 60 | + } |
| 61 | + /** @var Wishlist $wishlist */ |
| 62 | + $wishlist = $value['model']; |
| 63 | + |
| 64 | + $wishlistItems = $this->getWishListItems($wishlist); |
| 65 | + |
| 66 | + $data = []; |
| 67 | + foreach ($wishlistItems as $wishlistItem) { |
| 68 | + $data[] = [ |
| 69 | + 'id' => $wishlistItem->getId(), |
| 70 | + 'qty' => $wishlistItem->getData('qty'), |
| 71 | + 'description' => $wishlistItem->getDescription(), |
| 72 | + 'added_at' => $wishlistItem->getAddedAt(), |
| 73 | + 'model' => $wishlistItem, |
| 74 | + ]; |
| 75 | + } |
| 76 | + return $data; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Get wishlist items |
| 81 | + * |
| 82 | + * @param Wishlist $wishlist |
| 83 | + * @return Item[] |
| 84 | + */ |
| 85 | + private function getWishListItems(Wishlist $wishlist): array |
| 86 | + { |
| 87 | + /** @var WishlistItemCollection $wishlistItemCollection */ |
| 88 | + $wishlistItemCollection = $this->wishlistItemCollectionFactory->create(); |
| 89 | + $wishlistItemCollection |
| 90 | + ->addWishlistFilter($wishlist) |
| 91 | + ->addStoreFilter(array_map(function (StoreInterface $store) { |
| 92 | + return $store->getId(); |
| 93 | + }, $this->storeManager->getStores())) |
| 94 | + ->setVisibilityFilter(); |
| 95 | + return $wishlistItemCollection->getItems(); |
| 96 | + } |
| 97 | +} |
0 commit comments