|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\MessageQueue\Setup; |
| 9 | + |
| 10 | +use Magento\Framework\Setup\ConfigOptionsListInterface; |
| 11 | +use Magento\Framework\Setup\Option\SelectConfigOption; |
| 12 | +use Magento\Framework\App\DeploymentConfig; |
| 13 | +use Magento\Framework\Config\Data\ConfigData; |
| 14 | +use Magento\Framework\Config\File\ConfigFilePool; |
| 15 | + |
| 16 | +/** |
| 17 | + * Deployment configuration consumers options needed for Setup application |
| 18 | + */ |
| 19 | +class ConfigOptionsList implements ConfigOptionsListInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Input key for the option |
| 23 | + */ |
| 24 | + const INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES ='consumers-wait-for-messages'; |
| 25 | + |
| 26 | + /** |
| 27 | + * Path to the value in the deployment config |
| 28 | + */ |
| 29 | + const CONFIG_PATH_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES = 'queue/consumers_wait_for_messages'; |
| 30 | + |
| 31 | + /** |
| 32 | + * Default value |
| 33 | + */ |
| 34 | + const DEFULT_CONSUMERS_WAIT_FOR_MESSAGES = 1; |
| 35 | + |
| 36 | + private $selectOptions = [0, 1]; |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheritdoc |
| 40 | + */ |
| 41 | + public function getOptions() |
| 42 | + { |
| 43 | + return [ |
| 44 | + new SelectConfigOption( |
| 45 | + self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES, |
| 46 | + SelectConfigOption::FRONTEND_WIZARD_SELECT, |
| 47 | + $this->selectOptions, |
| 48 | + self::CONFIG_PATH_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES, |
| 49 | + 'Should consumers wait for message from the queue? 1 - Yes, 0 - No', |
| 50 | + self::DEFULT_CONSUMERS_WAIT_FOR_MESSAGES |
| 51 | + ), |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @inheritdoc |
| 57 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 58 | + */ |
| 59 | + public function createConfig(array $data, DeploymentConfig $deploymentConfig) |
| 60 | + { |
| 61 | + $configData = new ConfigData(ConfigFilePool::APP_ENV); |
| 62 | + |
| 63 | + if (!$this->isDataEmpty($data, self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES)) { |
| 64 | + $configData->set( |
| 65 | + self::CONFIG_PATH_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES, |
| 66 | + (int)$data[self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES] |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + return[$configData]; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @inheritdoc |
| 75 | + */ |
| 76 | + public function validate(array $options, DeploymentConfig $deploymentConfig) |
| 77 | + { |
| 78 | + $errors = []; |
| 79 | + |
| 80 | + if (!$this->isDataEmpty($options, self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES) |
| 81 | + && !in_array($options[self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES], $this->selectOptions)) { |
| 82 | + $errors[] = 'You can use only 1 or 0 for ' . self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES . ' option'; |
| 83 | + } |
| 84 | + |
| 85 | + return $errors; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Check if data ($data) with key ($key) is empty |
| 90 | + * |
| 91 | + * @param array $data |
| 92 | + * @param string $key |
| 93 | + * @return bool |
| 94 | + */ |
| 95 | + private function isDataEmpty(array $data, $key) |
| 96 | + { |
| 97 | + if (isset($data[$key]) && $data[$key] !== '') { |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + return true; |
| 102 | + } |
| 103 | +} |
0 commit comments