8
8
namespace Magento \CatalogGraphQl \Model \Resolver \Product ;
9
9
10
10
use Magento \Catalog \Model \Product ;
11
- use Magento \Catalog \Helper \ImageFactory as CatalogImageHelperFactory ;
11
+ use Magento \Catalog \Model \Product \ImageFactory ;
12
+ use Magento \Framework \Exception \LocalizedException ;
12
13
use Magento \Framework \GraphQl \Config \Element \Field ;
13
- use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
14
14
use Magento \Framework \GraphQl \Query \ResolverInterface ;
15
15
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
16
- use Magento \Store \Model \StoreManagerInterface ;
17
16
18
17
/**
19
- * Return product image paths by image type.
18
+ * Returns product's image data
20
19
*/
21
20
class ProductImage implements ResolverInterface
22
21
{
23
22
/**
24
- * @var CatalogImageHelperFactory
25
- */
26
- private $ catalogImageHelperFactory ;
27
-
28
- /**
29
- * @var StoreManagerInterface
23
+ * Product image factory
24
+ *
25
+ * @var ImageFactory
30
26
*/
31
- private $ storeManager ;
27
+ private $ productImageFactory ;
32
28
33
29
/**
34
- * @param CatalogImageHelperFactory $catalogImageHelperFactory
35
- * @param StoreManagerInterface $storeManager
30
+ * @param ImageFactory $productImageFactory
36
31
*/
37
32
public function __construct (
38
- CatalogImageHelperFactory $ catalogImageHelperFactory ,
39
- StoreManagerInterface $ storeManager
33
+ ImageFactory $ productImageFactory
40
34
) {
41
- $ this ->catalogImageHelperFactory = $ catalogImageHelperFactory ;
42
- $ this ->storeManager = $ storeManager ;
35
+ $ this ->productImageFactory = $ productImageFactory ;
43
36
}
44
37
45
38
/**
46
- * Get product's image by type.
47
- *
48
39
* @inheritdoc
49
40
*/
50
41
public function resolve (
@@ -53,30 +44,26 @@ public function resolve(
53
44
ResolveInfo $ info ,
54
45
array $ value = null ,
55
46
array $ args = null
56
- ) {
47
+ ): array {
57
48
if (!isset ($ value ['model ' ])) {
58
- throw new GraphQlInputException (__ ('"model" value should be specified ' ));
49
+ throw new LocalizedException (__ ('"model" value should be specified ' ));
59
50
}
60
51
/** @var Product $product */
61
52
$ product = $ value ['model ' ];
62
53
$ imageType = $ field ->getName ();
63
54
64
- /** @var \Magento\Catalog\Helper\Image $catalogImageHelper */
65
- $ catalogImageHelper = $ this -> catalogImageHelperFactory -> create ();
55
+ $ imagePath = $ product -> getData ( $ imageType );
56
+ $ imageLabel = $ product -> getData ( $ imageType . ' _ ' . ' label ' ) ?? $ product -> getName ();
66
57
67
- /** @var \Magento\Catalog\Helper\Image $image */
68
- $ image = $ catalogImageHelper ->init (
69
- $ product ,
70
- 'product_ ' . $ imageType ,
71
- ['type ' => $ imageType ]
72
- );
58
+ $ image = $ this ->productImageFactory ->create ();
59
+ $ image ->setDestinationSubdir ($ imageType )
60
+ ->setBaseFile ($ imagePath );
61
+ $ imageUrl = $ image ->getUrl ();
73
62
74
- $ imageData = [
75
- 'url ' => $ image -> getUrl () ,
76
- 'path ' => $ product -> getData ( $ imageType ) ,
77
- 'label ' => $ image -> getLabel ()
63
+ return [
64
+ 'url ' => $ imageUrl ,
65
+ 'path ' => $ imagePath ,
66
+ 'label ' => $ imageLabel ,
78
67
];
79
-
80
- return $ imageData ;
81
68
}
82
69
}
0 commit comments