-
Notifications
You must be signed in to change notification settings - Fork 247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Msi 2182 source entity expansion with pickup location attributes #2267
Changes from 10 commits
4da7b8b
10401ca
f6d9bb5
2314c9f
748fd78
7426756
7559630
551568b
143b67f
f46fe29
28c8dda
f57691c
3b64018
e276996
86f5f3a
0a20315
ca323b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\InventoryInStorePickup\Model\Source; | ||
|
||
use Magento\Framework\Api\ExtensionAttributesFactory; | ||
use Magento\Framework\DataObject; | ||
use Magento\InventoryApi\Api\Data\SourceInterface; | ||
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface; | ||
|
||
/** | ||
* Set store-pickup related source extension attributes | ||
*/ | ||
class SetExtensionAttributes | ||
{ | ||
/** | ||
* @var ExtensionAttributesFactory | ||
*/ | ||
private $extensionAttributesFactory; | ||
|
||
/** | ||
* @param ExtensionAttributesFactory $extensionAttributesFactory | ||
*/ | ||
public function __construct(ExtensionAttributesFactory $extensionAttributesFactory) | ||
{ | ||
$this->extensionAttributesFactory = $extensionAttributesFactory; | ||
} | ||
/** | ||
* @param SourceInterface|DataObject $source | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep doc block and contract consistent There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
*/ | ||
public function execute(SourceInterface $source): void | ||
{ | ||
$pickupAvailable = $source->getData(PickupLocationInterface::IS_PICKUP_LOCATION_ACTIVE); | ||
$frontendName = $source->getData(PickupLocationInterface::FRONTEND_NAME); | ||
$frontendDescription = $source->getData(PickupLocationInterface::FRONTEND_DESCRIPTION); | ||
ishakhsuvarov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$extensionAttributes = $source->getExtensionAttributes(); | ||
|
||
if ($extensionAttributes === null) { | ||
$extensionAttributes = $this->extensionAttributesFactory->create(SourceInterface::class); | ||
/** @noinspection PhpParamsInspection */ | ||
$source->setExtensionAttributes($extensionAttributes); | ||
} | ||
|
||
$extensionAttributes | ||
->setIsPickupLocationActive((bool)$pickupAvailable) | ||
->setFrontendName($frontendName) | ||
->setFrontendDescription($frontendDescription); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,25 +7,24 @@ | |
|
||
namespace Magento\InventoryInStorePickup\Plugin\InventoryApi\SourceRepository; | ||
|
||
use Magento\Framework\Api\ExtensionAttributesFactory; | ||
use Magento\InventoryApi\Api\Data\SourceInterface; | ||
use Magento\InventoryApi\Api\Data\SourceSearchResultsInterface; | ||
use Magento\InventoryApi\Api\SourceRepositoryInterface; | ||
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface; | ||
use Magento\InventoryInStorePickup\Model\Source\SetExtensionAttributes; | ||
|
||
class LoadInStorePickupOnGetListPlugin | ||
{ | ||
/** | ||
* @var ExtensionAttributesFactory | ||
* @var SetExtensionAttributes | ||
*/ | ||
private $extensionAttributesFactory; | ||
private $setExtensionAttributes; | ||
|
||
/** | ||
* @param ExtensionAttributesFactory $extensionAttributesFactory | ||
* @param SetExtensionAttributes $setExtensionAttributes | ||
*/ | ||
public function __construct(ExtensionAttributesFactory $extensionAttributesFactory) | ||
{ | ||
$this->extensionAttributesFactory = $extensionAttributesFactory; | ||
public function __construct( | ||
SetExtensionAttributes $setExtensionAttributes | ||
) { | ||
$this->setExtensionAttributes = $setExtensionAttributes; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this seems to be an excessive extraction, it seems to be the bet solution if we don't figure out to load data in a better way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, as a variant, it probably would be nice to extract this logic into a chain of responsibility. |
||
} | ||
|
||
/** | ||
|
@@ -40,18 +39,12 @@ public function __construct(ExtensionAttributesFactory $extensionAttributesFacto | |
public function afterGetList( | ||
SourceRepositoryInterface $subject, | ||
SourceSearchResultsInterface $sourceSearchResults | ||
):SourceSearchResultsInterface { | ||
foreach ($sourceSearchResults->getItems() as $source) { | ||
$extensionAttributes = $source->getExtensionAttributes(); | ||
|
||
if ($extensionAttributes === null) { | ||
$extensionAttributes = $this->extensionAttributesFactory->create(SourceInterface::class); | ||
$source->setExtensionAttributes($extensionAttributes); | ||
} | ||
|
||
$pickupAvailable = $source->getData(PickupLocationInterface::IS_PICKUP_LOCATION_ACTIVE); | ||
$extensionAttributes->setIsPickupLocationActive((bool)$pickupAvailable); | ||
} | ||
): SourceSearchResultsInterface { | ||
$items = $sourceSearchResults->getItems(); | ||
array_walk( | ||
$items, | ||
[$this->setExtensionAttributes, 'execute'] | ||
); | ||
|
||
return $sourceSearchResults; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,49 +7,41 @@ | |
|
||
namespace Magento\InventoryInStorePickup\Plugin\InventoryApi\SourceRepository; | ||
|
||
use Magento\Framework\Api\ExtensionAttributesFactory; | ||
use Magento\Framework\DataObject; | ||
use Magento\InventoryApi\Api\Data\SourceInterface; | ||
use Magento\InventoryApi\Api\SourceRepositoryInterface; | ||
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface; | ||
use Magento\InventoryInStorePickup\Model\Source\SetExtensionAttributes; | ||
|
||
class LoadInStorePickupOnGetPlugin | ||
{ | ||
/** | ||
* @var \Magento\Framework\Api\ExtensionAttributesFactory | ||
* @var SetExtensionAttributes | ||
*/ | ||
private $extensionAttributesFactory; | ||
private $setExtensionAttributes; | ||
|
||
/** | ||
* @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionAttributesFactory | ||
* @param SetExtensionAttributes $setExtensionAttributes | ||
*/ | ||
public function __construct(ExtensionAttributesFactory $extensionAttributesFactory) | ||
{ | ||
$this->extensionAttributesFactory = $extensionAttributesFactory; | ||
public function __construct( | ||
SetExtensionAttributes $setExtensionAttributes | ||
) { | ||
$this->setExtensionAttributes = $setExtensionAttributes; | ||
} | ||
|
||
/** | ||
* Enrich the given Source Objects with the In-Store pickup attribute | ||
* | ||
* @param SourceRepositoryInterface $subject | ||
* @param SourceInterface $source | ||
* @param SourceInterface|DataObject $source | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's cheating. We can't typehint for SourceInterface and rely on Dataobject in reality There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
* | ||
* @return SourceInterface | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function afterGet( | ||
SourceRepositoryInterface $subject, | ||
SourceInterface $source | ||
):SourceInterface { | ||
$pickupAvailable = $source->getData(PickupLocationInterface::IS_PICKUP_LOCATION_ACTIVE); | ||
|
||
$extensionAttributes = $source->getExtensionAttributes(); | ||
|
||
if ($extensionAttributes === null) { | ||
$extensionAttributes = $this->extensionAttributesFactory->create(SourceInterface::class); | ||
$source->setExtensionAttributes($extensionAttributes); | ||
} | ||
|
||
$extensionAttributes->setIsPickupLocationActive((bool)$pickupAvailable); | ||
): SourceInterface { | ||
$this->setExtensionAttributes->execute($source); | ||
|
||
return $source; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,28 +7,32 @@ | |
|
||
namespace Magento\InventoryInStorePickup\Plugin\InventoryApi\SourceRepository; | ||
|
||
use Magento\Framework\DataObject; | ||
use Magento\InventoryApi\Api\Data\SourceInterface; | ||
use Magento\InventoryApi\Api\SourceRepositoryInterface; | ||
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface as Location; | ||
|
||
class SaveInStorePickupPlugin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add description while we are here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
{ | ||
/** | ||
* Persist the In-Store pickup attribute on Source save | ||
* | ||
* @param SourceRepositoryInterface $subject | ||
* @param SourceInterface $source | ||
* @param SourceInterface|DataObject $source | ||
* | ||
* @return array | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function beforeSave( | ||
SourceRepositoryInterface $subject, | ||
SourceInterface $source | ||
):array { | ||
): array { | ||
$extensionAttributes = $source->getExtensionAttributes(); | ||
|
||
if ($extensionAttributes !== null && $extensionAttributes->getIsPickupLocationActive() !== null) { | ||
$source->setIsPickupLocationActive($extensionAttributes->getIsPickupLocationActive()); | ||
if ($extensionAttributes !== null) { | ||
$source->setData(Location::IS_PICKUP_LOCATION_ACTIVE, $extensionAttributes->getIsPickupLocationActive()); | ||
$source->setData(Location::FRONTEND_NAME, $extensionAttributes->getFrontendName()); | ||
$source->setData(Location::FRONTEND_DESCRIPTION, $extensionAttributes->getFrontendDescription()); | ||
} | ||
|
||
return [$source]; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\InventoryInStorePickup\Plugin\InventoryApi\SourceValidatorChain; | ||
|
||
use Magento\Framework\DataObject; | ||
use Magento\InventoryApi\Api\Data\SourceInterface; | ||
use Magento\InventoryApi\Model\SourceValidatorChain; | ||
|
||
/** | ||
* Set frontend name the same as regular name if it is empty | ||
*/ | ||
class SetFrontendName | ||
{ | ||
/** | ||
* @param SourceValidatorChain $chain | ||
* @param SourceInterface $source | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function beforeValidate(SourceValidatorChain $chain, SourceInterface $source) | ||
{ | ||
if ($source->getExtensionAttributes() === null) { | ||
return; | ||
} | ||
|
||
$frontendName = trim($source->getExtensionAttributes()->getFrontendName()); | ||
|
||
if ($frontendName === '') { | ||
$source->getExtensionAttributes()->setFrontendName($source->getName()); | ||
if ($source instanceof DataObject) { | ||
$source->setData('frontend_name', $source->getExtensionAttributes()->getFrontendName()); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Class name seems to abstract
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
InitPickupLocationExtensionAttributes
?