ConfigAccessor.php 3.8 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
<?php
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */

namespace Temando\Shipping\Model\Config;

use Magento\Framework\App\Config\ConfigTypeInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;

/**
 * Config Accessor
 *
 * @package Temando\Shipping\Model
 * @author  Benjamin Heuer <benjamin.heuer@netresearch.de>
 * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @link    https://www.temando.com/
 */
class ConfigAccessor
{
    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    /**
     * @var WriterInterface
     */
    private $configWriter;

    /**
     * @var ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * @var ConfigTypeInterface
     */
    private $systemConfigType;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * ConfigAccessor constructor.
     * @param StoreManagerInterface $storeManager
     * @param ScopeConfigInterface $scopeConfig
     * @param WriterInterface $configWriter
     * @param ConfigTypeInterface $systemConfigType
     * @param LoggerInterface $logger
     */
    public function __construct(
        StoreManagerInterface $storeManager,
        ScopeConfigInterface $scopeConfig,
        WriterInterface $configWriter,
        ConfigTypeInterface $systemConfigType,
        LoggerInterface $logger
    ) {
        $this->storeManager = $storeManager;
        $this->scopeConfig = $scopeConfig;
        $this->configWriter = $configWriter;
        $this->systemConfigType = $systemConfigType;
        $this->logger = $logger;
    }

    /**
     * Save config value to storage.
     *
     * @param string $path
     * @param string $value
     * @param mixed $scopeId
     * @return void
     */
    public function saveConfigValue($path, $value, $scopeId = 0)
    {
        $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT;

        if ($scopeId) {
            try {
                $scope = ScopeInterface::SCOPE_STORES;
                $scopeId = $this->storeManager->getStore($scopeId)->getId();
            } catch (NoSuchEntityException $exception) {
                $this->logger->warning(__('No store found for scope ID %1', $scopeId), ['exception' => $exception]);
                return;
            }
        }

        $this->configWriter->save($path, $value, $scope, $scopeId);
        $this->systemConfigType->clean();
    }

    /**
     * Delete config value from storage.
     *
     * @param string $path
     * @param mixed $scopeId
     * @return void
     */
    public function deleteConfigValue($path, $scopeId = 0)
    {
        $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT;

        if ($scopeId) {
            try {
                $scope = ScopeInterface::SCOPE_STORES;
                $scopeId = $this->storeManager->getStore($scopeId)->getId();
            } catch (NoSuchEntityException $exception) {
                $this->logger->warning(__('No store found for scope ID %1', $scopeId), ['exception' => $exception]);
                return;
            }
        }

        $this->configWriter->delete($path, $scope, $scopeId);
        $this->systemConfigType->clean();
    }

    /**
     * Read config value from storage.
     *
     * @param string $path
     * @param int $scopeId
     * @return mixed
     */
    public function getConfigValue($path, $scopeId = null)
    {
        $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
        if ($scopeId) {
            $scope = ScopeInterface::SCOPE_STORE;
        }

        return $this->scopeConfig->getValue($path, $scope, $scopeId);
    }
}