8
8
namespace Magento \QuoteGraphQl \Plugin ;
9
9
10
10
use Magento \Catalog \Model \ResourceModel \Product \Attribute \CollectionFactory as AttributeCollectionFactory ;
11
+ use Magento \Eav \Model \Validator \Attribute \Code ;
12
+ use Magento \Framework \App \ObjectManager ;
11
13
use Magento \Framework \GraphQl \Query \Fields ;
14
+ use Magento \Framework \Validator \ValidateException ;
12
15
use Magento \Quote \Model \Quote \Config as QuoteConfig ;
13
16
14
17
/**
@@ -26,16 +29,44 @@ class ProductAttributesExtender
26
29
*/
27
30
private $ attributeCollectionFactory ;
28
31
32
+ /**
33
+ * @var Code
34
+ */
35
+ private Code $ attributeCodeValidator ;
36
+
29
37
/**
30
38
* @param Fields $fields
31
39
* @param AttributeCollectionFactory $attributeCollectionFactory
40
+ * @param Code|null $attributeCodeValidator
32
41
*/
33
42
public function __construct (
34
43
Fields $ fields ,
35
- AttributeCollectionFactory $ attributeCollectionFactory
44
+ AttributeCollectionFactory $ attributeCollectionFactory ,
45
+ Code $ attributeCodeValidator = null
36
46
) {
37
47
$ this ->fields = $ fields ;
38
48
$ this ->attributeCollectionFactory = $ attributeCollectionFactory ;
49
+ $ this ->attributeCodeValidator = $ attributeCodeValidator ?? ObjectManager::getInstance ()->get (Code::class);
50
+ }
51
+
52
+ /**
53
+ * Get only attribute code that pass validation
54
+ *
55
+ * @return array
56
+ */
57
+ private function getValidatedAttributeCodes (): array
58
+ {
59
+ return array_filter ($ this ->fields ->getFieldsUsedInQuery (), [$ this ,'validateAttributeCode ' ]);
60
+ }
61
+
62
+ /**
63
+ * @param string|int $code
64
+ * @return bool
65
+ * @throws ValidateException
66
+ */
67
+ private function validateAttributeCode (string |int $ code )
68
+ {
69
+ return $ this ->attributeCodeValidator ->isValid ((string )$ code );
39
70
}
40
71
41
72
/**
@@ -51,7 +82,7 @@ public function afterGetProductAttributes(QuoteConfig $subject, array $result):
51
82
$ attributeCollection = $ this ->attributeCollectionFactory ->create ()
52
83
->removeAllFieldsFromSelect ()
53
84
->addFieldToSelect ('attribute_code ' )
54
- ->setCodeFilter ($ this ->fields -> getFieldsUsedInQuery ())
85
+ ->setCodeFilter ($ this ->getValidatedAttributeCodes ())
55
86
->load ();
56
87
$ attributes = $ attributeCollection ->getColumnValues ('attribute_code ' );
57
88
0 commit comments