-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-7685: [GraphQl] Adding Wishlist GraphQl coverage #28205
- Loading branch information
Showing
34 changed files
with
2,983 additions
and
5 deletions.
There are no files selected for viewing
164 changes: 164 additions & 0 deletions
164
app/code/Magento/Wishlist/Model/Wishlist/AddProductsToWishlist.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Wishlist\Model\Wishlist; | ||
|
||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Framework\Exception\AlreadyExistsException; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel; | ||
use Magento\Wishlist\Model\Wishlist; | ||
use Magento\Wishlist\Model\Wishlist\BuyRequest\BuyRequestBuilder; | ||
use Magento\Wishlist\Model\Wishlist\Data\WishlistItem; | ||
use Magento\Wishlist\Model\Wishlist\Data\WishlistOutput; | ||
|
||
/** | ||
* Adding products to wishlist | ||
*/ | ||
class AddProductsToWishlist | ||
{ | ||
/**#@+ | ||
* Error message codes | ||
*/ | ||
private const ERROR_PRODUCT_NOT_FOUND = 'PRODUCT_NOT_FOUND'; | ||
private const ERROR_UNDEFINED = 'UNDEFINED'; | ||
/**#@-*/ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $errors = []; | ||
|
||
/** | ||
* @var BuyRequestBuilder | ||
*/ | ||
private $buyRequestBuilder; | ||
|
||
/** | ||
* @var ProductRepositoryInterface | ||
*/ | ||
private $productRepository; | ||
|
||
/** | ||
* @var WishlistResourceModel | ||
*/ | ||
private $wishlistResource; | ||
|
||
/** | ||
* @param ProductRepositoryInterface $productRepository | ||
* @param BuyRequestBuilder $buyRequestBuilder | ||
* @param WishlistResourceModel $wishlistResource | ||
*/ | ||
public function __construct( | ||
ProductRepositoryInterface $productRepository, | ||
BuyRequestBuilder $buyRequestBuilder, | ||
WishlistResourceModel $wishlistResource | ||
) { | ||
$this->productRepository = $productRepository; | ||
$this->buyRequestBuilder = $buyRequestBuilder; | ||
$this->wishlistResource = $wishlistResource; | ||
} | ||
|
||
/** | ||
* Adding products to wishlist | ||
* | ||
* @param Wishlist $wishlist | ||
* @param array $wishlistItems | ||
* | ||
* @return WishlistOutput | ||
* | ||
* @throws AlreadyExistsException | ||
*/ | ||
public function execute(Wishlist $wishlist, array $wishlistItems): WishlistOutput | ||
{ | ||
foreach ($wishlistItems as $wishlistItem) { | ||
$this->addItemToWishlist($wishlist, $wishlistItem); | ||
} | ||
|
||
$wishlistOutput = $this->prepareOutput($wishlist); | ||
|
||
if ($wishlist->isObjectNew() || count($wishlistOutput->getErrors()) !== count($wishlistItems)) { | ||
$this->wishlistResource->save($wishlist); | ||
} | ||
|
||
return $wishlistOutput; | ||
} | ||
|
||
/** | ||
* Add product item to wishlist | ||
* | ||
* @param Wishlist $wishlist | ||
* @param WishlistItem $wishlistItem | ||
* | ||
* @return void | ||
*/ | ||
private function addItemToWishlist(Wishlist $wishlist, WishlistItem $wishlistItem): void | ||
{ | ||
$sku = $wishlistItem->getParentSku() ?? $wishlistItem->getSku(); | ||
|
||
try { | ||
$product = $this->productRepository->get($sku, false, null, true); | ||
} catch (NoSuchEntityException $e) { | ||
$this->addError( | ||
__('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(), | ||
self::ERROR_PRODUCT_NOT_FOUND | ||
); | ||
|
||
return; | ||
} | ||
|
||
try { | ||
$options = $this->buyRequestBuilder->build($wishlistItem, (int) $product->getId()); | ||
$result = $wishlist->addNewItem($product, $options); | ||
|
||
if (is_string($result)) { | ||
$this->addError($result); | ||
} | ||
} catch (LocalizedException $exception) { | ||
$this->addError($exception->getMessage()); | ||
} catch (\Throwable $e) { | ||
$this->addError( | ||
__( | ||
'Could not add the product with SKU "%sku" to the wishlist:: %message', | ||
['sku' => $sku, 'message' => $e->getMessage()] | ||
)->render() | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Add wishlist line item error | ||
* | ||
* @param string $message | ||
* @param string|null $code | ||
* | ||
* @return void | ||
*/ | ||
private function addError(string $message, string $code = null): void | ||
{ | ||
$this->errors[] = new Data\Error( | ||
$message, | ||
$code ?? self::ERROR_UNDEFINED | ||
); | ||
} | ||
|
||
/** | ||
* Prepare output | ||
* | ||
* @param Wishlist $wishlist | ||
* | ||
* @return WishlistOutput | ||
*/ | ||
private function prepareOutput(Wishlist $wishlist): WishlistOutput | ||
{ | ||
$output = new WishlistOutput($wishlist, $this->errors); | ||
$this->errors = []; | ||
|
||
return $output; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BundleDataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\Wishlist\Model\Wishlist\BuyRequest; | ||
|
||
use Magento\Wishlist\Model\Wishlist\Data\WishlistItem; | ||
|
||
/** | ||
* Data provider for bundle product buy requests | ||
*/ | ||
class BundleDataProvider implements BuyRequestDataProviderInterface | ||
{ | ||
private const PROVIDER_OPTION_TYPE = 'bundle'; | ||
|
||
/** | ||
* @inheritdoc | ||
* | ||
* @phpcs:disable Magento2.Functions.DiscouragedFunction | ||
*/ | ||
public function execute(WishlistItem $wishlistItem, ?int $productId): array | ||
{ | ||
$bundleOptionsData = []; | ||
|
||
foreach ($wishlistItem->getSelectedOptions() as $optionData) { | ||
$optionData = \explode('/', base64_decode($optionData->getId())); | ||
|
||
if ($this->isProviderApplicable($optionData) === false) { | ||
continue; | ||
} | ||
|
||
[, $optionId, $optionValueId, $optionQuantity] = $optionData; | ||
|
||
$bundleOptionsData['bundle_option'][$optionId] = $optionValueId; | ||
$bundleOptionsData['bundle_option_qty'][$optionId] = $optionQuantity; | ||
} | ||
|
||
return $bundleOptionsData; | ||
} | ||
|
||
/** | ||
* Checks whether this provider is applicable for the current option | ||
* | ||
* @param array $optionData | ||
* | ||
* @return bool | ||
*/ | ||
private function isProviderApplicable(array $optionData): bool | ||
{ | ||
return $optionData[0] === self::PROVIDER_OPTION_TYPE; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BuyRequestBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Wishlist\Model\Wishlist\BuyRequest; | ||
|
||
use Magento\Framework\DataObject; | ||
use Magento\Framework\DataObjectFactory; | ||
use Magento\Wishlist\Model\Wishlist\Data\WishlistItem; | ||
|
||
/** | ||
* Building buy request for all product types | ||
*/ | ||
class BuyRequestBuilder | ||
{ | ||
/** | ||
* @var BuyRequestDataProviderInterface[] | ||
*/ | ||
private $providers; | ||
|
||
/** | ||
* @var DataObjectFactory | ||
*/ | ||
private $dataObjectFactory; | ||
|
||
/** | ||
* @param DataObjectFactory $dataObjectFactory | ||
* @param array $providers | ||
*/ | ||
public function __construct( | ||
DataObjectFactory $dataObjectFactory, | ||
array $providers = [] | ||
) { | ||
$this->dataObjectFactory = $dataObjectFactory; | ||
$this->providers = $providers; | ||
} | ||
|
||
/** | ||
* Build product buy request for adding to wishlist | ||
* | ||
* @param WishlistItem $wishlistItemData | ||
* @param int|null $productId | ||
* | ||
* @return DataObject | ||
*/ | ||
public function build(WishlistItem $wishlistItemData, ?int $productId = null): DataObject | ||
{ | ||
$requestData = [ | ||
[ | ||
'qty' => $wishlistItemData->getQuantity(), | ||
] | ||
]; | ||
|
||
foreach ($this->providers as $provider) { | ||
$requestData[] = $provider->execute($wishlistItemData, $productId); | ||
} | ||
|
||
return $this->dataObjectFactory->create(['data' => array_merge(...$requestData)]); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BuyRequestDataProviderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Wishlist\Model\Wishlist\BuyRequest; | ||
|
||
use Magento\Wishlist\Model\Wishlist\Data\WishlistItem; | ||
|
||
/** | ||
* Build buy request for adding products to wishlist | ||
*/ | ||
interface BuyRequestDataProviderInterface | ||
{ | ||
/** | ||
* Provide buy request data from add to wishlist item request | ||
* | ||
* @param WishlistItem $wishlistItemData | ||
* @param int|null $productId | ||
* | ||
* @return array | ||
*/ | ||
public function execute(WishlistItem $wishlistItemData, ?int $productId): array; | ||
} |
Oops, something went wrong.