SensitiveConfigSetFacade.php 4.35 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Deploy\Console\Command\App\SensitiveConfigSet;

use Magento\Deploy\Console\Command\App\SensitiveConfigSetCommand;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Deploy\Model\ConfigWriter;
use Magento\Framework\App\Config\CommentParserInterface;
use Magento\Framework\App\Scope\ValidatorInterface;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\Exception\LocalizedException;

/**
 * Processes the sensitive:config:set command.
 */
class SensitiveConfigSetFacade
{
    /**
     * The parser for config comments.
     *
     * @var CommentParserInterface
     */
    private $commentParser;

    /**
     * The pool of configs.
     *
     * @var ConfigFilePool
     */
    private $configFilePool;

    /**
     * The config writer.
     *
     * @var ConfigWriter
     */
    private $configWriter;

    /**
     * The validator for scopes.
     *
     * @var ValidatorInterface
     */
    private $scopeValidator;

    /**
     * The factory of config collectors.
     *
     * @var CollectorFactory
     */
    private $collectorFactory;

    /**
     * @param ConfigFilePool $configFilePool The pool of configs
     * @param CommentParserInterface $commentParser The parser for config comments
     * @param ConfigWriter $configWriter The config writer
     * @param ValidatorInterface $scopeValidator The validator for scopes
     * @param CollectorFactory $collectorFactory The factory of config collectors
     */
    public function __construct(
        ConfigFilePool $configFilePool,
        CommentParserInterface $commentParser,
        ConfigWriter $configWriter,
        ValidatorInterface $scopeValidator,
        CollectorFactory $collectorFactory
    ) {
        $this->commentParser = $commentParser;
        $this->configFilePool = $configFilePool;
        $this->configWriter = $configWriter;
        $this->scopeValidator = $scopeValidator;
        $this->collectorFactory = $collectorFactory;
    }

    /**
     * Processes the config:sensitive:set command.
     *
     * @param InputInterface $input The input manager
     * @param OutputInterface $output The output manager
     * @return void
     * @throws LocalizedException If scope or scope code is not valid
     * @throws RuntimeException If sensitive config can not be filled
     */
    public function process(InputInterface $input, OutputInterface $output)
    {
        $scope = $input->getOption(SensitiveConfigSetCommand::INPUT_OPTION_SCOPE);
        $scopeCode = $input->getOption(SensitiveConfigSetCommand::INPUT_OPTION_SCOPE_CODE);
        $isInteractive = $input->getOption(SensitiveConfigSetCommand::INPUT_OPTION_INTERACTIVE);

        $this->scopeValidator->isValid($scope, $scopeCode);
        $configPaths = $this->getConfigPaths();
        $collector = $this->collectorFactory->create(
            $isInteractive ? CollectorFactory::TYPE_INTERACTIVE : CollectorFactory::TYPE_SIMPLE
        );
        $values = $collector->getValues($input, $output, $configPaths);

        $this->configWriter->save($values, $scope, $scopeCode);

        $output->writeln(sprintf(
            '<info>Configuration value%s saved in app/etc/%s</info>',
            $isInteractive ? 's' : '',
            $this->configFilePool->getPath(ConfigFilePool::APP_ENV)
        ));
    }

    /**
     * Get sensitive configuration paths.
     *
     * @return array
     * @throws LocalizedException if configuration file not exists or sensitive configuration is empty
     */
    private function getConfigPaths()
    {
        $configFilePath = $this->configFilePool->getPath(ConfigFilePool::APP_CONFIG);
        try {
            $configPaths = $this->commentParser->execute($configFilePath);
        } catch (FileSystemException $e) {
            throw new RuntimeException(__(
                'File app/etc/%1 can\'t be read. Please check if it exists and has read permissions.',
                [
                    $configFilePath
                ]
            ));
        }

        if (empty($configPaths)) {
            throw new RuntimeException(__('There are no sensitive configurations to fill'));
        }

        return $configPaths;
    }
}