Converter.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\WebapiAsync\Model\ServiceConfig;
  8. /**
  9. * Converter of webapi_async.xml content into array format.
  10. */
  11. class Converter implements \Magento\Framework\Config\ConverterInterface
  12. {
  13. /**#@+
  14. * Array keys for config internal representation.
  15. */
  16. const KEY_SERVICES = 'services';
  17. const KEY_METHOD = 'method';
  18. const KEY_METHODS = 'methods';
  19. const KEY_SYNCHRONOUS_INVOCATION_ONLY = 'synchronousInvocationOnly';
  20. const KEY_ROUTES = 'routes';
  21. /**#@-*/
  22. private $allowedRouteMethods = [
  23. \Magento\Webapi\Model\Rest\Config::HTTP_METHOD_GET,
  24. \Magento\Webapi\Model\Rest\Config::HTTP_METHOD_POST,
  25. \Magento\Webapi\Model\Rest\Config::HTTP_METHOD_PUT,
  26. \Magento\Webapi\Model\Rest\Config::HTTP_METHOD_DELETE,
  27. \Magento\Webapi\Model\Rest\Config::HTTP_METHOD_PATCH
  28. ];
  29. /**
  30. * {@inheritdoc}
  31. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  32. * @SuppressWarnings(PHPMD.NPathComplexity)
  33. */
  34. public function convert($source)
  35. {
  36. $result = [self::KEY_SERVICES => []];
  37. /** @var \DOMNodeList $services */
  38. $services = $source->getElementsByTagName('service');
  39. /** @var \DOMElement $service */
  40. foreach ($services as $service) {
  41. if (!$this->canConvertXmlNode($service)) {
  42. continue;
  43. }
  44. $serviceClass = $this->getServiceClass($service);
  45. $serviceMethod = $this->getServiceMethod($service);
  46. // Define the service method's key if this hasn't yet been defined
  47. $this->initServiceMethodsKey($result, $serviceClass, $serviceMethod);
  48. $this->mergeSynchronousInvocationMethodsData($service, $result, $serviceClass, $serviceMethod);
  49. }
  50. $result[self::KEY_ROUTES] = $this->convertRouteCustomizations($source);
  51. return $result;
  52. }
  53. /**
  54. * Merge service data related to synchronous-only method invocations.
  55. *
  56. * @param \DOMElement $service
  57. * @param array $result
  58. * @param string $serviceClass
  59. * @param string $serviceMethod
  60. */
  61. private function mergeSynchronousInvocationMethodsData(
  62. \DOMElement $service,
  63. array &$result,
  64. $serviceClass,
  65. $serviceMethod
  66. ) {
  67. $result[self::KEY_SERVICES][$serviceClass][self::KEY_METHODS][$serviceMethod] = array_merge(
  68. $result[self::KEY_SERVICES][$serviceClass][self::KEY_METHODS][$serviceMethod],
  69. [
  70. self::KEY_SYNCHRONOUS_INVOCATION_ONLY => $this->isSynchronousMethodInvocationOnly($service)
  71. ]
  72. );
  73. }
  74. /**
  75. * @param \DOMElement $node
  76. * @return bool
  77. */
  78. private function canConvertXmlNode(\DOMElement $node)
  79. {
  80. if ($node->nodeType !== XML_ELEMENT_NODE) {
  81. return false;
  82. }
  83. if ($this->getServiceClass($node) === null) {
  84. return false;
  85. }
  86. if ($this->getServiceMethod($node) === null) {
  87. return false;
  88. }
  89. return true;
  90. }
  91. /**
  92. * Define the methods key against the service. Allows for other types of service information.
  93. *
  94. * @param array $result
  95. * @param string $serviceClass
  96. * @param string $serviceMethod
  97. */
  98. private function initServiceMethodsKey(array &$result, $serviceClass, $serviceMethod)
  99. {
  100. if (!isset($result[self::KEY_SERVICES][$serviceClass])) {
  101. $result[self::KEY_SERVICES][$serviceClass] = [self::KEY_METHODS => []];
  102. }
  103. if (!isset($result[self::KEY_SERVICES][$serviceClass][self::KEY_METHODS][$serviceMethod])) {
  104. $result[self::KEY_SERVICES][$serviceClass][self::KEY_METHODS][$serviceMethod] = [];
  105. }
  106. }
  107. /**
  108. * @param \DOMElement $service
  109. * @return null|string
  110. */
  111. private function getServiceClass(\DOMElement $service)
  112. {
  113. $serviceClass = $service->attributes->getNamedItem('class')->nodeValue;
  114. return mb_strlen((string) $serviceClass) === 0 ? null : $serviceClass;
  115. }
  116. /**
  117. * @param \DOMElement $service
  118. * @return null|string
  119. */
  120. private function getServiceMethod(\DOMElement $service)
  121. {
  122. $serviceMethod = $service->attributes->getNamedItem('method')->nodeValue;
  123. return mb_strlen((string) $serviceMethod) === 0 ? null : $serviceMethod;
  124. }
  125. /**
  126. * @param \DOMElement $serviceNode
  127. * @return bool
  128. */
  129. private function isSynchronousMethodInvocationOnly(\DOMElement $serviceNode)
  130. {
  131. $synchronousInvocationOnlyNodes = $serviceNode->getElementsByTagName('synchronousInvocationOnly');
  132. return $this->isSynchronousInvocationOnlyTrue($synchronousInvocationOnlyNodes->item(0));
  133. }
  134. /**
  135. * @param \DOMElement $synchronousInvocationOnlyNode
  136. * @return bool|mixed
  137. */
  138. private function isSynchronousInvocationOnlyTrue(\DOMElement $synchronousInvocationOnlyNode = null)
  139. {
  140. if ($synchronousInvocationOnlyNode === null) {
  141. return false;
  142. }
  143. if (mb_strlen((string) $synchronousInvocationOnlyNode->nodeValue) === 0) {
  144. return true;
  145. }
  146. return filter_var($synchronousInvocationOnlyNode->nodeValue, FILTER_VALIDATE_BOOLEAN);
  147. }
  148. /**
  149. * Convert and merge "route" nodes, which represent route customizations
  150. * @param \DOMDocument $source
  151. * @return array
  152. */
  153. private function convertRouteCustomizations($source)
  154. {
  155. $customRoutes = [];
  156. $routes = $source->getElementsByTagName('route');
  157. /** @var \DOMElement $route */
  158. foreach ($routes as $route) {
  159. $routeUrl = $this->getRouteUrl($route);
  160. $routeMethod = $this->getRouteMethod($route);
  161. $routeAlias = $this->getRouteAlias($route);
  162. if ($routeUrl && $routeMethod && $routeAlias) {
  163. if (!isset($customRoutes[$routeAlias])) {
  164. $customRoutes[$routeAlias] = [];
  165. }
  166. $customRoutes[$routeAlias][$routeMethod] = $routeUrl;
  167. }
  168. }
  169. return $customRoutes;
  170. }
  171. /**
  172. * @param \DOMElement $route
  173. * @return null|string
  174. */
  175. private function getRouteUrl($route)
  176. {
  177. $url = $route->attributes->getNamedItem('url')->nodeValue;
  178. return mb_strlen((string) $url) === 0 ? null : $url;
  179. }
  180. /**
  181. * @param \DOMElement $route
  182. * @return null|string
  183. */
  184. private function getRouteAlias($route)
  185. {
  186. $alias = $route->attributes->getNamedItem('alias')->nodeValue;
  187. return mb_strlen((string) $alias) === 0 ? null : ltrim($alias, '/');
  188. }
  189. /**
  190. * @param \DOMElement $route
  191. * @return null|string
  192. */
  193. private function getRouteMethod($route)
  194. {
  195. $method = $route->attributes->getNamedItem('method')->nodeValue;
  196. $method = mb_strlen((string) $method) === 0 ? null : $method;
  197. return ($this->validateRouteMethod($method)) ? $method : null;
  198. }
  199. /**
  200. * @param string $method
  201. * @return bool
  202. */
  203. private function validateRouteMethod($method)
  204. {
  205. return in_array($method, $this->allowedRouteMethods);
  206. }
  207. }