AttributeOptions.php 3.5 KB

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