ConfigFactory.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Data\Design;
  7. use Magento\Framework\App\ScopeValidatorInterface;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Store\Model\StoreManagerInterface;
  10. use Magento\Theme\Api\Data\DesignConfigDataInterface;
  11. use Magento\Theme\Api\Data\DesignConfigExtension;
  12. use Magento\Theme\Api\Data\DesignConfigInterfaceFactory;
  13. use Magento\Theme\Model\Design\Config\MetadataProviderInterface;
  14. use Magento\Theme\Api\Data\DesignConfigDataInterfaceFactory;
  15. use Magento\Theme\Api\Data\DesignConfigExtensionFactory;
  16. use Magento\Theme\Api\Data\DesignConfigInterface;
  17. use Magento\Store\Model\ScopeInterface;
  18. /**
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class ConfigFactory
  22. {
  23. /**
  24. * @var DesignConfigInterfaceFactory
  25. */
  26. protected $designConfigFactory;
  27. /**
  28. * @var MetadataProviderInterface
  29. */
  30. protected $metadataProvider;
  31. /**
  32. * @var DesignConfigDataInterfaceFactory
  33. */
  34. protected $designConfigDataFactory;
  35. /**
  36. * @var DesignConfigExtensionFactory
  37. */
  38. protected $configExtensionFactory;
  39. /**
  40. * @var ScopeValidatorInterface
  41. */
  42. protected $scopeValidator;
  43. /**
  44. * @var StoreManagerInterface
  45. */
  46. protected $storeManager;
  47. /**
  48. * @param DesignConfigInterfaceFactory $designConfigFactory
  49. * @param MetadataProviderInterface $metadataProvider
  50. * @param DesignConfigDataInterfaceFactory $designConfigDataFactory
  51. * @param DesignConfigExtensionFactory $configExtensionFactory
  52. * @param ScopeValidatorInterface $scopeValidator
  53. * @param StoreManagerInterface $storeManager
  54. */
  55. public function __construct(
  56. DesignConfigInterfaceFactory $designConfigFactory,
  57. MetadataProviderInterface $metadataProvider,
  58. DesignConfigDataInterfaceFactory $designConfigDataFactory,
  59. DesignConfigExtensionFactory $configExtensionFactory,
  60. ScopeValidatorInterface $scopeValidator,
  61. StoreManagerInterface $storeManager
  62. ) {
  63. $this->designConfigFactory = $designConfigFactory;
  64. $this->metadataProvider = $metadataProvider;
  65. $this->designConfigDataFactory = $designConfigDataFactory;
  66. $this->configExtensionFactory = $configExtensionFactory;
  67. $this->scopeValidator = $scopeValidator;
  68. $this->storeManager = $storeManager;
  69. }
  70. /**
  71. * Create Design Configuration for scope
  72. *
  73. * @param mixed $scope
  74. * @param int $scopeId
  75. * @param array $data
  76. * @return DesignConfigInterface
  77. * @throws LocalizedException
  78. */
  79. public function create($scope, $scopeId, array $data = [])
  80. {
  81. if (!$this->scopeValidator->isValidScope($scope, $scopeId)) {
  82. throw new LocalizedException(__('The scope or scope ID is invalid. Verify both and try again.'));
  83. }
  84. $designConfigData = $this->getDesignConfigData($scope, $scopeId);
  85. $configData = [];
  86. foreach ($this->metadataProvider->get() as $name => $metadata) {
  87. $metadata['field'] = $name;
  88. /** @var DesignConfigDataInterface $configDataObject */
  89. $configDataObject = $this->designConfigDataFactory->create();
  90. $configDataObject->setPath($metadata['path']);
  91. $configDataObject->setFieldConfig($metadata);
  92. if (isset($data[$name])) {
  93. $configDataObject->setValue($data[$name]);
  94. }
  95. $configData[] = $configDataObject;
  96. }
  97. /** @var DesignConfigExtension $designConfigExtension */
  98. $designConfigExtension = $this->configExtensionFactory->create();
  99. $designConfigExtension->setDesignConfigData($configData);
  100. $designConfigData->setExtensionAttributes($designConfigExtension);
  101. return $designConfigData;
  102. }
  103. /**
  104. * Retrieve design config data with correct scope
  105. *
  106. * @param string $scope
  107. * @param string $scopeId
  108. * @return DesignConfigInterface
  109. */
  110. protected function getDesignConfigData($scope, $scopeId)
  111. {
  112. /** @var DesignConfigInterface $designConfigData */
  113. $designConfigData = $this->designConfigFactory->create();
  114. $scopeInfo = $this->getCorrectScope($scope, $scopeId);
  115. $designConfigData->setScope($scopeInfo['scope']);
  116. $designConfigData->setScopeId($scopeInfo['scopeId']);
  117. return $designConfigData;
  118. }
  119. /**
  120. * Retrieve correct scope corresponding single store mode configuration
  121. *
  122. * @param string $scope
  123. * @param string $scopeId
  124. * @return array
  125. */
  126. protected function getCorrectScope($scope, $scopeId)
  127. {
  128. $isSingleStoreMode = $this->storeManager->isSingleStoreMode();
  129. if ($isSingleStoreMode) {
  130. $websites = $this->storeManager->getWebsites();
  131. $singleStoreWebsite = array_shift($websites);
  132. $scope = ScopeInterface::SCOPE_WEBSITES;
  133. $scopeId = $singleStoreWebsite->getId();
  134. }
  135. return [
  136. 'scope' => $scope,
  137. 'scopeId' => $scopeId
  138. ];
  139. }
  140. }