Skip to content

Commit

Permalink
#2839: Used extension attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Idolov committed Mar 5, 2020
1 parent c987966 commit e919c4a
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 205 deletions.
12 changes: 12 additions & 0 deletions InventoryInStorePickup/etc/extension_attributes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\InventoryInStorePickupApi\Api\Data\SearchRequestInterface">
<attribute code="products_info" type="Magento\InventoryInStorePickupShippingApi\Api\Data\ProductInfoInterface[]"/>
</extension_attributes>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryInStorePickupShipping\Model;

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\InventoryInStorePickupShipping\Model\ResourceModel\GetPickupLocationIntersectionForSkus;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\InventoryApi\Api\Data\StockSourceLinkInterface;
use Magento\InventoryApi\Api\GetStockSourceLinksInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryInStorePickupShipping\Model\ResourceModel\GetPickupLocationIntersectionForSkus;
use Magento\InventoryInStorePickupShippingApi\Api\Data\RequestInterface;
use Magento\InventoryInStorePickupShippingApi\Api\GetAvailableLocationsForPickupInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;

/**
* @inheritdoc
* Filter pickup locations according to products availability.
*/
class GetAvailableLocationsForPickup implements GetAvailableLocationsForPickupInterface
class FilterSourcesByProductsAvailabilityPlugin
{
/**
* @var GetPickupLocationIntersectionForSkus
Expand All @@ -37,50 +37,79 @@ class GetAvailableLocationsForPickup implements GetAvailableLocationsForPickupIn
private $getStockSourceLinks;

/**
* @var SourceRepositoryInterface
* @var SearchCriteriaBuilder
*/
private $sourceRepository;
private $searchCriteriaBuilder;

/**
* @var SearchCriteriaBuilder
* @var SearchResultInterfaceFactory
*/
private $searchCriteriaBuilder;
private $searchResultFactory;

/**
* @param GetPickupLocationIntersectionForSkus $getPickupLocationIntersectionForSkues
* @param StockResolverInterface $stockResolver
* @param GetStockSourceLinksInterface $getStockSourceLinks
* @param SourceRepositoryInterface $sourceRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param GetStockSourceLinksInterface $getStockSourceLinks
* @param SearchResultInterfaceFactory $searchResultFactory
*/
public function __construct(
GetPickupLocationIntersectionForSkus $getPickupLocationIntersectionForSkues,
StockResolverInterface $stockResolver,
SearchCriteriaBuilder $searchCriteriaBuilder,
GetStockSourceLinksInterface $getStockSourceLinks,
SourceRepositoryInterface $sourceRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
SearchResultInterfaceFactory $searchResultFactory
) {
$this->getPickupLocationIntersectionForSkues = $getPickupLocationIntersectionForSkues;
$this->stockResolver = $stockResolver;
$this->getStockSourceLinks = $getStockSourceLinks;
$this->sourceRepository = $sourceRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->searchResultFactory = $searchResultFactory;
}

/**
* @inheritdoc
* @param GetPickupLocationsInterface $subject
* @param SearchResultInterface $result
* @param SearchRequestInterface $request
* @return SearchResultInterface
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function execute(RequestInterface $request): array
{
public function afterExecute(
GetPickupLocationsInterface $subject,
SearchResultInterface $result,
SearchRequestInterface $request
) : SearchResultInterface {
$extensionAttributes = $request->getExtensionAttributes();
if (!$extensionAttributes || !$extensionAttributes->getProductsInfo()) {
return $result;
}

$skus = [];
foreach ($request->getProductsInfo() as $item) {
foreach ($extensionAttributes->getProductsInfo() as $item) {
$skus[] = $item->getSku();
}
$sourceCodes = $this->getPickupLocationIntersectionForSkues->execute($skus);

$stock = $this->stockResolver->execute($request->getScopeType(), $request->getScopeCode());
$sourceCodesAssignedToStock = $this->getSourceCodesAssignedToStock($stock->getStockId());
$availableSources = array_intersect(
$this->getPickupLocationIntersectionForSkues->execute($skus),
$this->getSourceCodesAssignedToStock($stock->getStockId())
);

$availableLocations = [];
foreach ($result->getItems() as $pickupLocation) {
if (in_array($pickupLocation->getPickupLocationCode(), $availableSources)) {
$availableLocations[] = $pickupLocation;
}
}

return array_intersect($sourceCodes, $sourceCodesAssignedToStock);
return $this->searchResultFactory->create(
[
'items' => $availableLocations,
'totalCount' => count($availableLocations),
'searchRequest' => $request
]
);
}

/**
Expand Down
98 changes: 0 additions & 98 deletions InventoryInStorePickupShipping/Model/Request.php

This file was deleted.

6 changes: 3 additions & 3 deletions InventoryInStorePickupShipping/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
<preference for="Magento\InventoryInStorePickupShippingApi\Model\Carrier\Command\GetShippingPriceInterface" type="Magento\InventoryInStorePickupShipping\Model\Carrier\Command\GetShippingPrice" />
<preference for="Magento\InventoryInStorePickupShippingApi\Model\Carrier\Command\GetShippingPriceRequestInterface" type="Magento\InventoryInStorePickupShipping\Model\Carrier\Command\GetShippingPriceRequest" />
<preference for="Magento\InventoryInStorePickupShippingApi\Model\IsInStorePickupDeliveryCartInterface" type="Magento\InventoryInStorePickupShipping\Model\IsInStorePickupDeliveryCart" />
<preference for="Magento\InventoryInStorePickupShippingApi\Api\Data\RequestInterface" type="Magento\InventoryInStorePickupShipping\Model\Request" />
<preference for="Magento\InventoryInStorePickupShippingApi\Api\GetAvailableLocationsForPickupInterface" type="Magento\InventoryInStorePickupShipping\Model\GetAvailableLocationsForPickup" />
<preference for="Magento\InventoryInStorePickupShippingApi\Api\Data\ProductInfoInterface" type="Magento\InventoryInStorePickupShipping\Model\ProductInfo" />

<type name="Magento\InventoryInStorePickupShippingApi\Model\Carrier\Validation\RequestValidatorChain">
<arguments>
<argument name="validators" xsi:type="array">
<item name="sales_channel" xsi:type="object">Magento\InventoryInStorePickupShipping\Model\Carrier\Validation\SalesChannelValidator</item>
</argument>
</arguments>
</type>
<type name="Magento\InventoryInStorePickupApi\Api\GetPickupLocationsInterface">
<plugin name="filter_locations_by_products_availability" type="Magento\InventoryInStorePickupShipping\Model\FilterSourcesByProductsAvailabilityPlugin" />
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function getExtensionAttributes(): ?ProductInfoExtensionInterface;
* Set Product Info Extension.
*
* @param \Magento\InventoryInStorePickupShippingApi\Api\Data\ProductInfoExtensionInterface $productInfoExtension
* @return void
*/
public function setExtensionAttributes(ProductInfoExtensionInterface $productInfoExtension): void;
}
54 changes: 0 additions & 54 deletions InventoryInStorePickupShippingApi/Api/Data/RequestInterface.php

This file was deleted.

This file was deleted.

0 comments on commit e919c4a

Please sign in to comment.