Skip to content

Commit be94408

Browse files
committed
change topic generation to be based on service contract rather than route info
1 parent 1e67f8b commit be94408

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

app/code/Magento/WebapiAsync/Model/Config.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,18 @@ public function getServices()
7878
public function getTopicName($routeUrl, $httpMethod)
7979
{
8080
$services = $this->getServices();
81-
$topicName = $this->generateTopicNameByRouteData(
81+
$lookupKey = $this->generateLookupKeyByRouteData(
8282
$routeUrl,
8383
$httpMethod
8484
);
8585

86-
if (array_key_exists($topicName, $services) === false) {
86+
if (array_key_exists($lookupKey, $services) === false) {
8787
throw new LocalizedException(
88-
__('WebapiAsync config for "%topicName" does not exist.', ['topicName' => $topicName])
88+
__('WebapiAsync config for "%lookupKey" does not exist.', ['lookupKey' => $lookupKey])
8989
);
9090
}
9191

92-
return $services[$topicName][self::SERVICE_PARAM_KEY_TOPIC];
92+
return $services[$lookupKey][self::SERVICE_PARAM_KEY_TOPIC];
9393
}
9494

9595
/**
@@ -105,11 +105,18 @@ private function generateTopicsDataFromWebapiConfig()
105105
$serviceInterface = $httpMethodData[Converter::KEY_SERVICE][Converter::KEY_SERVICE_CLASS];
106106
$serviceMethod = $httpMethodData[Converter::KEY_SERVICE][Converter::KEY_SERVICE_METHOD];
107107

108-
$topicName = $this->generateTopicNameByRouteData(
108+
$lookupKey = $this->generateLookupKeyByRouteData(
109109
$routeUrl,
110110
$httpMethod
111111
);
112-
$services[$topicName] = [
112+
113+
$topicName = $this->generateTopicNameFromService(
114+
$serviceInterface,
115+
$serviceMethod,
116+
$httpMethod
117+
);
118+
119+
$services[$lookupKey] = [
113120
self::SERVICE_PARAM_KEY_INTERFACE => $serviceInterface,
114121
self::SERVICE_PARAM_KEY_METHOD => $serviceMethod,
115122
self::SERVICE_PARAM_KEY_TOPIC => $topicName,
@@ -122,7 +129,7 @@ private function generateTopicsDataFromWebapiConfig()
122129
}
123130

124131
/**
125-
* Generate topic name based on service type and method name.
132+
* Generate lookup key name based on route and method
126133
*
127134
* Perform the following conversion:
128135
* self::TOPIC_PREFIX + /V1/products + POST => async.V1.products.POST
@@ -131,19 +138,39 @@ private function generateTopicsDataFromWebapiConfig()
131138
* @param string $httpMethod
132139
* @return string
133140
*/
134-
private function generateTopicNameByRouteData($routeUrl, $httpMethod)
141+
private function generateLookupKeyByRouteData($routeUrl, $httpMethod)
135142
{
136-
return self::TOPIC_PREFIX . $this->generateTopicName($routeUrl, $httpMethod, '/', false);
143+
return self::TOPIC_PREFIX . $this->generateKey($routeUrl, $httpMethod, '/', false);
137144
}
138145

139146
/**
147+
* Generate topic name based on service type and method name.
148+
*
149+
* Perform the following conversion:
150+
* self::TOPIC_PREFIX + Magento\Catalog\Api\ProductRepositoryInterface + save + POST
151+
* => async.magento.catalog.api.productrepositoryinterface.save.POST
152+
*
153+
* @param string $serviceMethod
154+
* @param string $serviceInterface
155+
* @param string $httpMethod
156+
* @return string
157+
*/
158+
private function generateTopicNameFromService($serviceInterface, $serviceMethod, $httpMethod)
159+
{
160+
$typeName = strtolower(sprintf('%s.%s', $serviceInterface, $serviceMethod));
161+
return self::TOPIC_PREFIX . $this->generateKey($typeName, $httpMethod, '\\', false);
162+
}
163+
164+
/**
165+
* Join and simplify input type and method into a string that can be used as an array key
166+
*
140167
* @param string $typeName
141168
* @param string $methodName
142169
* @param string $delimiter
143170
* @param bool $lcfirst
144171
* @return string
145172
*/
146-
private function generateTopicName($typeName, $methodName, $delimiter = '\\', $lcfirst = true)
173+
private function generateKey($typeName, $methodName, $delimiter = '\\', $lcfirst = true)
147174
{
148175
$parts = explode($delimiter, ltrim($typeName, $delimiter));
149176
foreach ($parts as &$part) {

app/code/Magento/WebapiAsync/Test/Unit/Model/ConfigTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function setUp()
5454
);
5555
}
5656

57-
public function testGetServicesSetsTopicFromRoute()
57+
public function testGetServicesSetsTopicFromServiceContractName()
5858
{
5959
$services = [
6060
Converter::KEY_ROUTES => [
@@ -77,14 +77,14 @@ public function testGetServicesSetsTopicFromRoute()
7777
'async.V1.products.POST' => [
7878
'interface' => 'Magento\Catalog\Api\ProductRepositoryInterface',
7979
'method' => 'save',
80-
'topic' => 'async.V1.products.POST',
80+
'topic' => 'async.magento.catalog.api.productrepositoryinterface.save.POST',
8181
]
8282
];
8383
*/
8484
$result = $this->config->getServices();
8585

86-
$expectedTopic = 'async.V1.products.POST';
87-
$this->assertArrayHasKey($expectedTopic, $result);
88-
$this->assertEquals($result[$expectedTopic]['topic'], $expectedTopic);
89-
}
90-
}
86+
$expectedTopic = 'async.magento.catalog.api.productrepositoryinterface.save.POST';
87+
$lookupKey = 'async.V1.products.POST';
88+
$this->assertArrayHasKey($lookupKey, $result);
89+
$this->assertEquals($result[$lookupKey]['topic'], $expectedTopic);
90+
}}

0 commit comments

Comments
 (0)