|
| 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\Category; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Category; |
| 11 | +use Magento\Catalog\Model\Product\Visibility; |
| 12 | +use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor\StockProcessor; |
| 13 | +use Magento\Framework\Api\SearchCriteriaInterface; |
| 14 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 15 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 16 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 17 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * Retrieves products count for a category |
| 21 | + */ |
| 22 | +class ProductsCount implements ResolverInterface |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var Visibility |
| 26 | + */ |
| 27 | + private $catalogProductVisibility; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var StockProcessor |
| 31 | + */ |
| 32 | + private $stockProcessor; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var SearchCriteriaInterface |
| 36 | + */ |
| 37 | + private $searchCriteria; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param Visibility $catalogProductVisibility |
| 41 | + * @param SearchCriteriaInterface $searchCriteria |
| 42 | + * @param StockProcessor $stockProcessor |
| 43 | + */ |
| 44 | + public function __construct( |
| 45 | + Visibility $catalogProductVisibility, |
| 46 | + SearchCriteriaInterface $searchCriteria, |
| 47 | + StockProcessor $stockProcessor |
| 48 | + ) { |
| 49 | + $this->catalogProductVisibility = $catalogProductVisibility; |
| 50 | + $this->searchCriteria = $searchCriteria; |
| 51 | + $this->stockProcessor = $stockProcessor; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @inheritdoc |
| 56 | + */ |
| 57 | + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) |
| 58 | + { |
| 59 | + if (!isset($value['model'])) { |
| 60 | + throw new GraphQlInputException(__('"model" value should be specified')); |
| 61 | + } |
| 62 | + /** @var Category $category */ |
| 63 | + $category = $value['model']; |
| 64 | + $productsCollection = $category->getProductCollection(); |
| 65 | + $productsCollection->setVisibility($this->catalogProductVisibility->getVisibleInSiteIds()); |
| 66 | + $productsCollection = $this->stockProcessor->process($productsCollection, $this->searchCriteria, []); |
| 67 | + |
| 68 | + return $productsCollection->getSize(); |
| 69 | + } |
| 70 | +} |
0 commit comments