forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
magento#257: create new id_v2 option
- Loading branch information
m.mezhensky
committed
May 13, 2020
1 parent
9fa16c7
commit 3140823
Showing
4 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
app/code/Magento/CatalogGraphQl/Model/Resolver/Product/CustomizableOptionValueIdV2.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,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); | ||
} | ||
} |
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
83 changes: 83 additions & 0 deletions
83
...onfigurableProductGraphQl/Model/Resolver/Variant/Attributes/ConfigurableAttributeIdV2.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,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); | ||
} | ||
} |
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