Validator.php 6.17 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
<?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;
    }
}