CustomAttributeMetadata.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\Framework\GraphQl\Schema\Type\ResolveInfo;
  9. use Magento\EavGraphQl\Model\Resolver\Query\Type;
  10. use Magento\Framework\Exception\InputException;
  11. use Magento\Framework\Exception\LocalizedException;
  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\ResolverInterface;
  16. /**
  17. * Resolve data for custom attribute metadata requests
  18. */
  19. class CustomAttributeMetadata implements ResolverInterface
  20. {
  21. /**
  22. * @var Type
  23. */
  24. private $type;
  25. /**
  26. * @param Type $type
  27. */
  28. public function __construct(Type $type)
  29. {
  30. $this->type = $type;
  31. }
  32. /**
  33. * @inheritdoc
  34. */
  35. public function resolve(
  36. Field $field,
  37. $context,
  38. ResolveInfo $info,
  39. array $value = null,
  40. array $args = null
  41. ) {
  42. $attributes['items'] = null;
  43. $attributeInputs = $args['attributes'];
  44. foreach ($attributeInputs as $attribute) {
  45. if (!isset($attribute['attribute_code']) || !isset($attribute['entity_type'])) {
  46. $attributes['items'][] = $this->createInputException($attribute);
  47. continue;
  48. }
  49. try {
  50. $type = $this->type->getType($attribute['attribute_code'], $attribute['entity_type']);
  51. } catch (InputException $exception) {
  52. $attributes['items'][] = new GraphQlNoSuchEntityException(
  53. __(
  54. 'Attribute code %1 of entity type %2 not configured to have a type.',
  55. [$attribute['attribute_code'], $attribute['entity_type']]
  56. )
  57. );
  58. continue;
  59. } catch (LocalizedException $exception) {
  60. $attributes['items'][] = new GraphQlInputException(
  61. __(
  62. 'Invalid entity_type specified: %1',
  63. [$attribute['entity_type']]
  64. )
  65. );
  66. continue;
  67. }
  68. if (empty($type)) {
  69. continue;
  70. }
  71. $attributes['items'][] = [
  72. 'attribute_code' => $attribute['attribute_code'],
  73. 'entity_type' => $attribute['entity_type'],
  74. 'attribute_type' => ucfirst($type)
  75. ];
  76. }
  77. return $attributes;
  78. }
  79. /**
  80. * Create GraphQL input exception for an invalid attribute input
  81. *
  82. * @param array $attribute
  83. * @return GraphQlInputException
  84. */
  85. private function createInputException(array $attribute) : GraphQlInputException
  86. {
  87. $isCodeSet = isset($attribute['attribute_code']);
  88. $isEntitySet = isset($attribute['entity_type']);
  89. $messagePart = !$isCodeSet ? 'attribute_code' : 'entity_type';
  90. $messagePart .= !$isCodeSet && !$isEntitySet ? '/entity_type' : '';
  91. $identifier = "Empty AttributeInput";
  92. if ($isCodeSet) {
  93. $identifier = 'attribute_code: ' . $attribute['attribute_code'];
  94. } elseif ($isEntitySet) {
  95. $identifier = 'entity_type: ' . $attribute['entity_type'];
  96. }
  97. return new GraphQlInputException(
  98. __(
  99. 'Missing %1 for the input %2.',
  100. [$messagePart, $identifier]
  101. )
  102. );
  103. }
  104. }