diff --git a/Model/Config/Backend/Credentials.php b/Model/Config/Backend/Credentials.php index c212462..75fcc66 100644 --- a/Model/Config/Backend/Credentials.php +++ b/Model/Config/Backend/Credentials.php @@ -9,6 +9,7 @@ use Cloudinary\Cloudinary\Core\Exception\InvalidCredentials; use Magento\Config\Model\Config\Backend\Encrypted; use Magento\Framework\App\Cache\TypeListInterface; +use Magento\Framework\App\Config\ReinitableConfigInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Data\Collection\AbstractDb; use Magento\Framework\Encryption\EncryptorInterface; @@ -39,6 +40,13 @@ class Credentials extends Encrypted */ private $api; + /** + * Application config + * + * @var ScopeConfigInterface + */ + protected $appConfig; + /** * @param Context $context * @param Registry $registry @@ -50,6 +58,7 @@ class Credentials extends Encrypted * @param AbstractDb $resourceCollection * @param ConfigurationBuilder $configurationBuilder * @param Api $api + * @param ReinitableConfigInterface $appConfig * @param array $data */ public function __construct( @@ -63,11 +72,13 @@ public function __construct( AbstractDb $resourceCollection = null, ConfigurationBuilder $configurationBuilder, Api $api, + ReinitableConfigInterface $appConfig, array $data = [] ) { $this->configuration = $configuration; $this->configurationBuilder = $configurationBuilder; $this->api = $api; + $this->appConfig = $appConfig; parent::__construct( $context, @@ -87,7 +98,13 @@ public function beforeSave() parent::beforeSave(); - if ($rawValue) { + $this->cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER); + $this->appConfig->reinit(); + + if ($rawValue || $this->configuration->isEnabled(false)) { + if (!$rawValue) { + throw new ValidatorException(__(self::CREDENTIALS_CHECK_MISSING)); + } if ($this->isSaveAllowed()) { $this->validate($this->getCredentialsFromEnvironmentVariable($rawValue)); } else { diff --git a/Model/Configuration.php b/Model/Configuration.php index aef7aa7..24d5eb1 100644 --- a/Model/Configuration.php +++ b/Model/Configuration.php @@ -173,9 +173,9 @@ public function getUploadConfig() /** * @return boolean */ - public function isEnabled() + public function isEnabled($checkEnvVar = true) { - return $this->hasEnvironmentVariable() && $this->configReader->isSetFlag(self::CONFIG_PATH_ENABLED); + return ($this->hasEnvironmentVariable() || !$checkEnvVar) && $this->configReader->isSetFlag(self::CONFIG_PATH_ENABLED); } public function enable()