Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MigrateStoresAllowedCountriesToWebsite.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Directory\Model\AllowedCountriesFactory;
16 
18 {
22  private $moduleDataSetup;
23 
27  private $storeManager;
28 
32  private $allowedCountries;
33 
40  public function __construct(
41  ModuleDataSetupInterface $moduleDataSetup,
42  \Magento\Store\Model\StoreManagerInterface $storeManager,
43  \Magento\Directory\Model\AllowedCountries $allowedCountries
44  ) {
45  $this->moduleDataSetup = $moduleDataSetup;
46  $this->storeManager = $storeManager;
47  $this->allowedCountries = $allowedCountries;
48  }
49 
53  public function apply()
54  {
55  $this->moduleDataSetup->getConnection()->beginTransaction();
56 
57  try {
58  $this->migrateStoresAllowedCountriesToWebsite();
59  $this->moduleDataSetup->getConnection()->commit();
60  } catch (\Exception $e) {
61  $this->moduleDataSetup->getConnection()->rollBack();
62  throw $e;
63  }
64  }
65 
71  private function migrateStoresAllowedCountriesToWebsite()
72  {
73  $allowedCountries = [];
74  //Process Websites
75  foreach ($this->storeManager->getStores() as $store) {
76  $allowedCountries = $this->mergeAllowedCountries(
77  $allowedCountries,
78  $this->getAllowedCountries(ScopeInterface::SCOPE_STORE, $store->getId()),
79  $store->getWebsiteId()
80  );
81  }
82  //Process stores
83  foreach ($this->storeManager->getWebsites() as $website) {
84  $allowedCountries = $this->mergeAllowedCountries(
85  $allowedCountries,
86  $this->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $website->getId()),
87  $website->getId()
88  );
89  }
90 
91  $connection = $this->moduleDataSetup->getConnection();
92 
93  //Remove everything from stores scope
94  $connection->delete(
95  $this->moduleDataSetup->getTable('core_config_data'),
96  [
98  'scope = ?' => ScopeInterface::SCOPE_STORES
99  ]
100  );
101 
102  //Update websites
103  foreach ($allowedCountries as $scopeId => $countries) {
104  $connection->update(
105  $this->moduleDataSetup->getTable('core_config_data'),
106  [
107  'value' => implode(',', $countries)
108  ],
109  [
111  'scope_id = ?' => $scopeId,
112  'scope = ?' => ScopeInterface::SCOPE_WEBSITES
113  ]
114  );
115  }
116  }
117 
125  private function getAllowedCountries($scope, $scopeCode)
126  {
127  return $this->allowedCountries->makeCountriesUnique(
128  $this->allowedCountries->getCountriesFromConfig($scope, $scopeCode)
129  );
130  }
131 
140  private function mergeAllowedCountries(array $countries, array $newCountries, $identifier)
141  {
142  if (!isset($countries[$identifier])) {
143  $countries[$identifier] = $newCountries;
144  } else {
145  $countries[$identifier] = array_replace($countries[$identifier], $newCountries);
146  }
147 
148  return $countries;
149  }
150 
154  public static function getDependencies()
155  {
156  return [
157  UpdateAutocompleteOnStorefrontConfigPath::class
158  ];
159  }
160 
164  public static function getVersion()
165  {
166  return '2.0.9';
167  }
168 
172  public function getAliases()
173  {
174  return [];
175  }
176 }
__construct(ModuleDataSetupInterface $moduleDataSetup, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Directory\Model\AllowedCountries $allowedCountries)
$storeManager
$connection
Definition: bulk.php:13