7
7
8
8
namespace Magento \EavGraphQl \Model \Resolver ;
9
9
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 ;
11
13
use Magento \Framework \GraphQl \Config \Element \Field ;
14
+ use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
15
+ use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
12
16
use Magento \Framework \GraphQl \Query \Resolver \Value ;
13
17
use Magento \Framework \GraphQl \Query \Resolver \ValueFactory ;
14
18
use Magento \Framework \GraphQl \Query \ResolverInterface ;
20
24
class AttributeOptions implements ResolverInterface
21
25
{
22
26
/**
23
- * @var AttributeOptionManagementInterface
27
+ * @var AttributeOptionsDataProvider
24
28
*/
25
- protected $ optionManager ;
29
+ private $ attributeOptionsDataProvider ;
26
30
27
31
/**
28
- * @var ValueFactory
32
+ * @var AttributeOptions
29
33
*/
30
- protected $ valueFactory ;
34
+ private $ valueFactory ;
31
35
32
36
/**
33
- * AttributeOptions constructor.
34
- *
35
- * @param AttributeOptionManagementInterface $optionManager
37
+ * @param AttributeOptionsDataProvider $attributeOptionsDataProvider
36
38
* @param ValueFactory $valueFactory
37
39
*/
38
40
public function __construct (
39
- AttributeOptionManagementInterface $ optionManager ,
41
+ AttributeOptionsDataProvider $ attributeOptionsDataProvider ,
40
42
ValueFactory $ valueFactory
41
43
) {
42
- $ this ->optionManager = $ optionManager ;
44
+ $ this ->attributeOptionsDataProvider = $ attributeOptionsDataProvider ;
43
45
$ this ->valueFactory = $ valueFactory ;
44
46
}
45
47
46
48
/**
47
- * { @inheritDoc}
49
+ * @inheritDoc
48
50
*/
49
51
public function resolve (
50
52
Field $ field ,
@@ -53,36 +55,60 @@ public function resolve(
53
55
array $ value = null ,
54
56
array $ args = null
55
57
) : Value {
56
- $ options = [];
57
58
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 );
60
62
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 ' ));
66
77
}
67
78
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
+ }
74
81
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 ' ));
80
91
}
81
92
82
- $ result = function () use ($ options ) {
83
- return $ options ;
84
- };
93
+ return $ value ['attribute_code ' ];
94
+ }
85
95
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 ;
87
113
}
88
114
}
0 commit comments