Validator.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Communication\Config;
  7. use Magento\Framework\Reflection\TypeProcessor;
  8. use Magento\Framework\Reflection\MethodsMap;
  9. /**
  10. * Communication configuration validator.
  11. */
  12. class Validator
  13. {
  14. /**
  15. * @var TypeProcessor
  16. */
  17. private $typeProcessor;
  18. /**
  19. * @var MethodsMap
  20. */
  21. private $methodsMap;
  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 response schema definition for topic
  37. *
  38. * @param string $responseSchema
  39. * @param string $topicName
  40. * @return void
  41. */
  42. public function validateResponseSchemaType($responseSchema, $topicName)
  43. {
  44. try {
  45. $this->validateType($responseSchema);
  46. } catch (\InvalidArgumentException $e) {
  47. throw new \LogicException(
  48. 'Response schema definition has service class with wrong annotated methods',
  49. $e->getCode(),
  50. $e
  51. );
  52. } catch (\Exception $e) {
  53. throw new \LogicException(
  54. sprintf(
  55. 'Response schema definition for topic "%s" should reference existing type or service class. '
  56. . 'Given "%s"',
  57. $topicName,
  58. $responseSchema
  59. )
  60. );
  61. }
  62. }
  63. /**
  64. * Validate request schema definition for topic
  65. *
  66. * @param string $requestSchema
  67. * @param string $topicName
  68. * @return void
  69. */
  70. public function validateRequestSchemaType($requestSchema, $topicName)
  71. {
  72. try {
  73. $this->validateType($requestSchema);
  74. } catch (\InvalidArgumentException $e) {
  75. throw new \LogicException(
  76. 'Request schema definition has service class with wrong annotated methods',
  77. $e->getCode(),
  78. $e
  79. );
  80. } catch (\Exception $e) {
  81. throw new \LogicException(
  82. sprintf(
  83. 'Request schema definition for topic "%s" should reference existing service class. '
  84. . 'Given "%s"',
  85. $topicName,
  86. $requestSchema
  87. )
  88. );
  89. }
  90. }
  91. /**
  92. * Validate service method specified in the definition of handler
  93. *
  94. * @param string $serviceName
  95. * @param string $methodName
  96. * @param string $handlerName
  97. * @param string $topicName
  98. * @return void
  99. */
  100. public function validateResponseHandlersType($serviceName, $methodName, $handlerName, $topicName)
  101. {
  102. try {
  103. $this->methodsMap->getMethodParams($serviceName, $methodName);
  104. } catch (\Exception $e) {
  105. throw new \LogicException(
  106. sprintf(
  107. 'Service method specified in the definition of handler "%s" for topic "%s"'
  108. . ' is not available. Given "%s"',
  109. $handlerName,
  110. $topicName,
  111. $serviceName . '::' . $methodName
  112. )
  113. );
  114. }
  115. }
  116. /**
  117. * Ensure that specified type is either a simple type or a valid service data type.
  118. *
  119. * @param string $typeName
  120. * @return $this
  121. * @throws \Exception In case when type is invalid
  122. * @throws \InvalidArgumentException if methods don't have annotation
  123. */
  124. protected function validateType($typeName)
  125. {
  126. if ($this->typeProcessor->isTypeSimple($typeName)) {
  127. return $this;
  128. }
  129. if ($this->typeProcessor->isArrayType($typeName)) {
  130. $arrayItemType = $this->typeProcessor->getArrayItemType($typeName);
  131. $this->methodsMap->getMethodsMap($arrayItemType);
  132. } else {
  133. $this->methodsMap->getMethodsMap($typeName);
  134. }
  135. return $this;
  136. }
  137. }