DesignConfigRepository.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model;
  7. use Magento\Framework\Indexer\IndexerRegistry;
  8. use Magento\Theme\Api\Data\DesignConfigInterface;
  9. use Magento\Theme\Api\DesignConfigRepositoryInterface;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Framework\App\Config\ReinitableConfigInterface;
  12. use Magento\Theme\Model\Data\Design\Config as DesignConfig;
  13. use Magento\Theme\Model\Design\Config\Storage as ConfigStorage;
  14. class DesignConfigRepository implements DesignConfigRepositoryInterface
  15. {
  16. /**
  17. * @var \Magento\Framework\App\Config\ReinitableConfigInterface
  18. */
  19. protected $reinitableConfig;
  20. /**
  21. * @var \Magento\Framework\Indexer\IndexerRegistry
  22. */
  23. protected $indexerRegistry;
  24. /**
  25. * @var \Magento\Theme\Model\Design\Config\Storage
  26. */
  27. protected $configStorage;
  28. /**
  29. * Design config validator
  30. *
  31. * @var \Magento\Theme\Model\Design\Config\Validator
  32. */
  33. private $validator;
  34. /**
  35. * @param ConfigStorage $configStorage
  36. * @param ReinitableConfigInterface $reinitableConfig
  37. * @param IndexerRegistry $indexerRegistry
  38. */
  39. public function __construct(
  40. ConfigStorage $configStorage,
  41. ReinitableConfigInterface $reinitableConfig,
  42. IndexerRegistry $indexerRegistry
  43. ) {
  44. $this->reinitableConfig = $reinitableConfig;
  45. $this->indexerRegistry = $indexerRegistry;
  46. $this->configStorage = $configStorage;
  47. }
  48. /**
  49. * Get config validator
  50. *
  51. * @return Design\Config\Validator
  52. *
  53. * @deprecated 100.1.0
  54. */
  55. private function getValidator()
  56. {
  57. if (null === $this->validator) {
  58. $this->validator =\Magento\Framework\App\ObjectManager::getInstance()->get(
  59. \Magento\Theme\Model\Design\Config\Validator::class
  60. );
  61. }
  62. return $this->validator;
  63. }
  64. /**
  65. * @inheritDoc
  66. */
  67. public function getByScope($scope, $scopeId)
  68. {
  69. return $this->configStorage->load($scope, $scopeId);
  70. }
  71. /**
  72. * @inheritDoc
  73. */
  74. public function save(DesignConfigInterface $designConfig)
  75. {
  76. if (!($designConfig->getExtensionAttributes() &&
  77. $designConfig->getExtensionAttributes()->getDesignConfigData())
  78. ) {
  79. throw new LocalizedException(
  80. __("The config can't be saved because it's empty. Complete the config and try again.")
  81. );
  82. }
  83. $this->getValidator()->validate($designConfig);
  84. $this->configStorage->save($designConfig);
  85. $this->reinitableConfig->reinit();
  86. $this->reindexGrid();
  87. return $designConfig;
  88. }
  89. /**
  90. * @inheritDoc
  91. */
  92. public function delete(DesignConfigInterface $designConfig)
  93. {
  94. if (!($designConfig->getExtensionAttributes() &&
  95. $designConfig->getExtensionAttributes()->getDesignConfigData())
  96. ) {
  97. throw new LocalizedException(
  98. __("The config can't be saved because it's empty. Complete the config and try again.")
  99. );
  100. }
  101. $this->configStorage->delete($designConfig);
  102. $this->reinitableConfig->reinit();
  103. $this->reindexGrid();
  104. return $designConfig;
  105. }
  106. /**
  107. * Synchronize design config grid
  108. *
  109. * @return void
  110. */
  111. protected function reindexGrid()
  112. {
  113. $this->indexerRegistry->get(DesignConfig::DESIGN_CONFIG_GRID_INDEXER_ID)->reindexAll();
  114. }
  115. }