MigrateStoresAllowedCountriesToWebsite.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Setup\Patch\Data;
  7. use Magento\Directory\Model\AllowedCountries;
  8. use Magento\Framework\Setup\ModuleDataSetupInterface;
  9. use Magento\Directory\Model\AllowedCountriesFactory;
  10. use Magento\Store\Model\ScopeInterface;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. use Magento\Framework\Setup\Patch\DataPatchInterface;
  13. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  14. class MigrateStoresAllowedCountriesToWebsite implements DataPatchInterface, PatchVersionInterface
  15. {
  16. /**
  17. * @var ModuleDataSetupInterface
  18. */
  19. private $moduleDataSetup;
  20. /**
  21. * @var StoreManagerInterface
  22. */
  23. private $storeManager;
  24. /**
  25. * @var AllowedCountriesFactory
  26. */
  27. private $allowedCountries;
  28. /**
  29. * MigrateStoresAllowedCountriesToWebsite constructor.
  30. * @param ModuleDataSetupInterface $moduleDataSetup
  31. * @param StoreManagerInterface $storeManager
  32. * @param AllowedCountries $allowedCountries
  33. */
  34. public function __construct(
  35. ModuleDataSetupInterface $moduleDataSetup,
  36. \Magento\Store\Model\StoreManagerInterface $storeManager,
  37. \Magento\Directory\Model\AllowedCountries $allowedCountries
  38. ) {
  39. $this->moduleDataSetup = $moduleDataSetup;
  40. $this->storeManager = $storeManager;
  41. $this->allowedCountries = $allowedCountries;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function apply()
  47. {
  48. $this->moduleDataSetup->getConnection()->beginTransaction();
  49. try {
  50. $this->migrateStoresAllowedCountriesToWebsite();
  51. $this->moduleDataSetup->getConnection()->commit();
  52. } catch (\Exception $e) {
  53. $this->moduleDataSetup->getConnection()->rollBack();
  54. throw $e;
  55. }
  56. }
  57. /**
  58. * Merge allowed countries from stores to websites
  59. *
  60. * @return void
  61. */
  62. private function migrateStoresAllowedCountriesToWebsite()
  63. {
  64. $allowedCountries = [];
  65. //Process Websites
  66. foreach ($this->storeManager->getStores() as $store) {
  67. $allowedCountries = $this->mergeAllowedCountries(
  68. $allowedCountries,
  69. $this->getAllowedCountries(ScopeInterface::SCOPE_STORE, $store->getId()),
  70. $store->getWebsiteId()
  71. );
  72. }
  73. //Process stores
  74. foreach ($this->storeManager->getWebsites() as $website) {
  75. $allowedCountries = $this->mergeAllowedCountries(
  76. $allowedCountries,
  77. $this->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $website->getId()),
  78. $website->getId()
  79. );
  80. }
  81. $connection = $this->moduleDataSetup->getConnection();
  82. //Remove everything from stores scope
  83. $connection->delete(
  84. $this->moduleDataSetup->getTable('core_config_data'),
  85. [
  86. 'path = ?' => AllowedCountries::ALLOWED_COUNTRIES_PATH,
  87. 'scope = ?' => ScopeInterface::SCOPE_STORES
  88. ]
  89. );
  90. //Update websites
  91. foreach ($allowedCountries as $scopeId => $countries) {
  92. $connection->update(
  93. $this->moduleDataSetup->getTable('core_config_data'),
  94. [
  95. 'value' => implode(',', $countries)
  96. ],
  97. [
  98. 'path = ?' => AllowedCountries::ALLOWED_COUNTRIES_PATH,
  99. 'scope_id = ?' => $scopeId,
  100. 'scope = ?' => ScopeInterface::SCOPE_WEBSITES
  101. ]
  102. );
  103. }
  104. }
  105. /**
  106. * Retrieve countries not depending on global scope
  107. *
  108. * @param string $scope
  109. * @param int $scopeCode
  110. * @return array
  111. */
  112. private function getAllowedCountries($scope, $scopeCode)
  113. {
  114. return $this->allowedCountries->makeCountriesUnique(
  115. $this->allowedCountries->getCountriesFromConfig($scope, $scopeCode)
  116. );
  117. }
  118. /**
  119. * Merge allowed countries between different scopes
  120. *
  121. * @param array $countries
  122. * @param array $newCountries
  123. * @param string $identifier
  124. * @return array
  125. */
  126. private function mergeAllowedCountries(array $countries, array $newCountries, $identifier)
  127. {
  128. if (!isset($countries[$identifier])) {
  129. $countries[$identifier] = $newCountries;
  130. } else {
  131. $countries[$identifier] = array_replace($countries[$identifier], $newCountries);
  132. }
  133. return $countries;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public static function getDependencies()
  139. {
  140. return [
  141. UpdateAutocompleteOnStorefrontConfigPath::class
  142. ];
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public static function getVersion()
  148. {
  149. return '2.0.9';
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getAliases()
  155. {
  156. return [];
  157. }
  158. }