|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Landofcoder |
| 4 | + * |
| 5 | + * NOTICE OF LICENSE |
| 6 | + * |
| 7 | + * This source file is subject to the Landofcoder.com license that is |
| 8 | + * available through the world-wide-web at this URL: |
| 9 | + * https://landofcoder.com/terms |
| 10 | + * |
| 11 | + * DISCLAIMER |
| 12 | + * |
| 13 | + * Do not edit or add to this file if you wish to upgrade this extension to newer |
| 14 | + * version in the future. |
| 15 | + * |
| 16 | + * @category Landofcoder |
| 17 | + * @package Lof_MarketplaceGraphQl |
| 18 | + * @copyright Copyright (c) 2021 Landofcoder (https://www.landofcoder.com/) |
| 19 | + * @license https://landofcoder.com/terms |
| 20 | + */ |
| 21 | + |
| 22 | +namespace Lof\MarketplaceGraphQl\Model\Resolver; |
| 23 | + |
| 24 | +use Lof\MarketPlace\Api\SellerProductsRepositoryInterface; |
| 25 | +use Lof\MarketPlace\Api\SellersFrontendRepositoryInterface; |
| 26 | +use Lof\MarketPlace\Api\SellerGroupRepositoryInterface; |
| 27 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 28 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 29 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 30 | +use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\Builder as SearchCriteriaBuilder; |
| 31 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 32 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 33 | +use Magento\Framework\Exception\InputException; |
| 34 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 35 | +use Magento\Search\Model\Query; |
| 36 | +use Magento\Store\Api\Data\StoreInterface; |
| 37 | +use Magento\Store\Model\ScopeInterface; |
| 38 | +use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\ArgumentApplier\Filter; |
| 39 | + |
| 40 | +/** |
| 41 | + * Class SellerGroups |
| 42 | + * |
| 43 | + * @package Lof\MarketplaceGraphQl\Model\Resolver |
| 44 | + */ |
| 45 | +class SellerGroups extends AbstractSellerQuery implements ResolverInterface |
| 46 | +{ |
| 47 | + |
| 48 | + /** |
| 49 | + * @var SellerGroupRepositoryInterface |
| 50 | + */ |
| 51 | + private $sellerGroup; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var string |
| 55 | + */ |
| 56 | + private const SPECIAL_CHARACTERS = '-+~/\\<>\'":*$#@()!,.?`=%&^'; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var ScopeConfigInterface |
| 60 | + */ |
| 61 | + private $scopeConfig; |
| 62 | + |
| 63 | + /** |
| 64 | + * Sellers constructor. |
| 65 | + * @param SearchCriteriaBuilder $searchCriteriaBuilder |
| 66 | + * @param SellersFrontendRepositoryInterface $seller |
| 67 | + * @param SellerProductsRepositoryInterface $productSeller |
| 68 | + * @param ProductRepositoryInterface $productRepository |
| 69 | + * @param SellerGroupRepositoryInterface $sellerGroup |
| 70 | + * @param ScopeConfigInterface $scopeConfig |
| 71 | + */ |
| 72 | + public function __construct( |
| 73 | + SearchCriteriaBuilder $searchCriteriaBuilder, |
| 74 | + SellersFrontendRepositoryInterface $seller, |
| 75 | + SellerProductsRepositoryInterface $productSeller, |
| 76 | + ProductRepositoryInterface $productRepository, |
| 77 | + SellerGroupRepositoryInterface $sellerGroup, |
| 78 | + ScopeConfigInterface $scopeConfig |
| 79 | + ) { |
| 80 | + $this->sellerGroup = $sellerGroup; |
| 81 | + $this->scopeConfig = $scopeConfig; |
| 82 | + parent::__construct($searchCriteriaBuilder, $seller, $productSeller, $productRepository); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @inheritdoc |
| 87 | + */ |
| 88 | + public function resolve( |
| 89 | + Field $field, |
| 90 | + $context, |
| 91 | + ResolveInfo $info, |
| 92 | + array $value = null, |
| 93 | + array $args = null |
| 94 | + ) { |
| 95 | + if ($args['currentPage'] < 1) { |
| 96 | + throw new GraphQlInputException(__('currentPage value must be greater than 0.')); |
| 97 | + } |
| 98 | + if ($args['pageSize'] < 1) { |
| 99 | + throw new GraphQlInputException(__('pageSize value must be greater than 0.')); |
| 100 | + } |
| 101 | + $store = $context->getExtensionAttributes()->getStore(); |
| 102 | + $args[Filter::ARGUMENT_NAME] = $this->formatMatchFilters($args['filter'], $store); |
| 103 | + $searchCriteria = $this->searchCriteriaBuilder->build('lof_marketplace_seller_group', $args); |
| 104 | + $searchCriteria->setCurrentPage($args['currentPage']); |
| 105 | + $searchCriteria->setPageSize($args['pageSize']); |
| 106 | + |
| 107 | + $searchResult = $this->sellerGroup->getList($searchCriteria); |
| 108 | + $totalPages = $args['pageSize'] ? ((int)ceil($searchResult->getTotalCount() / $args['pageSize'])) : 0; |
| 109 | + $resultItems = $searchResult->getItems(); |
| 110 | + $items = []; |
| 111 | + if($resultItems){ |
| 112 | + foreach($resultItems as $_item){ |
| 113 | + $items[] = $_item->__toArray(); |
| 114 | + } |
| 115 | + } |
| 116 | + return [ |
| 117 | + 'total_count' => $searchResult->getTotalCount(), |
| 118 | + 'items' => $items, |
| 119 | + 'page_info' => [ |
| 120 | + 'page_size' => $args['pageSize'], |
| 121 | + 'current_page' => $args['currentPage'], |
| 122 | + 'total_pages' => $totalPages |
| 123 | + ] |
| 124 | + ]; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Format match filter to behave like fuzzy match |
| 129 | + * |
| 130 | + * @param array $filter |
| 131 | + * @param StoreInterface $store |
| 132 | + * @return array |
| 133 | + * @throws InputException |
| 134 | + */ |
| 135 | + private function formatMatchFilters(array $filters, StoreInterface $store): array |
| 136 | + { |
| 137 | + $minQueryLength = $this->scopeConfig->getValue( |
| 138 | + Query::XML_PATH_MIN_QUERY_LENGTH, |
| 139 | + ScopeInterface::SCOPE_STORE, |
| 140 | + $store |
| 141 | + ); |
| 142 | + foreach ($filters as $filter => $condition) { |
| 143 | + $conditionType = current(array_keys($condition)); |
| 144 | + $tmpminQueryLength = $minQueryLength; |
| 145 | + if ($conditionType === 'match') { |
| 146 | + $searchValue = trim(str_replace(self::SPECIAL_CHARACTERS, '', $condition[$conditionType])); |
| 147 | + $matchLength = strlen($searchValue); |
| 148 | + if ($matchLength < $tmpminQueryLength) { |
| 149 | + throw new InputException(__('Invalid match filter. Minimum length is %1.', $tmpminQueryLength)); |
| 150 | + } |
| 151 | + unset($filters[$filter]['match']); |
| 152 | + $filters[$filter]['like'] = '%' . $searchValue . '%'; |
| 153 | + } |
| 154 | + } |
| 155 | + return $filters; |
| 156 | + } |
| 157 | +} |
0 commit comments