ConsumerFactory.php 5.41 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\MessageQueue;

use Magento\Framework\MessageQueue\ConfigInterface as QueueConfig;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Phrase;
use Magento\Framework\MessageQueue\ConsumerInterface;
use Magento\Framework\MessageQueue\Consumer\ConfigInterface as ConsumerConfig;
use Magento\Framework\MessageQueue\Consumer\Config\ConsumerConfigItemInterface;
use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;

/**
 * Class which creates Consumers
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class ConsumerFactory
{
    /**
     * Object Manager instance
     *
     * @var ObjectManagerInterface
     */
    private $objectManager = null;

    /**
     * @var ConsumerConfig
     */
    private $consumerConfig;

    /**
     * @var CommunicationConfig
     */
    private $communicationConfig;

    /**
     * Initialize dependencies.
     *
     * @param QueueConfig $queueConfig
     * @param ObjectManagerInterface $objectManager
     *
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function __construct(
        QueueConfig $queueConfig,
        ObjectManagerInterface $objectManager
    ) {
        $this->objectManager = $objectManager;
    }

    /**
     * Return the actual Consumer implementation for the given consumer name.
     *
     * @param string $consumerName
     * @param int $batchSize [optional]
     * @return ConsumerInterface
     * @throws LocalizedException
     */
    public function get($consumerName, $batchSize = 0)
    {
        $consumerConfig = $this->getConsumerConfig()->getConsumer($consumerName);
        if ($consumerConfig === null) {
            throw new LocalizedException(
                new Phrase('Specified consumer "%consumer" is not declared.', ['consumer' => $consumerName])
            );
        }

        return $this->objectManager->create(
            $consumerConfig->getConsumerInstance(),
            [
                'configuration' => $this->createConsumerConfiguration($consumerConfig),
                'batchSize' => $batchSize,
            ]
        );
    }

    /**
     * Creates the objects necessary for the ConsumerConfigurationInterface to configure a Consumer.
     *
     * @param ConsumerConfigItemInterface $consumerConfigItem
     * @return ConsumerConfigurationInterface
     */
    private function createConsumerConfiguration($consumerConfigItem)
    {
        $customConsumerHandlers = [];
        foreach ($consumerConfigItem->getHandlers() as $handlerConfig) {
            $customConsumerHandlers[] = [
                $this->objectManager->create($handlerConfig->getType()),
                $handlerConfig->getMethod()
            ];
        }
        $topics = [];
        foreach ($this->getCommunicationConfig()->getTopics() as $topicConfig) {
            $topicName = $topicConfig[CommunicationConfig::TOPIC_NAME];
            $topics[$topicName] = [
                ConsumerConfigurationInterface::TOPIC_HANDLERS => $customConsumerHandlers
                    ?: $this->getHandlersFromCommunicationConfig($topicName),
                ConsumerConfigurationInterface::TOPIC_TYPE => $topicConfig[CommunicationConfig::TOPIC_IS_SYNCHRONOUS]
                    ? ConsumerConfiguration::TYPE_SYNC
                    : ConsumerConfiguration::TYPE_ASYNC
            ];
        }
        $configData = [
            ConsumerConfigurationInterface::CONSUMER_NAME => $consumerConfigItem->getName(),
            ConsumerConfigurationInterface::QUEUE_NAME => $consumerConfigItem->getQueue(),
            ConsumerConfigurationInterface::TOPICS => $topics,
            ConsumerConfigurationInterface::MAX_MESSAGES => $consumerConfigItem->getMaxMessages(),
        ];

        return $this->objectManager->create(
            \Magento\Framework\MessageQueue\ConsumerConfiguration::class,
            ['data' => $configData]
        );
    }

    /**
     * Get consumer config.
     *
     * @return ConsumerConfig
     *
     * @deprecated 102.0.1
     */
    private function getConsumerConfig()
    {
        if ($this->consumerConfig === null) {
            $this->consumerConfig = \Magento\Framework\App\ObjectManager::getInstance()->get(ConsumerConfig::class);
        }
        return $this->consumerConfig;
    }

    /**
     * Get communication config.
     *
     * @return CommunicationConfig
     *
     * @deprecated 102.0.1
     */
    private function getCommunicationConfig()
    {
        if ($this->communicationConfig === null) {
            $this->communicationConfig = \Magento\Framework\App\ObjectManager::getInstance()
                ->get(CommunicationConfig::class);
        }
        return $this->communicationConfig;
    }

    /**
     * Get handlers by topic based on communication config.
     *
     * @param string $topicName
     * @return array
     */
    private function getHandlersFromCommunicationConfig($topicName)
    {
        $topicConfig = $this->getCommunicationConfig()->getTopic($topicName);
        $handlers = [];
        foreach ($topicConfig[CommunicationConfig::TOPIC_HANDLERS] as $handlerConfig) {
            $handlers[] = [
                $this->objectManager->create($handlerConfig[CommunicationConfig::HANDLER_TYPE]),
                $handlerConfig[CommunicationConfig::HANDLER_METHOD]
            ];
        }
        return $handlers;
    }
}