Skip to content

Commit fa5c921

Browse files
ENGCOM-7685: [GraphQl] Adding Wishlist GraphQl coverage #28205
- Merge Pull Request #28205 from eduard13/magento2:feature/wishlist-graphql-258 - Merged commits: 1. 5de8d9b 2. eddb4eb 3. 7488a36 4. 3346f33 5. 28b1d02 6. c988ab2 7. 58215aa 8. 73650e3 9. a5cc790 10. 9e023a4 11. 08bd47e 12. 27c3142 13. 71ebd52 14. 8370818 15. f0a4bc9 16. 476ea33 17. f1d13b7 18. 5bc151b 19. b71144d
2 parents 679cd33 + b71144d commit fa5c921

34 files changed

+2983
-5
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Wishlist\Model\Wishlist;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\Exception\AlreadyExistsException;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel;
15+
use Magento\Wishlist\Model\Wishlist;
16+
use Magento\Wishlist\Model\Wishlist\BuyRequest\BuyRequestBuilder;
17+
use Magento\Wishlist\Model\Wishlist\Data\WishlistItem;
18+
use Magento\Wishlist\Model\Wishlist\Data\WishlistOutput;
19+
20+
/**
21+
* Adding products to wishlist
22+
*/
23+
class AddProductsToWishlist
24+
{
25+
/**#@+
26+
* Error message codes
27+
*/
28+
private const ERROR_PRODUCT_NOT_FOUND = 'PRODUCT_NOT_FOUND';
29+
private const ERROR_UNDEFINED = 'UNDEFINED';
30+
/**#@-*/
31+
32+
/**
33+
* @var array
34+
*/
35+
private $errors = [];
36+
37+
/**
38+
* @var BuyRequestBuilder
39+
*/
40+
private $buyRequestBuilder;
41+
42+
/**
43+
* @var ProductRepositoryInterface
44+
*/
45+
private $productRepository;
46+
47+
/**
48+
* @var WishlistResourceModel
49+
*/
50+
private $wishlistResource;
51+
52+
/**
53+
* @param ProductRepositoryInterface $productRepository
54+
* @param BuyRequestBuilder $buyRequestBuilder
55+
* @param WishlistResourceModel $wishlistResource
56+
*/
57+
public function __construct(
58+
ProductRepositoryInterface $productRepository,
59+
BuyRequestBuilder $buyRequestBuilder,
60+
WishlistResourceModel $wishlistResource
61+
) {
62+
$this->productRepository = $productRepository;
63+
$this->buyRequestBuilder = $buyRequestBuilder;
64+
$this->wishlistResource = $wishlistResource;
65+
}
66+
67+
/**
68+
* Adding products to wishlist
69+
*
70+
* @param Wishlist $wishlist
71+
* @param array $wishlistItems
72+
*
73+
* @return WishlistOutput
74+
*
75+
* @throws AlreadyExistsException
76+
*/
77+
public function execute(Wishlist $wishlist, array $wishlistItems): WishlistOutput
78+
{
79+
foreach ($wishlistItems as $wishlistItem) {
80+
$this->addItemToWishlist($wishlist, $wishlistItem);
81+
}
82+
83+
$wishlistOutput = $this->prepareOutput($wishlist);
84+
85+
if ($wishlist->isObjectNew() || count($wishlistOutput->getErrors()) !== count($wishlistItems)) {
86+
$this->wishlistResource->save($wishlist);
87+
}
88+
89+
return $wishlistOutput;
90+
}
91+
92+
/**
93+
* Add product item to wishlist
94+
*
95+
* @param Wishlist $wishlist
96+
* @param WishlistItem $wishlistItem
97+
*
98+
* @return void
99+
*/
100+
private function addItemToWishlist(Wishlist $wishlist, WishlistItem $wishlistItem): void
101+
{
102+
$sku = $wishlistItem->getParentSku() ?? $wishlistItem->getSku();
103+
104+
try {
105+
$product = $this->productRepository->get($sku, false, null, true);
106+
} catch (NoSuchEntityException $e) {
107+
$this->addError(
108+
__('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(),
109+
self::ERROR_PRODUCT_NOT_FOUND
110+
);
111+
112+
return;
113+
}
114+
115+
try {
116+
$options = $this->buyRequestBuilder->build($wishlistItem, (int) $product->getId());
117+
$result = $wishlist->addNewItem($product, $options);
118+
119+
if (is_string($result)) {
120+
$this->addError($result);
121+
}
122+
} catch (LocalizedException $exception) {
123+
$this->addError($exception->getMessage());
124+
} catch (\Throwable $e) {
125+
$this->addError(
126+
__(
127+
'Could not add the product with SKU "%sku" to the wishlist:: %message',
128+
['sku' => $sku, 'message' => $e->getMessage()]
129+
)->render()
130+
);
131+
}
132+
}
133+
134+
/**
135+
* Add wishlist line item error
136+
*
137+
* @param string $message
138+
* @param string|null $code
139+
*
140+
* @return void
141+
*/
142+
private function addError(string $message, string $code = null): void
143+
{
144+
$this->errors[] = new Data\Error(
145+
$message,
146+
$code ?? self::ERROR_UNDEFINED
147+
);
148+
}
149+
150+
/**
151+
* Prepare output
152+
*
153+
* @param Wishlist $wishlist
154+
*
155+
* @return WishlistOutput
156+
*/
157+
private function prepareOutput(Wishlist $wishlist): WishlistOutput
158+
{
159+
$output = new WishlistOutput($wishlist, $this->errors);
160+
$this->errors = [];
161+
162+
return $output;
163+
}
164+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Wishlist\Model\Wishlist\BuyRequest;
9+
10+
use Magento\Wishlist\Model\Wishlist\Data\WishlistItem;
11+
12+
/**
13+
* Data provider for bundle product buy requests
14+
*/
15+
class BundleDataProvider implements BuyRequestDataProviderInterface
16+
{
17+
private const PROVIDER_OPTION_TYPE = 'bundle';
18+
19+
/**
20+
* @inheritdoc
21+
*
22+
* @phpcs:disable Magento2.Functions.DiscouragedFunction
23+
*/
24+
public function execute(WishlistItem $wishlistItem, ?int $productId): array
25+
{
26+
$bundleOptionsData = [];
27+
28+
foreach ($wishlistItem->getSelectedOptions() as $optionData) {
29+
$optionData = \explode('/', base64_decode($optionData->getId()));
30+
31+
if ($this->isProviderApplicable($optionData) === false) {
32+
continue;
33+
}
34+
35+
[, $optionId, $optionValueId, $optionQuantity] = $optionData;
36+
37+
$bundleOptionsData['bundle_option'][$optionId] = $optionValueId;
38+
$bundleOptionsData['bundle_option_qty'][$optionId] = $optionQuantity;
39+
}
40+
41+
return $bundleOptionsData;
42+
}
43+
44+
/**
45+
* Checks whether this provider is applicable for the current option
46+
*
47+
* @param array $optionData
48+
*
49+
* @return bool
50+
*/
51+
private function isProviderApplicable(array $optionData): bool
52+
{
53+
return $optionData[0] === self::PROVIDER_OPTION_TYPE;
54+
}
55+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Wishlist\Model\Wishlist\BuyRequest;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\Framework\DataObjectFactory;
12+
use Magento\Wishlist\Model\Wishlist\Data\WishlistItem;
13+
14+
/**
15+
* Building buy request for all product types
16+
*/
17+
class BuyRequestBuilder
18+
{
19+
/**
20+
* @var BuyRequestDataProviderInterface[]
21+
*/
22+
private $providers;
23+
24+
/**
25+
* @var DataObjectFactory
26+
*/
27+
private $dataObjectFactory;
28+
29+
/**
30+
* @param DataObjectFactory $dataObjectFactory
31+
* @param array $providers
32+
*/
33+
public function __construct(
34+
DataObjectFactory $dataObjectFactory,
35+
array $providers = []
36+
) {
37+
$this->dataObjectFactory = $dataObjectFactory;
38+
$this->providers = $providers;
39+
}
40+
41+
/**
42+
* Build product buy request for adding to wishlist
43+
*
44+
* @param WishlistItem $wishlistItemData
45+
* @param int|null $productId
46+
*
47+
* @return DataObject
48+
*/
49+
public function build(WishlistItem $wishlistItemData, ?int $productId = null): DataObject
50+
{
51+
$requestData = [
52+
[
53+
'qty' => $wishlistItemData->getQuantity(),
54+
]
55+
];
56+
57+
foreach ($this->providers as $provider) {
58+
$requestData[] = $provider->execute($wishlistItemData, $productId);
59+
}
60+
61+
return $this->dataObjectFactory->create(['data' => array_merge(...$requestData)]);
62+
}
63+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Wishlist\Model\Wishlist\BuyRequest;
9+
10+
use Magento\Wishlist\Model\Wishlist\Data\WishlistItem;
11+
12+
/**
13+
* Build buy request for adding products to wishlist
14+
*/
15+
interface BuyRequestDataProviderInterface
16+
{
17+
/**
18+
* Provide buy request data from add to wishlist item request
19+
*
20+
* @param WishlistItem $wishlistItemData
21+
* @param int|null $productId
22+
*
23+
* @return array
24+
*/
25+
public function execute(WishlistItem $wishlistItemData, ?int $productId): array;
26+
}

0 commit comments

Comments
 (0)