15
15
use Magento \Framework \Exception \LocalizedException ;
16
16
use Magento \Webapi \Model \Config \Converter ;
17
17
18
+ /**
19
+ * Class for accessing to Webapi_Async configuration.
20
+ */
18
21
class Config implements \Magento \AsynchronousOperations \Model \ConfigInterface
19
22
{
20
23
/**
@@ -55,7 +58,7 @@ public function __construct(
55
58
}
56
59
57
60
/**
58
- * { @inheritdoc}
61
+ * @inheritdoc
59
62
*/
60
63
public function getServices ()
61
64
{
@@ -73,26 +76,30 @@ public function getServices()
73
76
}
74
77
75
78
/**
76
- * { @inheritdoc}
79
+ * @inheritdoc
77
80
*/
78
81
public function getTopicName ($ routeUrl , $ httpMethod )
79
82
{
80
83
$ services = $ this ->getServices ();
81
- $ topicName = $ this ->generateTopicNameByRouteData (
84
+ $ lookupKey = $ this ->generateLookupKeyByRouteData (
82
85
$ routeUrl ,
83
86
$ httpMethod
84
87
);
85
88
86
- if (array_key_exists ($ topicName , $ services ) === false ) {
89
+ if (array_key_exists ($ lookupKey , $ services ) === false ) {
87
90
throw new LocalizedException (
88
- __ ('WebapiAsync config for "%topicName " does not exist. ' , ['topicName ' => $ topicName ])
91
+ __ ('WebapiAsync config for "%lookupKey " does not exist. ' , ['lookupKey ' => $ lookupKey ])
89
92
);
90
93
}
91
94
92
- return $ services [$ topicName ][self ::SERVICE_PARAM_KEY_TOPIC ];
95
+ return $ services [$ lookupKey ][self ::SERVICE_PARAM_KEY_TOPIC ];
93
96
}
94
97
95
98
/**
99
+ * Generate topic data for all defined services
100
+ *
101
+ * Topic data is indexed by a lookup key that is derived from route data
102
+ *
96
103
* @return array
97
104
*/
98
105
private function generateTopicsDataFromWebapiConfig ()
@@ -105,11 +112,18 @@ private function generateTopicsDataFromWebapiConfig()
105
112
$ serviceInterface = $ httpMethodData [Converter::KEY_SERVICE ][Converter::KEY_SERVICE_CLASS ];
106
113
$ serviceMethod = $ httpMethodData [Converter::KEY_SERVICE ][Converter::KEY_SERVICE_METHOD ];
107
114
108
- $ topicName = $ this ->generateTopicNameByRouteData (
115
+ $ lookupKey = $ this ->generateLookupKeyByRouteData (
109
116
$ routeUrl ,
110
117
$ httpMethod
111
118
);
112
- $ services [$ topicName ] = [
119
+
120
+ $ topicName = $ this ->generateTopicNameFromService (
121
+ $ serviceInterface ,
122
+ $ serviceMethod ,
123
+ $ httpMethod
124
+ );
125
+
126
+ $ services [$ lookupKey ] = [
113
127
self ::SERVICE_PARAM_KEY_INTERFACE => $ serviceInterface ,
114
128
self ::SERVICE_PARAM_KEY_METHOD => $ serviceMethod ,
115
129
self ::SERVICE_PARAM_KEY_TOPIC => $ topicName ,
@@ -122,7 +136,7 @@ private function generateTopicsDataFromWebapiConfig()
122
136
}
123
137
124
138
/**
125
- * Generate topic name based on service type and method name.
139
+ * Generate lookup key name based on route and method
126
140
*
127
141
* Perform the following conversion:
128
142
* self::TOPIC_PREFIX + /V1/products + POST => async.V1.products.POST
@@ -131,19 +145,39 @@ private function generateTopicsDataFromWebapiConfig()
131
145
* @param string $httpMethod
132
146
* @return string
133
147
*/
134
- private function generateTopicNameByRouteData ($ routeUrl , $ httpMethod )
148
+ private function generateLookupKeyByRouteData ($ routeUrl , $ httpMethod )
135
149
{
136
- return self ::TOPIC_PREFIX . $ this ->generateTopicName ($ routeUrl , $ httpMethod , '/ ' , false );
150
+ return self ::TOPIC_PREFIX . $ this ->generateKey ($ routeUrl , $ httpMethod , '/ ' , false );
137
151
}
138
152
139
153
/**
154
+ * Generate topic name based on service type and method name.
155
+ *
156
+ * Perform the following conversion:
157
+ * self::TOPIC_PREFIX + Magento\Catalog\Api\ProductRepositoryInterface + save + POST
158
+ * => async.magento.catalog.api.productrepositoryinterface.save.POST
159
+ *
160
+ * @param string $serviceInterface
161
+ * @param string $serviceMethod
162
+ * @param string $httpMethod
163
+ * @return string
164
+ */
165
+ private function generateTopicNameFromService ($ serviceInterface , $ serviceMethod , $ httpMethod )
166
+ {
167
+ $ typeName = strtolower (sprintf ('%s.%s ' , $ serviceInterface , $ serviceMethod ));
168
+ return strtolower (self ::TOPIC_PREFIX . $ this ->generateKey ($ typeName , $ httpMethod , '\\' , false ));
169
+ }
170
+
171
+ /**
172
+ * Join and simplify input type and method into a string that can be used as an array key
173
+ *
140
174
* @param string $typeName
141
175
* @param string $methodName
142
176
* @param string $delimiter
143
177
* @param bool $lcfirst
144
178
* @return string
145
179
*/
146
- private function generateTopicName ($ typeName , $ methodName , $ delimiter = '\\' , $ lcfirst = true )
180
+ private function generateKey ($ typeName , $ methodName , $ delimiter = '\\' , $ lcfirst = true )
147
181
{
148
182
$ parts = explode ($ delimiter , ltrim ($ typeName , $ delimiter ));
149
183
foreach ($ parts as &$ part ) {
0 commit comments