123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\MessageQueue\Config;
- use Magento\Framework\Reflection\MethodsMap;
- use Magento\Framework\Reflection\TypeProcessor;
- /**
- * Communication configuration validator.
- */
- class Validator
- {
- /**
- * @var MethodsMap
- */
- private $methodsMap;
- /**
- * @var TypeProcessor
- */
- private $typeProcessor;
- /**
- * Initialize dependencies.
- *
- * @param TypeProcessor $typeProcessor
- * @param MethodsMap $methodsMap
- */
- public function __construct(
- TypeProcessor $typeProcessor,
- MethodsMap $methodsMap
- ) {
- $this->typeProcessor = $typeProcessor;
- $this->methodsMap = $methodsMap;
- }
- /**
- * Validate schema Method Type
- *
- * @param string $schemaType
- * @param string $schemaMethod
- * @param string $topicName
- * @return void
- * @throws \LogicException
- */
- public function validateSchemaMethodType($schemaType, $schemaMethod, $topicName)
- {
- try {
- $this->methodsMap->getMethodParams($schemaType, $schemaMethod);
- } catch (\Exception $e) {
- throw new \LogicException(
- sprintf(
- 'Service method specified for topic "%s" is not available. Given "%s"',
- $topicName,
- $schemaType . '::' . $schemaMethod
- )
- );
- }
- }
- /**
- * Validate handler type
- *
- * @param string $serviceName
- * @param string $methodName
- * @param string $consumerName
- * @return void
- * @throws \LogicException
- */
- public function validateHandlerType($serviceName, $methodName, $consumerName)
- {
- try {
- $this->methodsMap->getMethodParams($serviceName, $methodName);
- } catch (\Exception $e) {
- throw new \LogicException(
- sprintf(
- 'Service method specified in handler for consumer "%s"'
- . ' is not available. Given "%s"',
- $consumerName,
- $serviceName . '::' . $methodName
- )
- );
- }
- }
- /**
- * Validate topic in a bind
- *
- * @param string[] $topics
- * @param string $topicName
- * @return void
- * @throws \LogicException
- */
- public function validateBindTopic($topics, $topicName)
- {
- $isDefined = false;
- if (strpos($topicName, '#') === false && strpos($topicName, '*') === false) {
- if (in_array($topicName, $topics)) {
- $isDefined = true;
- }
- } else {
- $pattern = $this->buildWildcardPattern($topicName);
- if (count(preg_grep($pattern, $topics))) {
- $isDefined = true;
- }
- }
- if (!$isDefined) {
- throw new \LogicException(
- sprintf('Topic "%s" declared in binds must be defined in topics', $topicName)
- );
- }
- }
- /**
- * Construct perl regexp pattern for matching topic names from wildcard key.
- *
- * @param string $wildcardKey
- * @return string
- */
- public function buildWildcardPattern($wildcardKey)
- {
- $pattern = '/^' . str_replace('.', '\.', $wildcardKey);
- $pattern = str_replace('#', '.+', $pattern);
- $pattern = str_replace('*', '[^\.]+', $pattern);
- if (strpos($wildcardKey, '#') === strlen($wildcardKey)) {
- $pattern .= '/';
- } else {
- $pattern .= '$/';
- }
- return $pattern;
- }
- /**
- * Validate publisher in the topic
- *
- * @param string[] $publishers
- * @param string $publisherName
- * @param string $topicName
- * @return void
- * @throws \LogicException
- */
- public function validateTopicPublisher($publishers, $publisherName, $topicName)
- {
- if (!in_array($publisherName, $publishers)) {
- throw new \LogicException(
- sprintf(
- 'Publisher "%s", specified in env.php for topic "%s" is not declared.',
- $publisherName,
- $topicName
- )
- );
- }
- }
- /**
- * Validate response schema type
- *
- * @param string $responseSchema
- * @param string $topicName
- * @return void
- * @throws \LogicException
- */
- public function validateResponseSchemaType($responseSchema, $topicName)
- {
- try {
- $this->validateType($responseSchema);
- } catch (\Exception $e) {
- throw new \LogicException(
- sprintf(
- 'Response schema definition for topic "%s" should reference existing type or service class. '
- . 'Given "%s"',
- $topicName,
- $responseSchema
- )
- );
- }
- }
- /**
- * Validate schema type
- *
- * @param string $schema
- * @param string $topicName
- * @return void
- * @throws \LogicException
- */
- public function validateSchemaType($schema, $topicName)
- {
- try {
- $this->validateType($schema);
- } catch (\Exception $e) {
- throw new \LogicException(
- sprintf(
- 'Schema definition for topic "%s" should reference existing type or service class. '
- . 'Given "%s"',
- $topicName,
- $schema
- )
- );
- }
- }
- /**
- * Ensure that specified type is either a simple type or a valid service data type.
- *
- * @param string $typeName
- * @return $this
- * @throws \Exception In case when type is invalid
- */
- protected function validateType($typeName)
- {
- if ($this->typeProcessor->isTypeSimple($typeName)) {
- return $this;
- }
- if ($this->typeProcessor->isArrayType($typeName)) {
- $arrayItemType = $this->typeProcessor->getArrayItemType($typeName);
- $this->methodsMap->getMethodsMap($arrayItemType);
- } else {
- $this->methodsMap->getMethodsMap($typeName);
- }
- return $this;
- }
- }
|