ConfigAccessor.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Config;
  6. use Magento\Framework\App\Config\ConfigTypeInterface;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\App\Config\Storage\WriterInterface;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\Store\Model\ScopeInterface;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. use Psr\Log\LoggerInterface;
  13. /**
  14. * Config Accessor
  15. *
  16. * @package Temando\Shipping\Model
  17. * @author Benjamin Heuer <benjamin.heuer@netresearch.de>
  18. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. * @link https://www.temando.com/
  20. */
  21. class ConfigAccessor
  22. {
  23. /**
  24. * @var StoreManagerInterface
  25. */
  26. private $storeManager;
  27. /**
  28. * @var WriterInterface
  29. */
  30. private $configWriter;
  31. /**
  32. * @var ScopeConfigInterface
  33. */
  34. private $scopeConfig;
  35. /**
  36. * @var ConfigTypeInterface
  37. */
  38. private $systemConfigType;
  39. /**
  40. * @var LoggerInterface
  41. */
  42. private $logger;
  43. /**
  44. * ConfigAccessor constructor.
  45. * @param StoreManagerInterface $storeManager
  46. * @param ScopeConfigInterface $scopeConfig
  47. * @param WriterInterface $configWriter
  48. * @param ConfigTypeInterface $systemConfigType
  49. * @param LoggerInterface $logger
  50. */
  51. public function __construct(
  52. StoreManagerInterface $storeManager,
  53. ScopeConfigInterface $scopeConfig,
  54. WriterInterface $configWriter,
  55. ConfigTypeInterface $systemConfigType,
  56. LoggerInterface $logger
  57. ) {
  58. $this->storeManager = $storeManager;
  59. $this->scopeConfig = $scopeConfig;
  60. $this->configWriter = $configWriter;
  61. $this->systemConfigType = $systemConfigType;
  62. $this->logger = $logger;
  63. }
  64. /**
  65. * Save config value to storage.
  66. *
  67. * @param string $path
  68. * @param string $value
  69. * @param mixed $scopeId
  70. * @return void
  71. */
  72. public function saveConfigValue($path, $value, $scopeId = 0)
  73. {
  74. $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
  75. if ($scopeId) {
  76. try {
  77. $scope = ScopeInterface::SCOPE_STORES;
  78. $scopeId = $this->storeManager->getStore($scopeId)->getId();
  79. } catch (NoSuchEntityException $exception) {
  80. $this->logger->warning(__('No store found for scope ID %1', $scopeId), ['exception' => $exception]);
  81. return;
  82. }
  83. }
  84. $this->configWriter->save($path, $value, $scope, $scopeId);
  85. $this->systemConfigType->clean();
  86. }
  87. /**
  88. * Delete config value from storage.
  89. *
  90. * @param string $path
  91. * @param mixed $scopeId
  92. * @return void
  93. */
  94. public function deleteConfigValue($path, $scopeId = 0)
  95. {
  96. $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
  97. if ($scopeId) {
  98. try {
  99. $scope = ScopeInterface::SCOPE_STORES;
  100. $scopeId = $this->storeManager->getStore($scopeId)->getId();
  101. } catch (NoSuchEntityException $exception) {
  102. $this->logger->warning(__('No store found for scope ID %1', $scopeId), ['exception' => $exception]);
  103. return;
  104. }
  105. }
  106. $this->configWriter->delete($path, $scope, $scopeId);
  107. $this->systemConfigType->clean();
  108. }
  109. /**
  110. * Read config value from storage.
  111. *
  112. * @param string $path
  113. * @param int $scopeId
  114. * @return mixed
  115. */
  116. public function getConfigValue($path, $scopeId = null)
  117. {
  118. $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
  119. if ($scopeId) {
  120. $scope = ScopeInterface::SCOPE_STORE;
  121. }
  122. return $this->scopeConfig->getValue($path, $scope, $scopeId);
  123. }
  124. }