22 declare (strict_types=1 );
33 namespace Cloudinary \Cloudinary \Model \GraphQLResolver ;
44
5+ use Cloudinary \Cloudinary \Model \Configuration ;
6+ use Magento \Framework \Exception \LocalizedException ;
57 use Magento \Framework \GraphQl \Config \Element \Field ;
68 use Magento \Framework \GraphQl \Query \ResolverInterface ;
79 use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
810 use Cloudinary \Cloudinary \Model \Api \ProductGalleryManagement ;
911 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 ;
1115 /**
1216 * Class ProductAttributeCldResolver
1317 **/
@@ -18,25 +22,78 @@ class ProductAttributeCldResolver implements ResolverInterface
1822 */
1923 private $ productGalleryManagement ;
2024
25+ protected $ productRepository ;
26+
27+ private $ urlBuilder ;
28+ private $ storeManager ;
29+
30+ protected $ _sku ;
31+
32+ protected $ _configuration ;
33+
2134 /**
2235 * ProductAttributeCldResolver constructor.
2336 * @param ProductGalleryManagement $productGalleryManagement
2437 */
2538 public function __construct (
26- ProductGalleryManagement $ productGalleryManagement
39+ ProductGalleryManagement $ productGalleryManagement ,
40+ ProductRepositoryInterface $ productRepository ,
41+ Configuration $ configuration ,
42+ UrlInterface $ urlBuilder ,
43+ StoreManagerInterface $ storeManager ,
2744 ) {
2845 $ this ->productGalleryManagement = $ productGalleryManagement ;
46+ $ this ->productRepository = $ productRepository ;
47+ $ this ->urlBuilder = $ urlBuilder ;
48+ $ this ->storeManager = $ storeManager ;
49+ $ this ->_configuration = $ configuration ;
2950 }
3051
3152 /**
3253 * @inheritdoc
3354 */
3455 public function resolve (Field $ field , $ context , ResolveInfo $ info , array $ value = null , array $ args = null )
3556 {
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 );
3884 $ jsonDecoder = new \Magento \Framework \Serialize \Serializer \Json ();
3985 $ productMedia = $ jsonDecoder ->unserialize ($ productMediaStr );
86+
4087 return $ productMedia ['data ' ];
4188 }
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