77
88namespace Magento \EavGraphQl \Model \Resolver ;
99
10- use Magento \Eav \Api \AttributeOptionManagementInterface ;
10+ use Magento \EavGraphQl \Model \Resolver \DataProvider \AttributeOptions as AttributeOptionsDataProvider ;
11+ use Magento \Framework \Exception \InputException ;
12+ use Magento \Framework \Exception \StateException ;
1113use Magento \Framework \GraphQl \Config \Element \Field ;
14+ use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
15+ use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
1216use Magento \Framework \GraphQl \Query \Resolver \Value ;
1317use Magento \Framework \GraphQl \Query \Resolver \ValueFactory ;
1418use Magento \Framework \GraphQl \Query \ResolverInterface ;
2024class AttributeOptions implements ResolverInterface
2125{
2226 /**
23- * @var AttributeOptionManagementInterface
27+ * @var AttributeOptionsDataProvider
2428 */
25- protected $ optionManager ;
29+ private $ attributeOptionsDataProvider ;
2630
2731 /**
28- * @var ValueFactory
32+ * @var AttributeOptions
2933 */
30- protected $ valueFactory ;
34+ private $ valueFactory ;
3135
3236 /**
33- * AttributeOptions constructor.
34- *
35- * @param AttributeOptionManagementInterface $optionManager
37+ * @param AttributeOptionsDataProvider $attributeOptionsDataProvider
3638 * @param ValueFactory $valueFactory
3739 */
3840 public function __construct (
39- AttributeOptionManagementInterface $ optionManager ,
41+ AttributeOptionsDataProvider $ attributeOptionsDataProvider ,
4042 ValueFactory $ valueFactory
4143 ) {
42- $ this ->optionManager = $ optionManager ;
44+ $ this ->attributeOptionsDataProvider = $ attributeOptionsDataProvider ;
4345 $ this ->valueFactory = $ valueFactory ;
4446 }
4547
4648 /**
47- * { @inheritDoc}
49+ * @inheritDoc
4850 */
4951 public function resolve (
5052 Field $ field ,
@@ -53,36 +55,60 @@ public function resolve(
5355 array $ value = null ,
5456 array $ args = null
5557 ) : Value {
56- $ options = [];
5758
58- $ entityType = !empty ($ value ['entity_type ' ]) ? $ value ['entity_type ' ] : '' ;
59- $ attributeCode = !empty ($ value ['attribute_code ' ]) ? $ value ['attribute_code ' ] : '' ;
59+ return $ this ->valueFactory ->create (function () use ($ value ) {
60+ $ entityType = $ this ->getEntityType ($ value );
61+ $ attributeCode = $ this ->getAttributeCode ($ value );
6062
61- try {
62- /** @var \Magento\Eav\Api\Data\AttributeOptionInterface[] $attributeOptions */
63- $ attributeOptions = $ this ->optionManager ->getItems ($ entityType , $ attributeCode );
64- } catch (\Exception $ e ) {
65- $ attributeOptions = [];
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 ' ));
6677 }
6778
68- if (is_array ($ attributeOptions )) {
69- /** @var \Magento\Eav\Api\Data\AttributeOptionInterface $option */
70- foreach ($ attributeOptions as $ option ) {
71- if ($ option ->getValue () === '' ) {
72- continue ;
73- }
79+ return (int )$ value ['entity_type ' ];
80+ }
7481
75- $ options [] = [
76- 'label ' => $ option ->getLabel (),
77- 'value ' => $ option ->getValue ()
78- ];
79- }
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 ' ));
8091 }
8192
82- $ result = function () use ($ options ) {
83- return $ options ;
84- };
93+ return $ value ['attribute_code ' ];
94+ }
8595
86- return $ this ->valueFactory ->create ($ result );
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 ;
87113 }
88114}
0 commit comments