Skip to content

Commit f4e2665

Browse files
committed
Implementing the ID_V2 for entered options, bundle and downloadable products
1 parent 3140823 commit f4e2665

File tree

7 files changed

+208
-28
lines changed

7 files changed

+208
-28
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\BundleGraphQl\Model\Resolver\Options;
9+
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
16+
/**
17+
* Format new option id_v2 in base64 encode for entered bundle options
18+
*/
19+
class BundleEnteredOptionValueIdV2 implements ResolverInterface
20+
{
21+
/**
22+
* Option type name
23+
*/
24+
private const OPTION_TYPE = 'bundle';
25+
26+
/**
27+
* Create a option id_v2 for entered option in "<option-type>/<option-id>/<option-value-id>/<quantity>" format
28+
*
29+
* @param Field $field
30+
* @param ContextInterface $context
31+
* @param ResolveInfo $info
32+
* @param array|null $value
33+
* @param array|null $args
34+
*
35+
* @return string
36+
*
37+
* @throws GraphQlInputException
38+
*
39+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
40+
*/
41+
public function resolve(
42+
Field $field,
43+
$context,
44+
ResolveInfo $info,
45+
array $value = null,
46+
array $args = null
47+
) {
48+
if (!isset($value['option_id']) || empty($value['option_id'])) {
49+
throw new GraphQlInputException(__('Wrong format option data: option_id should not be empty.'));
50+
}
51+
52+
if (!isset($value['selection_id']) || empty($value['selection_id'])) {
53+
throw new GraphQlInputException(__('Wrong format option data: selection_id should not be empty.'));
54+
}
55+
56+
$optionDetails = [
57+
self::OPTION_TYPE,
58+
$value['option_id'],
59+
$value['selection_id'],
60+
(int) $value['selection_qty']
61+
];
62+
63+
$content = implode('/', $optionDetails);
64+
65+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
66+
return base64_encode($content);
67+
}
68+
}

app/code/Magento/BundleGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type BundleItemOption @doc(description: "BundleItemOption defines characteristic
6666
price_type: PriceTypeEnum @doc(description: "One of FIXED, PERCENT, or DYNAMIC.")
6767
can_change_quantity: Boolean @doc(description: "Indicates whether the customer can change the number of items for this option.")
6868
product: ProductInterface @doc(description: "Contains details about this product option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product")
69+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\BundleGraphQl\\Model\\Resolver\\Options\\BundleEnteredOptionValueIdV2")
6970
}
7071

