|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * ScandiPWA - Progressive Web App for Magento |
| 4 | + * |
| 5 | + * Copyright © Scandiweb, Inc. All rights reserved. |
| 6 | + * See LICENSE for license details. |
| 7 | + * |
| 8 | + * @license OSL-3.0 (Open Software License ("OSL") v. 3.0) |
| 9 | + * @package scandipwa/quote-graphql |
| 10 | + * @link https://github.com/scandipwa/quote-graphql |
| 11 | + */ |
| 12 | + |
| 13 | +declare(strict_types=1); |
| 14 | + |
| 15 | +namespace ScandiPWA\QuoteGraphQl\Model\Resolver; |
| 16 | + |
| 17 | +use Exception; |
| 18 | +use Magento\Directory\Model\CountryFactory; |
| 19 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 20 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 21 | +use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; |
| 22 | +use Magento\Framework\GraphQl\Query\Resolver\Value; |
| 23 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 24 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 25 | +use Magento\InventoryInStorePickup\Model\SearchRequestBuilder; |
| 26 | +use Magento\InventoryInStorePickup\Model\GetPickupLocations; |
| 27 | + |
| 28 | +/** |
| 29 | + * Class GetStores |
| 30 | + * @package ScandiPWA\QuoteGraphQl\Model\Resolver |
| 31 | + */ |
| 32 | +class GetStores implements ResolverInterface |
| 33 | +{ |
| 34 | + /** |
| 35 | + * Config path to radius value (In magento original class it's private) |
| 36 | + */ |
| 37 | + protected const SEARCH_RADIUS = 'carriers/instore/search_radius'; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var SearchRequestBuilder |
| 41 | + */ |
| 42 | + protected $searchRequest; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var GetPickupLocations |
| 46 | + */ |
| 47 | + protected $getPickupLocations; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var CountryFactory |
| 51 | + */ |
| 52 | + protected $countryFactory; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var ScopeConfigInterface |
| 56 | + */ |
| 57 | + protected $scopeConfig; |
| 58 | + |
| 59 | + /** |
| 60 | + * GetStores constructor. |
| 61 | + * @param SearchRequestBuilder $searchRequest |
| 62 | + * @param GetPickupLocations $getPickupLocations |
| 63 | + * @param CountryFactory $countryFactory |
| 64 | + * @param ScopeConfigInterface $scopeConfig |
| 65 | + */ |
| 66 | + public function __construct( |
| 67 | + SearchRequestBuilder $searchRequest, |
| 68 | + GetPickupLocations $getPickupLocations, |
| 69 | + CountryFactory $countryFactory, |
| 70 | + ScopeConfigInterface $scopeConfig |
| 71 | + ) { |
| 72 | + |
| 73 | + $this->searchRequest = $searchRequest; |
| 74 | + $this->getPickupLocations = $getPickupLocations; |
| 75 | + $this->countryFactory = $countryFactory; |
| 76 | + $this->scopeConfig = $scopeConfig; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Fetches the data from persistence models and format it according to the GraphQL schema. |
| 81 | + * |
| 82 | + * @param Field $field |
| 83 | + * @param ContextInterface $context |
| 84 | + * @param ResolveInfo $info |
| 85 | + * @param array|null $value |
| 86 | + * @param array|null $args |
| 87 | + * @return mixed|Value |
| 88 | + * @throws Exception |
| 89 | + */ |
| 90 | + public function resolve( |
| 91 | + Field $field, |
| 92 | + $context, |
| 93 | + ResolveInfo $info, |
| 94 | + array $value = null, |
| 95 | + array $args = null |
| 96 | + ) { |
| 97 | + if (!isset($args['search']) || !isset($args['country'])) { |
| 98 | + throw new GraphQlInputException( |
| 99 | + __('Required parameter "search" or "country" is missing.') |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + $result = []; |
| 104 | + |
| 105 | + $searchRequest = $this->searchRequest |
| 106 | + ->setAreaSearchTerm(sprintf( |
| 107 | + '%s:%s', |
| 108 | + $args['search'], |
| 109 | + $args['country'] |
| 110 | + )) |
| 111 | + ->setAreaRadius($this->scopeConfig->getValue(self::SEARCH_RADIUS)) |
| 112 | + ->setScopeCode('base') |
| 113 | + ->create(); |
| 114 | + $searchResponse = $this->getPickupLocations->execute($searchRequest); |
| 115 | + |
| 116 | + foreach ($searchResponse->getItems() as $item) { |
| 117 | + $result[] = [ |
| 118 | + 'city' => $item->getCity(), |
| 119 | + 'country' => $this->countryFactory->create()->loadByCode($item->getCountryId())->getName(), |
| 120 | + 'description' => $item->getDescription(), |
| 121 | + 'name' => $item->getName(), |
| 122 | + 'phone' => $item->getPhone(), |
| 123 | + 'pickup_location_code' => $item->getPickupLocationCode(), |
| 124 | + 'postcode' => $item->getPostcode(), |
| 125 | + 'region' => $item->getRegion(), |
| 126 | + 'street' => $item->getStreet() |
| 127 | + ]; |
| 128 | + } |
| 129 | + |
| 130 | + return ['stores' => $result]; |
| 131 | + } |
| 132 | +} |
0 commit comments