Skip to content
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
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5de8d9b
Adding Wishlist GraphQl support
eduard13 May 11, 2020
eddb4eb
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 May 20, 2020
7488a36
Test coverage for Wishlist GraphQl
eduard13 May 22, 2020
3346f33
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 May 22, 2020
28b1d02
Implementing Todos and adjusting the tests
eduard13 May 25, 2020
c988ab2
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 May 26, 2020
58215aa
Fixing Unit Test
eduard13 May 26, 2020
73650e3
Fixing Unit Test
eduard13 May 26, 2020
a5cc790
Improving checking the customer wishlist
eduard13 Jun 2, 2020
9e023a4
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 Jun 2, 2020
08bd47e
Fixing static tests
eduard13 Jun 2, 2020
27c3142
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 Jun 4, 2020
71ebd52
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 Jun 4, 2020
8370818
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 Jun 8, 2020
f0a4bc9
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 Jun 10, 2020
476ea33
Converting the arguments in camelcase format
eduard13 Jun 11, 2020
f1d13b7
Converting the arguments in camelcase format
eduard13 Jun 11, 2020
5bc151b
Merge branch '2.4-develop' into feature/wishlist-graphql-258
eduard13 Jun 12, 2020
b71144d
Merge branch '2.4-develop' into feature/wishlist-graphql-258
avattam06 Jun 17, 2020
987c753
Update schema.graphqls
eduard13 Jun 23, 2020
c1ce778
Small schema fix
eduard13 Jun 23, 2020
487b29e
Small schema fixes
eduard13 Jun 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions app/code/Magento/Wishlist/Model/Wishlist/AddProductsToWishlist.php
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;
}
}
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;
}
}
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)]);
}
}
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;
}
Loading