7172
type BundleProduct implements ProductInterface, PhysicalProductInterface, CustomizableProductInterface @doc(description: "BundleProduct defines basic features of a bundle product and contains multiple BundleItems.") {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\CatalogGraphQl\Model\Resolver\Product;
9+
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
16+
/**
17+
* Format new option id_v2 in base64 encode for entered custom options
18+
*/
19+
class CustomizableEnteredOptionValueIdV2 implements ResolverInterface
20+
{
21+
/**
22+
* Option type name
23+
*/
24+
private const OPTION_TYPE = 'custom-option';
25+
26+
/**
27+
* Create a option id_v2 for entered option in "<option-type>/<option-id>" format
28+
*
29+
* @param Field $field
30+
* @param ContextInterface $context
31+
* @param ResolveInfo $info
32+
* @param array|null $value
33+
* @param array|null $args
34+
*
35+
* @return string
36+
*
37+
* @throws GraphQlInputException
38+
*
39+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
40+
*/
41+
public function resolve(
42+
Field $field,
43+
$context,
44+
ResolveInfo $info,
45+
array $value = null,
46+
array $args = null
47+
) {
48+
if (!isset($value['option_id']) || empty($value['option_id'])) {
49+
throw new GraphQlInputException(__('Wrong format option data: option_id should not be empty.'));
50+
}
51+
52+
$optionDetails = [
53+
self::OPTION_TYPE,
54+
$value['option_id']
55+
];
56+
57+
$content = implode('/', $optionDetails);
58+
59+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
60+
return base64_encode($content);
61+
}
62+
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/CustomizableOptionValueIdV2.php renamed to app/code/Magento/CatalogGraphQl/Model/Resolver/Product/CustomizableSelectedOptionValueIdV2.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,36 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver\Product;
99

10-
use Magento\Framework\Exception\LocalizedException;
1110
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1212
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
13-
use Magento\Framework\GraphQl\Query\Resolver\Value;
1413
use Magento\Framework\GraphQl\Query\ResolverInterface;
1514
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1615

1716
/**
18-
* @inheritdoc
19-
*
20-
* Format new option id_v2 in base64 encode for custom options
17+
* Format new option id_v2 in base64 encode for selected custom options
2118
*/
22-
class CustomizableOptionValueIdV2 implements ResolverInterface
19+
class CustomizableSelectedOptionValueIdV2 implements ResolverInterface
2320
{
21+
/**
22+
* Option type name
23+
*/
2424
private const OPTION_TYPE = 'custom-option';
2525

2626
/**
27-
* @inheritdoc
28-
*
29-
* Create new option id_v2 that encodes details for each option and in most cases can be presented
30-
* as base64("<option-type>/<option-id>/<option-value-id>")
27+
* Create a option id_v2 for selected option in "<option-type>/<option-id>/<option-value-id>" format
3128
*
3229
* @param Field $field
3330
* @param ContextInterface $context
3431
* @param ResolveInfo $info
3532
* @param array|null $value
3633
* @param array|null $args
37-
* @return Value|mixed|void
34+
*
35+
* @return string
36+
*
37+
* @throws GraphQlInputException
38+
*
39+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3840
*/
3941
public function resolve(
4042
Field $field,
@@ -43,23 +45,23 @@ public function resolve(
4345
array $value = null,
4446
array $args = null
4547
) {
48+
if (!isset($value['option_id']) || empty($value['option_id'])) {
49+
throw new GraphQlInputException(__('Wrong format option data: option_id should not be empty.'));
50+
}
51+
52+
if (!isset($value['option_type_id']) || empty($value['option_type_id'])) {
53+
throw new GraphQlInputException(__('Wrong format option data: option_type_id should not be empty.'));
54+
}
55+
4656
$optionDetails = [
4757
self::OPTION_TYPE,
4858
$value['option_id'],
4959
$value['option_type_id']
5060
];
5161

52-
if (!isset($value['option_id']) || empty($value['option_id'])) {
53-
throw new LocalizedException(__('Wrong format option data: option_id should not be empty.'));
54-
}
55-
56-
if (!isset($value['option_type_id']) || empty($value['option_type_id'])) {
57-
throw new LocalizedException(__('Wrong format option data: option_type_id should not be empty.'));
58-
}
62+
$content = implode('/', $optionDetails);
5963

6064
// phpcs:ignore Magento2.Functions.DiscouragedFunction
61-
$content = \implode('/', $optionDetails);
62-
6365
return base64_encode($content);
6466
}
6567
}

app/code/Magento/CatalogGraphQl/etc/schema.graphqls

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ type CustomizableAreaValue @doc(description: "CustomizableAreaValue defines the
132132
price_type: PriceTypeEnum @doc(description: "FIXED, PERCENT, or DYNAMIC.")
133133
sku: String @doc(description: "The Stock Keeping Unit for this option.")
134134
max_characters: Int @doc(description: "The maximum number of characters that can be entered for this customizable option.")
135-
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
135+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableEnteredOptionValueIdV2")
136136
}
137137

138138
type CategoryTree implements CategoryInterface @doc(description: "Category Tree implementation.") {
@@ -154,7 +154,7 @@ type CustomizableDateValue @doc(description: "CustomizableDateValue defines the
154154
price: Float @doc(description: "The price assigned to this option.")
155155
price_type: PriceTypeEnum @doc(description: "FIXED, PERCENT, or DYNAMIC.")
156156
sku: String @doc(description: "The Stock Keeping Unit for this option.")
157-
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
157+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableEnteredOptionValueIdV2")
158158
}
159159

160160
type CustomizableDropDownOption implements CustomizableOptionInterface @doc(description: "CustomizableDropDownOption contains information about a drop down menu that is defined as part of a customizable option.") {
@@ -168,7 +168,7 @@ type CustomizableDropDownValue @doc(description: "CustomizableDropDownValue defi
168168
sku: String @doc(description: "The Stock Keeping Unit for this option.")
169169
title: String @doc(description: "The display name for this option.")
170170
sort_order: Int @doc(description: "The order in which the option is displayed.")
171-
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
171+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableSelectedOptionValueIdV2")
172172
}
173173

174174
type CustomizableMultipleOption implements CustomizableOptionInterface @doc(description: "CustomizableMultipleOption contains information about a multiselect that is defined as part of a customizable option.") {
@@ -182,7 +182,7 @@ type CustomizableMultipleValue @doc(description: "CustomizableMultipleValue defi
182182
sku: String @doc(description: "The Stock Keeping Unit for this option.")
183183
title: String @doc(description: "The display name for this option.")
184184
sort_order: Int @doc(description: "The order in which the option is displayed.")
185-
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
185+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableSelectedOptionValueIdV2")
186186
}
187187

188188
type CustomizableFieldOption implements CustomizableOptionInterface @doc(description: "CustomizableFieldOption contains information about a text field that is defined as part of a customizable option.") {
@@ -195,7 +195,7 @@ type CustomizableFieldValue @doc(description: "CustomizableFieldValue defines th
195195
price_type: PriceTypeEnum @doc(description: "FIXED, PERCENT, or DYNAMIC.")
196196
sku: String @doc(description: "The Stock Keeping Unit for this option.")
197197
max_characters: Int @doc(description: "The maximum number of characters that can be entered for this customizable option.")
198-
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
198+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableEnteredOptionValueIdV2")
199199
}
200200

201201
type CustomizableFileOption implements CustomizableOptionInterface @doc(description: "CustomizableFileOption contains information about a file picker that is defined as part of a customizable option.") {
@@ -210,7 +210,7 @@ type CustomizableFileValue @doc(description: "CustomizableFileValue defines the
210210
file_extension: String @doc(description: "The file extension to accept.")
211211
image_size_x: Int @doc(description: "The maximum width of an image.")
212212
image_size_y: Int @doc(description: "The maximum height of an image.")
213-
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
213+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableEnteredOptionValueIdV2")
214214
}
215215

216216
interface MediaGalleryInterface @doc(description: "Contains basic information about a product image or video.") @typeResolver(class: "Magento\\CatalogGraphQl\\Model\\MediaGalleryTypeResolver") {
@@ -280,7 +280,7 @@ type CustomizableRadioValue @doc(description: "CustomizableRadioValue defines th
280280
sku: String @doc(description: "The Stock Keeping Unit for this option.")
281281
title: String @doc(description: "The display name for this option.")
282282
sort_order: Int @doc(description: "The order in which the radio button is displayed.")
283-
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
283+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableSelectedOptionValueIdV2")
284284
}
285285

286286
type CustomizableCheckboxOption implements CustomizableOptionInterface @doc(description: "CustomizableCheckbbixOption contains information about a set of checkbox values that are defined as part of a customizable option.") {
@@ -294,7 +294,7 @@ type CustomizableCheckboxValue @doc(description: "CustomizableCheckboxValue defi
294294
sku: String @doc(description: "The Stock Keeping Unit for this option.")
295295
title: String @doc(description: "The display name for this option.")
296296
sort_order: Int @doc(description: "The order in which the checkbox value is displayed.")
297-
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
297+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableSelectedOptionValueIdV2")
298298
}
299299

300300
type VirtualProduct implements ProductInterface, CustomizableProductInterface @doc(description: "A virtual product is non-tangible product that does not require shipping and is not kept in inventory.") {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\DownloadableGraphQl\Resolver\Product;
9+
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
15+
/**
16+
* Formatting the id_v2 for downloadable link
17+
*/
18+
class DownloadableLinksValueIdV2 implements ResolverInterface
19+
{
20+
private const OPTION_TYPE = 'downloadable';
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function resolve(
26+
Field $field,
27+
$context,
28+
ResolveInfo $info,
29+
array $value = null,
30+
array $args = null
31+
) {
32+
if (!isset($value['id']) || empty($value['id'])) {
33+
throw new GraphQlInputException(__('Wrong format option data: `id` should not be empty.'));
34+
}
35+
36+
$optionDetails = [
37+
self::OPTION_TYPE,
38+
$value['id']
39+
];
40+
41+
$content = implode('/', $optionDetails);
42+
43+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
44+
return base64_encode($content);
45+
}
46+
}

app/code/Magento/DownloadableGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type DownloadableProductLinks @doc(description: "DownloadableProductLinks define
5353
link_type: DownloadableFileTypeEnum @deprecated(reason: "`sample_url` serves to get the downloadable sample")
5454
sample_type: DownloadableFileTypeEnum @deprecated(reason: "`sample_url` serves to get the downloadable sample")
5555
sample_file: String @deprecated(reason: "`sample_url` serves to get the downloadable sample")
56+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\DownloadableGraphQl\\Resolver\\Product\\DownloadableLinksValueIdV2")
5657
}
5758

5859
type DownloadableProductSamples @doc(description: "DownloadableProductSamples defines characteristics of a downloadable product") {

0 commit comments

Comments
 (0)