Skip to content

Commit

Permalink
Merge pull request #77 from AleksandrsKondratjevs/issue-2777
Browse files Browse the repository at this point in the history
Add fetch all stores
  • Loading branch information
carinadues authored Jul 23, 2021
2 parents 637e9c2 + 84d3d83 commit 7c8dde4
Showing 1 changed file with 86 additions and 18 deletions.
104 changes: 86 additions & 18 deletions src/Model/Resolver/GetStores.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@
namespace ScandiPWA\QuoteGraphQl\Model\Resolver;

use Exception;
use Magento\Directory\Model\Country\Postcode\ConfigInterface;
use Magento\Directory\Model\CountryFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\InventoryInStorePickup\Model\SearchRequestBuilder;
use Magento\InventoryInStorePickup\Model\GetPickupLocations;
use Magento\InventoryInStorePickupApi\Api\Data\SearchRequestInterface;
use Magento\InventoryInStorePickupApi\Model\SearchRequestBuilderInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class GetStores
Expand All @@ -37,7 +45,7 @@ class GetStores implements ResolverInterface
protected const SEARCH_RADIUS = 'carriers/instore/search_radius';

/**
* @var SearchRequestBuilder
* @var SearchRequestBuilderInterface
*/
protected $searchRequest;

Expand All @@ -56,24 +64,39 @@ class GetStores implements ResolverInterface
*/
protected $scopeConfig;

/**
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @var ConfigInterface
*/
protected $postCodesConfig;

/**
* GetStores constructor.
* @param SearchRequestBuilder $searchRequest
* @param SearchRequestBuilderInterface $searchRequest
* @param GetPickupLocations $getPickupLocations
* @param CountryFactory $countryFactory
* @param ScopeConfigInterface $scopeConfig
* @param StoreManagerInterface $storeManager
* @param ConfigInterface $postCodesConfig
*/
public function __construct(
SearchRequestBuilder $searchRequest,
SearchRequestBuilderInterface $searchRequest,
GetPickupLocations $getPickupLocations,
CountryFactory $countryFactory,
ScopeConfigInterface $scopeConfig
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager,
ConfigInterface $postCodesConfig
) {

$this->searchRequest = $searchRequest;
$this->getPickupLocations = $getPickupLocations;
$this->countryFactory = $countryFactory;
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
$this->postCodesConfig = $postCodesConfig;
}

/**
Expand All @@ -94,24 +117,34 @@ public function resolve(
array $value = null,
array $args = null
) {
if (!isset($args['search']) || !isset($args['country'])) {
$search = $args['search'];
$country = $args['country'];

if (!isset($search) || !isset($country)) {
throw new GraphQlInputException(
__('Required parameter "search" or "country" is missing.')
);
}

$result = [];

$searchRequest = $this->searchRequest
->setAreaSearchTerm(sprintf(
'%s:%s',
$args['search'],
$args['country']
))
->setAreaRadius((int) $this->scopeConfig->getValue(self::SEARCH_RADIUS))
->setScopeCode('base')
->create();
$searchResponse = $this->getPickupLocations->execute($searchRequest);
if($args['search'] === '') {
$searchRequest = $this->getAllStores();
} else {
$postCodes = $this->postCodesConfig->getPostCodes();

if (!isset($postCodes[$country])) {
throw new GraphQlInputException(__('No in-delivery support for provided country. Please select another country.'));
}

$searchRequest = $this->getStoresBySearch($search, $country);
}

try {
$searchResponse = $this->getPickupLocations->execute($searchRequest);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
}

foreach ($searchResponse->getItems() as $item) {
$result[] = [
Expand All @@ -129,4 +162,39 @@ public function resolve(

return ['stores' => $result];
}

/**
* @return SearchRequestInterface
* @throws LocalizedException
*/
public function getAllStores() {
$searchRequest = $this->searchRequest
->setScopeType(SalesChannelInterface::TYPE_WEBSITE)
->setScopeCode($this->storeManager->getWebsite()->getCode())
->setPageSize(1)
->create();

return $searchRequest;
}

/**
* @param $search
* @param $country
* @return SearchRequestInterface
* @throws LocalizedException
*/
public function getStoresBySearch($search, $country) {
$searchRequest = $this->searchRequest
->setScopeType(SalesChannelInterface::TYPE_WEBSITE)
->setAreaSearchTerm(sprintf(
'%s:%s',
$search,
$country
))
->setScopeCode($this->storeManager->getWebsite()->getCode())
->setAreaRadius((int) $this->scopeConfig->getValue(self::SEARCH_RADIUS))
->create();

return $searchRequest;
}
}

0 comments on commit 7c8dde4

Please sign in to comment.