|
| 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\EavGraphQl\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\EavGraphQl\Model\Resolver\DataProvider\AttributeOptions as AttributeOptionsDataProvider; |
| 11 | +use Magento\Framework\Exception\InputException; |
| 12 | +use Magento\Framework\Exception\StateException; |
| 13 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 14 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 15 | +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; |
| 16 | +use Magento\Framework\GraphQl\Query\Resolver\Value; |
| 17 | +use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; |
| 18 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 19 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 20 | + |
| 21 | +/** |
| 22 | + * Resolve attribute options data for custom attribute. |
| 23 | + */ |
| 24 | +class AttributeOptions implements ResolverInterface |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var AttributeOptionsDataProvider |
| 28 | + */ |
| 29 | + private $attributeOptionsDataProvider; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var AttributeOptions |
| 33 | + */ |
| 34 | + private $valueFactory; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param AttributeOptionsDataProvider $attributeOptionsDataProvider |
| 38 | + * @param ValueFactory $valueFactory |
| 39 | + */ |
| 40 | + public function __construct( |
| 41 | + AttributeOptionsDataProvider $attributeOptionsDataProvider, |
| 42 | + ValueFactory $valueFactory |
| 43 | + ) { |
| 44 | + $this->attributeOptionsDataProvider = $attributeOptionsDataProvider; |
| 45 | + $this->valueFactory = $valueFactory; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @inheritDoc |
| 50 | + */ |
| 51 | + public function resolve( |
| 52 | + Field $field, |
| 53 | + $context, |
| 54 | + ResolveInfo $info, |
| 55 | + array $value = null, |
| 56 | + array $args = null |
| 57 | + ) : Value { |
| 58 | + |
| 59 | + return $this->valueFactory->create(function () use ($value) { |
| 60 | + $entityType = $this->getEntityType($value); |
| 61 | + $attributeCode = $this->getAttributeCode($value); |
| 62 | + |
| 63 | + $optionsData = $this->getAttributeOptionsData($entityType, $attributeCode); |
| 64 | + return $optionsData; |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param array $value |
| 70 | + * @return int |
| 71 | + * @throws GraphQlInputException |
| 72 | + */ |
| 73 | + private function getEntityType(array $value): int |
| 74 | + { |
| 75 | + if (!isset($value['entity_type'])) { |
| 76 | + throw new GraphQlInputException(__('"Entity type should be specified')); |
| 77 | + } |
| 78 | + |
| 79 | + return (int)$value['entity_type']; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param array $value |
| 84 | + * @return string |
| 85 | + * @throws GraphQlInputException |
| 86 | + */ |
| 87 | + private function getAttributeCode(array $value): string |
| 88 | + { |
| 89 | + if (!isset($value['attribute_code'])) { |
| 90 | + throw new GraphQlInputException(__('"Attribute code should be specified')); |
| 91 | + } |
| 92 | + |
| 93 | + return $value['attribute_code']; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param int $entityType |
| 98 | + * @param string $attributeCode |
| 99 | + * @return array |
| 100 | + * @throws GraphQlInputException |
| 101 | + * @throws GraphQlNoSuchEntityException |
| 102 | + */ |
| 103 | + private function getAttributeOptionsData(int $entityType, string $attributeCode): array |
| 104 | + { |
| 105 | + try { |
| 106 | + $optionsData = $this->attributeOptionsDataProvider->getData($entityType, $attributeCode); |
| 107 | + } catch (InputException $e) { |
| 108 | + throw new GraphQlInputException(__($e->getMessage()), $e); |
| 109 | + } catch (StateException $e) { |
| 110 | + throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); |
| 111 | + } |
| 112 | + return $optionsData; |
| 113 | + } |
| 114 | +} |
0 commit comments