Enabled.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Model\Config\Backend;
  7. use Magento\Analytics\Model\Config\Backend\Enabled\SubscriptionHandler;
  8. use Magento\Framework\App\Cache\TypeListInterface;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Framework\App\Config\Value;
  11. use Magento\Framework\Data\Collection\AbstractDb;
  12. use Magento\Framework\Exception\LocalizedException;
  13. use Magento\Framework\Model\Context;
  14. use Magento\Framework\Model\ResourceModel\AbstractResource;
  15. use Magento\Framework\Registry;
  16. /**
  17. * Config value backend model.
  18. */
  19. class Enabled extends Value
  20. {
  21. /**
  22. * Path to field subscription enabled into config structure.
  23. */
  24. const XML_ENABLED_CONFIG_STRUCTURE_PATH = 'analytics/general/enabled';
  25. /**
  26. * Service for processing of activation/deactivation MBI subscription.
  27. *
  28. * @var SubscriptionHandler
  29. */
  30. private $subscriptionHandler;
  31. /**
  32. * @param Context $context
  33. * @param Registry $registry
  34. * @param ScopeConfigInterface $config
  35. * @param TypeListInterface $cacheTypeList
  36. * @param SubscriptionHandler $subscriptionHandler
  37. * @param AbstractResource|null $resource
  38. * @param AbstractDb|null $resourceCollection
  39. * @param array $data
  40. */
  41. public function __construct(
  42. Context $context,
  43. Registry $registry,
  44. ScopeConfigInterface $config,
  45. TypeListInterface $cacheTypeList,
  46. SubscriptionHandler $subscriptionHandler,
  47. AbstractResource $resource = null,
  48. AbstractDb $resourceCollection = null,
  49. array $data = []
  50. ) {
  51. $this->subscriptionHandler = $subscriptionHandler;
  52. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  53. }
  54. /**
  55. * Add additional handling after config value was saved.
  56. *
  57. * @return Value
  58. * @throws LocalizedException
  59. */
  60. public function afterSave()
  61. {
  62. try {
  63. if ($this->isValueChanged()) {
  64. $enabled = $this->getData('value');
  65. $enabled ? $this->subscriptionHandler->processEnabled() : $this->subscriptionHandler->processDisabled();
  66. }
  67. } catch (\Exception $e) {
  68. $this->_logger->error($e->getMessage());
  69. throw new LocalizedException(__('There was an error save new configuration value.'));
  70. }
  71. return parent::afterSave();
  72. }
  73. }