Value.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Config;
  7. /**
  8. * Config data model
  9. *
  10. * This model is temporarily marked as API since {@see \Magento\Framework\App\Config\ValueInterface} doesn't fit
  11. * developers' needs of extensibility. In 2.4 we are going to introduce a new interface which should cover all needs
  12. * and deprecate the mentioned together with the model
  13. *
  14. * @method string getScope()
  15. * @method \Magento\Framework\App\Config\ValueInterface setScope(string $value)
  16. * @method int getScopeId()
  17. * @method \Magento\Framework\App\Config\ValueInterface setScopeId(int $value)
  18. * @method string getPath()
  19. * @method \Magento\Framework\App\Config\ValueInterface setPath(string $value)
  20. * @method string getValue()
  21. * @method \Magento\Framework\App\Config\ValueInterface setValue(string $value)
  22. *
  23. * @api
  24. *
  25. * @SuppressWarnings(PHPMD.NumberOfChildren)
  26. * @since 100.0.2
  27. */
  28. class Value extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\App\Config\ValueInterface
  29. {
  30. /**
  31. * Prefix of model events names
  32. *
  33. * @var string
  34. */
  35. protected $_eventPrefix = 'config_data';
  36. /**
  37. * Parameter name in event
  38. *
  39. * In observe method you can use $observer->getEvent()->getObject() in this case
  40. *
  41. * @var string
  42. */
  43. protected $_eventObject = 'config_data';
  44. /**
  45. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  46. */
  47. protected $_config;
  48. /**
  49. * @var \Magento\Framework\App\Cache\TypeListInterface
  50. */
  51. protected $cacheTypeList;
  52. /**
  53. * @param \Magento\Framework\Model\Context $context
  54. * @param \Magento\Framework\Registry $registry
  55. * @param ScopeConfigInterface $config
  56. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  57. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  58. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  59. * @param array $data
  60. */
  61. public function __construct(
  62. \Magento\Framework\Model\Context $context,
  63. \Magento\Framework\Registry $registry,
  64. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  65. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  66. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  67. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  68. array $data = []
  69. ) {
  70. $this->_config = $config;
  71. $this->cacheTypeList = $cacheTypeList;
  72. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  73. }
  74. /**
  75. * Check if config data value was changed
  76. *
  77. * @return bool
  78. */
  79. public function isValueChanged()
  80. {
  81. return $this->getValue() != $this->getOldValue();
  82. }
  83. /**
  84. * Get old value from existing config
  85. *
  86. * @return string
  87. */
  88. public function getOldValue()
  89. {
  90. return (string)$this->_config->getValue(
  91. $this->getPath(),
  92. $this->getScope() ?: ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  93. $this->getScopeCode()
  94. );
  95. }
  96. /**
  97. * Get value by key for new user data from <section>/groups/<group>/fields/<field>
  98. *
  99. * @param string $key
  100. * @return string
  101. */
  102. public function getFieldsetDataValue($key)
  103. {
  104. $data = $this->_getData('fieldset_data');
  105. return is_array($data) && isset($data[$key]) ? $data[$key] : null;
  106. }
  107. /**
  108. * Processing object after save data
  109. *
  110. * {@inheritdoc}. In addition, it sets status 'invalidate' for config caches
  111. *
  112. * @return $this
  113. */
  114. public function afterSave()
  115. {
  116. if ($this->isValueChanged()) {
  117. $this->cacheTypeList->invalidate(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);
  118. }
  119. return parent::afterSave();
  120. }
  121. /**
  122. * Processing object after delete data
  123. *
  124. * {@inheritdoc}. In addition, it sets status 'invalidate' for config caches
  125. *
  126. * @return $this
  127. * @since 100.1.0
  128. */
  129. public function afterDelete()
  130. {
  131. $this->cacheTypeList->invalidate(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);
  132. return parent::afterDelete();
  133. }
  134. }