6
6
use Cloudinary \Api ;
7
7
use Cloudinary \Cloudinary \Core \ConfigurationBuilder ;
8
8
use Cloudinary \Cloudinary \Core \ConfigurationInterface ;
9
+ use Magento \Framework \App \Request \Http ;
10
+ use Magento \Framework \Json \EncoderInterface ;
9
11
10
12
class ResourcesManagement implements \Cloudinary \Cloudinary \Api \ResourcesManagementInterface
11
13
{
@@ -28,26 +30,36 @@ class ResourcesManagement implements \Cloudinary\Cloudinary\Api\ResourcesManagem
28
30
private $ _api ;
29
31
30
32
/**
31
- * @var \Magento\Framework\App\Request\ Http
33
+ * @var Http
32
34
*/
33
35
private $ _request ;
34
36
37
+ /**
38
+ * @var EncoderInterface
39
+ */
40
+ private $ _jsonEncoder ;
41
+
35
42
/**
36
43
* ApiClient constructor.
37
44
*
38
45
* @param ConfigurationInterface $configuration
39
46
* @param ConfigurationBuilder $configurationBuilder
47
+ * @param Api $api
48
+ * @param Http $request
49
+ * @param EncoderInterface $jsonEncoder
40
50
*/
41
51
public function __construct (
42
52
ConfigurationInterface $ configuration ,
43
53
ConfigurationBuilder $ configurationBuilder ,
44
54
Api $ api ,
45
- \Magento \Framework \App \Request \Http $ request
55
+ Http $ request ,
56
+ EncoderInterface $ jsonEncoder
46
57
) {
47
58
$ this ->_configuration = $ configuration ;
48
59
$ this ->_configurationBuilder = $ configurationBuilder ;
49
60
$ this ->_api = $ api ;
50
61
$ this ->_request = $ request ;
62
+ $ this ->_jsonEncoder = $ jsonEncoder ;
51
63
if ($ this ->_configuration ->isEnabled ()) {
52
64
$ this ->_authorise ();
53
65
}
@@ -59,13 +71,6 @@ private function _authorise()
59
71
Cloudinary::$ USER_PLATFORM = $ this ->_configuration ->getUserPlatform ();
60
72
}
61
73
62
- public function _sendJsonResponse ($ response )
63
- {
64
- header ('Content-Type: application/json ' );
65
- echo json_encode ($ response );
66
- die;
67
- }
68
-
69
74
/**
70
75
* Get details of a single resource
71
76
*
@@ -76,18 +81,19 @@ protected function _getResourceData()
76
81
{
77
82
try {
78
83
$ this ->_resourceData = $ this ->_api ->resource (
79
- $ this ->_request ->getParam ("id " ), [
84
+ $ this ->_request ->getParam ("id " ),
85
+ [
80
86
"resource_type " => $ this ->_resourceType
81
87
]
82
88
);
83
- $ this ->_sendJsonResponse (
89
+ return $ this ->_jsonEncoder -> encode (
84
90
[
85
91
"error " => 0 ,
86
92
"data " => $ this ->_resourceData
87
93
]
88
94
);
89
95
} catch (\Exception $ e ) {
90
- $ this ->_sendJsonResponse (
96
+ return $ this ->_jsonEncoder -> encode (
91
97
[
92
98
"error " => 1 ,
93
99
"message " => $ e ->getMessage ()
@@ -102,7 +108,7 @@ protected function _getResourceData()
102
108
public function getImage ()
103
109
{
104
110
$ this ->_resourceType = "image " ;
105
- $ this ->_getResourceData ();
111
+ return $ this ->_getResourceData ();
106
112
}
107
113
108
114
/**
@@ -111,6 +117,6 @@ public function getImage()
111
117
public function getVideo ()
112
118
{
113
119
$ this ->_resourceType = "video " ;
114
- $ this ->_getResourceData ();
120
+ return $ this ->_getResourceData ();
115
121
}
116
122
}
0 commit comments