|
| 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\CatalogGraphQl\Model\Resolver\Products\Query; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\ResourceModel\Category\Collection; |
| 11 | +use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; |
| 12 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 13 | +use Magento\Framework\GraphQl\Query\Resolver\ArgumentsProcessorInterface; |
| 14 | + |
| 15 | +/** |
| 16 | + * Category Path processor class for category uid and category id arguments |
| 17 | + */ |
| 18 | +class CategoryUrlPathArgsProcessor implements ArgumentsProcessorInterface |
| 19 | +{ |
| 20 | + private const ID = 'category_id'; |
| 21 | + |
| 22 | + private const UID = 'category_uid'; |
| 23 | + |
| 24 | + private const PATH = 'category_url_path'; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var CollectionFactory |
| 28 | + */ |
| 29 | + private $collectionFactory; |
| 30 | + |
| 31 | + /** |
| 32 | + * @param CollectionFactory $collectionFactory |
| 33 | + */ |
| 34 | + public function __construct(CollectionFactory $collectionFactory) |
| 35 | + { |
| 36 | + $this->collectionFactory = $collectionFactory; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Composite processor that loops through available processors for arguments that come from graphql input |
| 41 | + * |
| 42 | + * @param string $fieldName, |
| 43 | + * @param array $args |
| 44 | + * @return array |
| 45 | + * @throws GraphQlInputException |
| 46 | + */ |
| 47 | + public function process( |
| 48 | + string $fieldName, |
| 49 | + array $args |
| 50 | + ): array { |
| 51 | + $idFilter = $args['filter'][self::ID] ?? []; |
| 52 | + $uidFilter = $args['filter'][self::UID] ?? []; |
| 53 | + $pathFilter = $args['filter'][self::PATH] ?? []; |
| 54 | + |
| 55 | + if (!empty($pathFilter) && $fieldName === 'products') { |
| 56 | + if (!empty($idFilter)) { |
| 57 | + throw new GraphQlInputException( |
| 58 | + __('`%1` and `%2` can\'t be used at the same time.', [self::ID, self::PATH]) |
| 59 | + ); |
| 60 | + } elseif (!empty($uidFilter)) { |
| 61 | + throw new GraphQlInputException( |
| 62 | + __('`%1` and `%2` can\'t be used at the same time.', [self::UID, self::PATH]) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** @var Collection $collection */ |
| 67 | + $collection = $this->collectionFactory->create(); |
| 68 | + $collection->addAttributeToSelect('entity_id'); |
| 69 | + $collection->addAttributeToFilter('url_path', $pathFilter['eq']); |
| 70 | + |
| 71 | + if ($collection->count() === 0) { |
| 72 | + throw new GraphQlInputException( |
| 73 | + __('No category with the provided %1 was found', ['category_url_path']) |
| 74 | + ); |
| 75 | + } |
| 76 | + $category = $collection->getFirstItem(); |
| 77 | + $args['filter'][self::ID]['eq'] = $category->getId(); |
| 78 | + |
| 79 | + unset($args['filter'][self::PATH]); |
| 80 | + } |
| 81 | + return $args; |
| 82 | + } |
| 83 | +} |
0 commit comments