Skip to content

Commit

Permalink
magento#257: create new id_v2 option
Browse files Browse the repository at this point in the history
  • Loading branch information
mmezhensky committed May 13, 2020
1 parent 9fa16c7 commit 3140823
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Product;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* @inheritdoc
*
* Format new option id_v2 in base64 encode for custom options
*/
class CustomizableOptionValueIdV2 implements ResolverInterface
{
private const OPTION_TYPE = 'custom-option';

/**
* @inheritdoc
*
* Create new option id_v2 that encodes details for each option and in most cases can be presented
* as base64("<option-type>/<option-id>/<option-value-id>")
*
* @param Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return Value|mixed|void
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$optionDetails = [
self::OPTION_TYPE,
$value['option_id'],
$value['option_type_id']
];

if (!isset($value['option_id']) || empty($value['option_id'])) {
throw new LocalizedException(__('Wrong format option data: option_id should not be empty.'));
}

if (!isset($value['option_type_id']) || empty($value['option_type_id'])) {
throw new LocalizedException(__('Wrong format option data: option_type_id should not be empty.'));
}

// phpcs:ignore Magento2.Functions.DiscouragedFunction
$content = \implode('/', $optionDetails);

return base64_encode($content);
}
}
8 changes: 8 additions & 0 deletions app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ type CustomizableAreaValue @doc(description: "CustomizableAreaValue defines the
price_type: PriceTypeEnum @doc(description: "FIXED, PERCENT, or DYNAMIC.")
sku: String @doc(description: "The Stock Keeping Unit for this option.")
max_characters: Int @doc(description: "The maximum number of characters that can be entered for this customizable option.")
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
}

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

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

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

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

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

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

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

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.") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ConfigurableProductGraphQl\Model\Resolver\Variant\Attributes;

use Magento\Eav\Model\ResourceModel\Entity\Attribute;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* @inheritdoc
*
* Format new option id_v2 in base64 encode for super attribute options
*/
class ConfigurableAttributeIdV2 implements ResolverInterface
{
private const OPTION_TYPE = 'configurable';

/**
* @var Attribute
*/
private $eavAttribute;

/**
* ConfigurableAttributeIdV2 constructor.
*
* @param Attribute $eavAttribute
*/
public function __construct(Attribute $eavAttribute)
{
$this->eavAttribute = $eavAttribute;
}

/**
* @inheritdoc
*
* Create new option id_v2 that encodes details for each option and in most cases can be presented
* as base64("<option-type>/<attribute-id>/<value-index>")
*
* @param Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return Value|mixed|string
* @throws LocalizedException
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$attribute_id = $this->eavAttribute->getIdByCode('catalog_product', $value['code']);
$optionDetails = [
self::OPTION_TYPE,
$attribute_id,
$value['value_index']
];

if (empty($attribute_id)) {
throw new LocalizedException(__('Wrong format option data: attribute_id should not be empty.'));
}

if (!isset($value['value_index']) || empty($value['value_index'])) {
throw new LocalizedException(__('Wrong format option data: value_index should not be empty.'));
}

// phpcs:ignore Magento2.Functions.DiscouragedFunction
$content = \implode('/', $optionDetails);

return base64_encode($content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ConfigurableAttributeOption @doc(description: "ConfigurableAttributeOption
label: String @doc(description: "A string that describes the configurable attribute option")
code: String @doc(description: "The ID assigned to the attribute")
value_index: Int @doc(description: "A unique index number assigned to the configurable product option")
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\ConfigurableProductGraphQl\\Model\\Resolver\\Variant\\Attributes\\ConfigurableAttributeIdV2")
}

type ConfigurableProductOptions @doc(description: "ConfigurableProductOptions defines configurable attributes for the specified product") {
Expand Down

0 comments on commit 3140823

Please sign in to comment.