149 lines
4.8 KiB
PHP
149 lines
4.8 KiB
PHP
<?php
|
|
/**
|
|
* Copyright © Magento, Inc. All rights reserved.
|
|
* See COPYING.txt for license details.
|
|
*/
|
|
declare(strict_types=1);
|
|
|
|
namespace Lts\Inventory\Model;
|
|
|
|
use Lts\Inventory\Api\Data\SourceCustomerInterfaceFactory;
|
|
use Lts\Inventory\Model\ResourceModel\SourceCustomer as ResourceInventorySourceCustomer;
|
|
use Magento\Customer\Model\Session;
|
|
use Magento\Framework\Exception\NoSuchEntityException;
|
|
use Magento\Inventory\Model\Source;
|
|
use Magento\InventoryApi\Api\Data\SourceInterface;
|
|
use Magento\InventoryApi\Api\SourceRepositoryInterface;
|
|
use Magento\InventoryInStorePickupApi\Api\Data\SearchRequestInterface;
|
|
use Magento\InventoryInStorePickupApi\Api\Data\SearchResultInterface;
|
|
use Magento\InventoryInStorePickupApi\Api\Data\SearchResultInterfaceFactory;
|
|
use Magento\InventoryInStorePickupApi\Api\GetPickupLocationsInterface;
|
|
use Magento\InventoryInStorePickupApi\Model\Mapper;
|
|
use Magento\InventoryInStorePickupApi\Model\SearchCriteriaResolverInterface;
|
|
use Magento\InventoryInStorePickupApi\Model\SearchResult\ExtractorInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
class GetPickupLocations implements GetPickupLocationsInterface
|
|
{
|
|
/**
|
|
* @var Mapper
|
|
*/
|
|
private Mapper $mapper;
|
|
|
|
/**
|
|
* @var SourceRepositoryInterface
|
|
*/
|
|
private SourceRepositoryInterface $sourceRepository;
|
|
|
|
/**
|
|
* @var SearchCriteriaResolverInterface
|
|
*/
|
|
private SearchCriteriaResolverInterface $searchCriteriaResolver;
|
|
|
|
/**
|
|
* @var ExtractorInterface
|
|
*/
|
|
private ExtractorInterface $extractor;
|
|
|
|
/**
|
|
* @var SearchResultInterfaceFactory
|
|
*/
|
|
private SearchResultInterfaceFactory $searchResultFactory;
|
|
private LoggerInterface $logger;
|
|
private ResourceInventorySourceCustomer $resource;
|
|
private SourceCustomerInterfaceFactory $sourceCustomerFactory;
|
|
private Session $customerSession;
|
|
|
|
const DEFAULT_SOURCE = 'CA-LA';
|
|
/**
|
|
* @var Source[]
|
|
*/
|
|
private array $sources;
|
|
|
|
/**
|
|
* @param Mapper $mapper
|
|
* @param SourceRepositoryInterface $sourceRepository
|
|
* @param ExtractorInterface $extractor
|
|
* @param SearchCriteriaResolverInterface $searchCriteriaResolver
|
|
* @param SearchResultInterfaceFactory $searchResultFactory
|
|
*/
|
|
public function __construct(
|
|
Mapper $mapper,
|
|
SourceRepositoryInterface $sourceRepository,
|
|
ExtractorInterface $extractor,
|
|
SearchCriteriaResolverInterface $searchCriteriaResolver,
|
|
SearchResultInterfaceFactory $searchResultFactory,
|
|
LoggerInterface $logger,
|
|
ResourceInventorySourceCustomer $resource,
|
|
SourceCustomerInterfaceFactory $sourceCustomerFactory,
|
|
Session $customerSession,
|
|
) {
|
|
$this->mapper = $mapper;
|
|
$this->sourceRepository = $sourceRepository;
|
|
$this->searchCriteriaResolver = $searchCriteriaResolver;
|
|
$this->extractor = $extractor;
|
|
$this->searchResultFactory = $searchResultFactory;
|
|
$this->logger = $logger;
|
|
$this->customerSession = $customerSession;
|
|
$this->resource = $resource;
|
|
$this->sourceCustomerFactory = $sourceCustomerFactory;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function getPreferredSource()
|
|
{
|
|
if (!empty($this->sources)) {
|
|
return $this->sources;
|
|
}
|
|
$customer = $this->customerSession->getCustomer();
|
|
if ($customer->getId()) {
|
|
/* @var $model SourceInterface */
|
|
$sourceCustomer = $this->sourceCustomerFactory->create();
|
|
$this->resource->load($sourceCustomer, $customer->getId(), 'customer_id');
|
|
|
|
try {
|
|
if (!$sourceCustomer->getSourceCode()) {
|
|
$sourceCustomer->setSourceCode(self::DEFAULT_SOURCE);
|
|
}
|
|
return $this->sources = [$this->sourceRepository->get($sourceCustomer->getSourceCode())];
|
|
} catch (\Exception $e) {
|
|
}
|
|
}
|
|
return [];
|
|
}
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function execute(SearchRequestInterface $searchRequest): SearchResultInterface
|
|
{
|
|
$searchCriteria = $this->searchCriteriaResolver->resolve($searchRequest);
|
|
$searchResult = $this->sourceRepository->getList($searchCriteria);
|
|
|
|
if (!empty($this->getPreferredSource())){
|
|
$sources = $this->getPreferredSource();
|
|
$searchResult->setTotalCount(1);
|
|
} else {
|
|
$sources = $this->extractor->getSources($searchRequest, $searchResult);
|
|
}
|
|
|
|
$pickupLocations = [];
|
|
|
|
foreach ($sources as $source) {
|
|
$pickupLocations[] = $this->mapper->map($source);
|
|
}
|
|
|
|
return $this->searchResultFactory->create(
|
|
[
|
|
'items' => $pickupLocations,
|
|
'totalCount' => $searchResult->getTotalCount(),
|
|
'searchRequest' => $searchRequest
|
|
]
|
|
);
|
|
}
|
|
}
|