DefaultProcessor.php 3.53 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Config\Console\Command\ConfigSet;

use Magento\Config\App\Config\Type\System;
use Magento\Config\Console\Command\ConfigSetCommand;
use Magento\Config\Model\Config\Factory as ConfigFactory;
use Magento\Framework\App\Config\ConfigPathResolver;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Config\Model\PreparedValueFactory;

/**
 * Processes default flow of config:set command.
 *
 * This processor saves the value of configuration into database.
 *
 * @inheritdoc
 * @api
 * @since 101.0.0
 */
class DefaultProcessor implements ConfigSetProcessorInterface
{
    /**
     * The deployment configuration reader.
     *
     * @var DeploymentConfig
     */
    private $deploymentConfig;

    /**
     * The resolver for configuration paths according to source type.
     *
     * @var ConfigPathResolver
     */
    private $configPathResolver;

    /**
     * The factory for prepared value.
     *
     * @var PreparedValueFactory
     */
    private $preparedValueFactory;

    /**
     * @var ConfigFactory
     */
    private $configFactory;

    /**
     * @param PreparedValueFactory $preparedValueFactory The factory for prepared value
     * @param DeploymentConfig $deploymentConfig The deployment configuration reader
     * @param ConfigPathResolver $configPathResolver The resolver for configuration paths according to source type
     * @param ConfigFactory|null $configFactory
     */
    public function __construct(
        PreparedValueFactory $preparedValueFactory,
        DeploymentConfig $deploymentConfig,
        ConfigPathResolver $configPathResolver,
        ConfigFactory $configFactory = null
    ) {
        $this->preparedValueFactory = $preparedValueFactory;
        $this->deploymentConfig = $deploymentConfig;
        $this->configPathResolver = $configPathResolver;

        $this->configFactory = $configFactory ?? ObjectManager::getInstance()->get(ConfigFactory::class);
    }

    /**
     * Processes database flow of config:set command.
     *
     * Requires installed application.
     *
     * @inheritdoc
     * @since 101.0.0
     */
    public function process($path, $value, $scope, $scopeCode)
    {
        if ($this->isLocked($path, $scope, $scopeCode)) {
            throw new CouldNotSaveException(
                __(
                    'The value you set has already been locked. To change the value, use the --%1 option.',
                    ConfigSetCommand::OPTION_LOCK_ENV
                )
            );
        }

        try {
            $config = $this->configFactory->create([
                'scope' => $scope,
                'scope_code' => $scopeCode,
            ]);
            $config->setDataByPath($path, $value);
            $config->save();
        } catch (\Exception $exception) {
            throw new CouldNotSaveException(__('%1', $exception->getMessage()), $exception);
        }
    }

    /**
     * Checks whether configuration is locked in file storage.
     *
     * @param string $path The path to configuration
     * @param string $scope The scope of configuration
     * @param string $scopeCode The scope code of configuration
     * @return bool
     */
    private function isLocked($path, $scope, $scopeCode)
    {
        $scopePath = $this->configPathResolver->resolve($path, $scope, $scopeCode, System::CONFIG_TYPE);

        return $this->deploymentConfig->get($scopePath) !== null;
    }
}