Validator.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\MessageQueue\Config;
  7. use Magento\Framework\Reflection\MethodsMap;
  8. use Magento\Framework\Reflection\TypeProcessor;
  9. /**
  10. * Communication configuration validator.
  11. */
  12. class Validator
  13. {
  14. /**
  15. * @var MethodsMap
  16. */
  17. private $methodsMap;
  18. /**
  19. * @var TypeProcessor
  20. */
  21. private $typeProcessor;
  22. /**
  23. * Initialize dependencies.
  24. *
  25. * @param TypeProcessor $typeProcessor
  26. * @param MethodsMap $methodsMap
  27. */
  28. public function __construct(
  29. TypeProcessor $typeProcessor,
  30. MethodsMap $methodsMap
  31. ) {
  32. $this->typeProcessor = $typeProcessor;
  33. $this->methodsMap = $methodsMap;
  34. }
  35. /**
  36. * Validate schema Method Type
  37. *
  38. * @param string $schemaType
  39. * @param string $schemaMethod
  40. * @param string $topicName
  41. * @return void
  42. * @throws \LogicException
  43. */
  44. public function validateSchemaMethodType($schemaType, $schemaMethod, $topicName)
  45. {
  46. try {
  47. $this->methodsMap->getMethodParams($schemaType, $schemaMethod);
  48. } catch (\Exception $e) {
  49. throw new \LogicException(
  50. sprintf(
  51. 'Service method specified for topic "%s" is not available. Given "%s"',
  52. $topicName,
  53. $schemaType . '::' . $schemaMethod
  54. )
  55. );
  56. }
  57. }
  58. /**
  59. * Validate handler type
  60. *
  61. * @param string $serviceName
  62. * @param string $methodName
  63. * @param string $consumerName
  64. * @return void
  65. * @throws \LogicException
  66. */
  67. public function validateHandlerType($serviceName, $methodName, $consumerName)
  68. {
  69. try {
  70. $this->methodsMap->getMethodParams($serviceName, $methodName);
  71. } catch (\Exception $e) {
  72. throw new \LogicException(
  73. sprintf(
  74. 'Service method specified in handler for consumer "%s"'
  75. . ' is not available. Given "%s"',
  76. $consumerName,
  77. $serviceName . '::' . $methodName
  78. )
  79. );
  80. }
  81. }
  82. /**
  83. * Validate topic in a bind
  84. *
  85. * @param string[] $topics
  86. * @param string $topicName
  87. * @return void
  88. * @throws \LogicException
  89. */
  90. public function validateBindTopic($topics, $topicName)
  91. {
  92. $isDefined = false;
  93. if (strpos($topicName, '#') === false && strpos($topicName, '*') === false) {
  94. if (in_array($topicName, $topics)) {
  95. $isDefined = true;
  96. }
  97. } else {
  98. $pattern = $this->buildWildcardPattern($topicName);
  99. if (count(preg_grep($pattern, $topics))) {
  100. $isDefined = true;
  101. }
  102. }
  103. if (!$isDefined) {
  104. throw new \LogicException(
  105. sprintf('Topic "%s" declared in binds must be defined in topics', $topicName)
  106. );
  107. }
  108. }
  109. /**
  110. * Construct perl regexp pattern for matching topic names from wildcard key.
  111. *
  112. * @param string $wildcardKey
  113. * @return string
  114. */
  115. public function buildWildcardPattern($wildcardKey)
  116. {
  117. $pattern = '/^' . str_replace('.', '\.', $wildcardKey);
  118. $pattern = str_replace('#', '.+', $pattern);
  119. $pattern = str_replace('*', '[^\.]+', $pattern);
  120. if (strpos($wildcardKey, '#') === strlen($wildcardKey)) {
  121. $pattern .= '/';
  122. } else {
  123. $pattern .= '$/';
  124. }
  125. return $pattern;
  126. }
  127. /**
  128. * Validate publisher in the topic
  129. *
  130. * @param string[] $publishers
  131. * @param string $publisherName
  132. * @param string $topicName
  133. * @return void
  134. * @throws \LogicException
  135. */
  136. public function validateTopicPublisher($publishers, $publisherName, $topicName)
  137. {
  138. if (!in_array($publisherName, $publishers)) {
  139. throw new \LogicException(
  140. sprintf(
  141. 'Publisher "%s", specified in env.php for topic "%s" is not declared.',
  142. $publisherName,
  143. $topicName
  144. )
  145. );
  146. }
  147. }
  148. /**
  149. * Validate response schema type
  150. *
  151. * @param string $responseSchema
  152. * @param string $topicName
  153. * @return void
  154. * @throws \LogicException
  155. */
  156. public function validateResponseSchemaType($responseSchema, $topicName)
  157. {
  158. try {
  159. $this->validateType($responseSchema);
  160. } catch (\Exception $e) {
  161. throw new \LogicException(
  162. sprintf(
  163. 'Response schema definition for topic "%s" should reference existing type or service class. '
  164. . 'Given "%s"',
  165. $topicName,
  166. $responseSchema
  167. )
  168. );
  169. }
  170. }
  171. /**
  172. * Validate schema type
  173. *
  174. * @param string $schema
  175. * @param string $topicName
  176. * @return void
  177. * @throws \LogicException
  178. */
  179. public function validateSchemaType($schema, $topicName)
  180. {
  181. try {
  182. $this->validateType($schema);
  183. } catch (\Exception $e) {
  184. throw new \LogicException(
  185. sprintf(
  186. 'Schema definition for topic "%s" should reference existing type or service class. '
  187. . 'Given "%s"',
  188. $topicName,
  189. $schema
  190. )
  191. );
  192. }
  193. }
  194. /**
  195. * Ensure that specified type is either a simple type or a valid service data type.
  196. *
  197. * @param string $typeName
  198. * @return $this
  199. * @throws \Exception In case when type is invalid
  200. */
  201. protected function validateType($typeName)
  202. {
  203. if ($this->typeProcessor->isTypeSimple($typeName)) {
  204. return $this;
  205. }
  206. if ($this->typeProcessor->isArrayType($typeName)) {
  207. $arrayItemType = $this->typeProcessor->getArrayItemType($typeName);
  208. $this->methodsMap->getMethodsMap($arrayItemType);
  209. } else {
  210. $this->methodsMap->getMethodsMap($typeName);
  211. }
  212. return $this;
  213. }
  214. }