2
2
declare (strict_types=1 );
3
3
namespace Cloudinary \Cloudinary \Model \GraphQLResolver ;
4
4
5
+ use Cloudinary \Cloudinary \Model \Configuration ;
6
+ use Magento \Framework \Exception \LocalizedException ;
5
7
use Magento \Framework \GraphQl \Config \Element \Field ;
6
8
use Magento \Framework \GraphQl \Query \ResolverInterface ;
7
9
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
8
10
use Cloudinary \Cloudinary \Model \Api \ProductGalleryManagement ;
9
11
use Magento \Framework \GraphQl \Type \Definition \ObjectType ;
10
-
12
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
13
+ use Magento \Store \Model \StoreManagerInterface ;
14
+ use Magento \Framework \UrlInterface ;
11
15
/**
12
16
* Class ProductAttributeCldResolver
13
17
**/
@@ -18,25 +22,78 @@ class ProductAttributeCldResolver implements ResolverInterface
18
22
*/
19
23
private $ productGalleryManagement ;
20
24
25
+ protected $ productRepository ;
26
+
27
+ private $ urlBuilder ;
28
+ private $ storeManager ;
29
+
30
+ protected $ _sku ;
31
+
32
+ protected $ _configuration ;
33
+
21
34
/**
22
35
* ProductAttributeCldResolver constructor.
23
36
* @param ProductGalleryManagement $productGalleryManagement
24
37
*/
25
38
public function __construct (
26
- ProductGalleryManagement $ productGalleryManagement
39
+ ProductGalleryManagement $ productGalleryManagement ,
40
+ ProductRepositoryInterface $ productRepository ,
41
+ Configuration $ configuration ,
42
+ UrlInterface $ urlBuilder ,
43
+ StoreManagerInterface $ storeManager ,
27
44
) {
28
45
$ this ->productGalleryManagement = $ productGalleryManagement ;
46
+ $ this ->productRepository = $ productRepository ;
47
+ $ this ->urlBuilder = $ urlBuilder ;
48
+ $ this ->storeManager = $ storeManager ;
49
+ $ this ->_configuration = $ configuration ;
29
50
}
30
51
31
52
/**
32
53
* @inheritdoc
33
54
*/
34
55
public function resolve (Field $ field , $ context , ResolveInfo $ info , array $ value = null , array $ args = null )
35
56
{
36
- $ productId = $ value ['sku ' ];
37
- $ productMediaStr = $ this ->productGalleryManagement ->getProductMedia ($ productId );
57
+ $ sku = $ value ['sku ' ] ?? null ;
58
+ if ($ sku ) {
59
+ $ this ->_sku = $ sku ;
60
+ }
61
+
62
+ $ attributeCodes = $ args ['attribute_codes ' ] ?? null ;
63
+ if ($ attributeCodes && is_array ($ attributeCodes )) {
64
+ $ this ->checkEnabled ();
65
+ $ product = $ this ->productRepository ->get ($ this ->_sku );
66
+ $ mediaAttributes = [];
67
+ foreach ($ attributeCodes as $ attributeCode ) {
68
+ $ attrValue = $ product ->getData ($ attributeCode );
69
+ if ($ attrValue ) {
70
+ foreach ($ product ->getMediaGalleryImages () as $ gallItem ) {
71
+ if ($ attrValue == $ gallItem ->getFile ()) {
72
+ $ mediaAttributes [] = [
73
+ 'attribute_code ' => $ attributeCode ,
74
+ 'url ' => $ gallItem ->getUrl ()
75
+ ];
76
+ }
77
+ }
78
+
79
+ }
80
+ }
81
+ return $ mediaAttributes ;
82
+ }
83
+ $ productMediaStr = $ this ->productGalleryManagement ->getProductMedia ($ this ->_sku );
38
84
$ jsonDecoder = new \Magento \Framework \Serialize \Serializer \Json ();
39
85
$ productMedia = $ jsonDecoder ->unserialize ($ productMediaStr );
86
+
40
87
return $ productMedia ['data ' ];
41
88
}
42
- }
89
+
90
+
91
+ private function checkEnabled () {
92
+ if (!$ this ->_configuration ->isEnabled ()) {
93
+ throw new LocalizedException (
94
+ __ ("Cloudinary module is disabled. Please enable it first in order to use this API. " )
95
+ );
96
+ }
97
+ return $ this ;
98
+ }
99
+ }
0 commit comments