-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
[GraphQl] Adding Wishlist GraphQl coverage #28205
Merged
magento-engcom-team
merged 22 commits into
magento:2.4-develop
from
eduard13:feature/wishlist-graphql-258
Jun 23, 2020
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
5de8d9b
Adding Wishlist GraphQl support
eduard13 eddb4eb
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 7488a36
Test coverage for Wishlist GraphQl
eduard13 3346f33
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 28b1d02
Implementing Todos and adjusting the tests
eduard13 c988ab2
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 58215aa
Fixing Unit Test
eduard13 73650e3
Fixing Unit Test
eduard13 a5cc790
Improving checking the customer wishlist
eduard13 9e023a4
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 08bd47e
Fixing static tests
eduard13 27c3142
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 71ebd52
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 8370818
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 f0a4bc9
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 476ea33
Converting the arguments in camelcase format
eduard13 f1d13b7
Converting the arguments in camelcase format
eduard13 5bc151b
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 b71144d
Merge branch '2.4-develop' into feature/wishlist-graphql-258
avattam06 987c753
Update schema.graphqls
eduard13 c1ce778
Small schema fix
eduard13 487b29e
Small schema fixes
eduard13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
162 changes: 162 additions & 0 deletions
162
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,162 @@ | ||
<?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 | ||
*/ | ||
private function addItemToWishlist(Wishlist $wishlist, WishlistItem $wishlistItem) | ||
{ | ||
$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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please, specify the return type