Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StoresFixture.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Setup\Fixtures;
8 
10 use Magento\Catalog\Model\CategoryFactory;
18 
42 class StoresFixture extends Fixture
43 {
45 
47 
49 
53  protected $priority = 10;
54 
58  private $websiteIds = [];
59 
63  private $storeGroupsIds = [];
64 
68  private $storeGroupsToWebsites = [];
69 
73  private $storeManager;
74 
78  private $scopeConfig;
79 
83  private $defaultStoreGroup;
84 
88  private $defaultWebsite;
89 
93  private $defaultParentCategoryId;
94 
98  private $defaultStoreGroupId;
99 
103  private $defaultWebsiteId;
104 
108  private $storeGroupsCount;
109 
113  private $storesCount;
114 
118  private $websitesCount;
119 
123  private $singleRootCategory;
124 
128  private $defaultStoreView;
129 
133  private $storeViewIds;
134 
138  private $eventManager;
139 
143  private $categoryFactory;
144 
148  private $localeConfig;
149 
159  public function __construct(
161  StoreManager $storeManager,
162  ManagerInterface $eventManager,
163  CategoryFactory $categoryFactory,
164  Config $localeConfig,
165  Writer $scopeConfig
166  ) {
167  parent::__construct($fixtureModel);
168  $this->storeManager = $storeManager;
169  $this->eventManager = $eventManager;
170  $this->categoryFactory = $categoryFactory;
171  $this->localeConfig = $localeConfig;
172  $this->scopeConfig = $scopeConfig;
173  }
174 
179  public function execute()
180  {
181  //get settings counts
182  $this->websitesCount = $this->fixtureModel->getValue('websites', self::DEFAULT_WEBSITE_COUNT);
183  $this->storeGroupsCount = $this->fixtureModel->getValue('store_groups', self::DEFAULT_STORE_COUNT);
184  $this->storesCount = $this->fixtureModel->getValue('store_views', self::DEFAULT_STORE_VIEW_COUNT);
185  $this->singleRootCategory = (bool)$this->fixtureModel->getValue('assign_entities_to_all_websites', false);
186 
187  if ($this->websitesCount <= self::DEFAULT_WEBSITE_COUNT
188  && $this->storeGroupsCount <= self::DEFAULT_STORE_COUNT
189  && $this->storesCount <= self::DEFAULT_STORE_VIEW_COUNT
190  ) {
191  return;
192  }
193 
194  //Get existing entities counts
195  $storeGroups = $this->storeManager->getGroups();
196  $this->storeGroupsIds= array_keys($storeGroups);
197 
198  foreach ($storeGroups as $storeGroupId => $storeGroup) {
199  $this->storeGroupsToWebsites[$storeGroupId] = $storeGroup->getWebsiteId();
200  }
201 
202  $this->websiteIds = array_values(array_unique($this->storeGroupsToWebsites));
203 
204  $this->defaultWebsite = $this->storeManager->getWebsite();
205  $this->defaultStoreGroup = $this->storeManager->getGroup();
206  $this->defaultWebsiteId = $this->defaultWebsite->getId();
207  $this->defaultStoreGroupId = $this->defaultStoreGroup->getId();
208  $this->defaultStoreView = $this->storeManager->getDefaultStoreView();
209  $this->storeViewIds = array_keys($this->storeManager->getStores());
210 
211  $this->generateWebsites();
212  $this->generateStoreGroups();
213  $this->generateStoreViews();
214  }
215 
220  private function generateWebsites()
221  {
222  $existedWebsitesCount = count($this->websiteIds) + self::DEFAULT_WEBSITE_COUNT;
223 
224  while ($existedWebsitesCount <= $this->websitesCount) {
225  $website = clone $this->defaultWebsite;
226  $websiteCode = sprintf('website_%d', $existedWebsitesCount);
227  $websiteName = sprintf('Website %d', $existedWebsitesCount);
228  $website->addData(
229  [
230  'website_id' => null,
231  'code' => $websiteCode,
232  'name' => $websiteName,
233  'is_default' => false,
234  ]
235  );
236  $website->save();
237  $this->websiteIds[] = $website->getId();
238  $existedWebsitesCount++;
239  }
240  }
241 
246  private function generateStoreGroups()
247  {
248  $existedStoreGroupCount = count($this->storeGroupsIds);
249  $existedWebsitesCount = count($this->websiteIds);
250 
251  while ($existedStoreGroupCount < $this->storeGroupsCount) {
252  $websiteId = $this->websiteIds[$existedStoreGroupCount % $existedWebsitesCount];
253  $storeGroupName = sprintf('Store Group %d - website_id_%d', ++$existedStoreGroupCount, $websiteId);
254  $storeGroupCode = sprintf('store_group_%d', $existedStoreGroupCount);
255 
256  $storeGroup = clone $this->defaultStoreGroup;
257  $storeGroup->addData(
258  [
259  'group_id' => null,
260  'website_id' => $websiteId,
261  'name' => $storeGroupName,
262  'code' => $storeGroupCode,
263  'root_category_id' => $this->getStoreCategoryId($storeGroupName),
264  ]
265  );
266  $storeGroup->save();
267  $this->storeGroupsIds[] = $storeGroup->getId();
268  $this->storeGroupsToWebsites[$storeGroup->getId()] = $websiteId;
269  }
270  }
271 
276  private function generateStoreViews()
277  {
278  $localesList = $this->localeConfig->getAllowedLocales();
279  $localesListCount = count($localesList);
280 
281  $existedStoreViewsCount = count($this->storeViewIds);
282  $existedStoreGroupCount = count($this->storeGroupsIds);
283 
284  while ($existedStoreViewsCount < $this->storesCount) {
285  $groupId = $this->storeGroupsIds[$existedStoreViewsCount % $existedStoreGroupCount];
286  $websiteId = $this->storeGroupsToWebsites[$groupId];
287  $store = clone $this->defaultStoreView;
288  $storeCode = sprintf('store_view_%d', ++$existedStoreViewsCount);
289  $storeName = sprintf(
290  'Store view %d - website_id_%d - group_id_%d',
291  $existedStoreViewsCount,
292  $websiteId,
293  $groupId
294  );
295 
296  $store->addData(
297  [
298  'store_id' => null,
299  'name' => $storeName,
300  'website_id' => $websiteId,
301  'group_id' => $groupId,
302  'code' => $storeCode
303  ]
304  )->save();
305 
306  $this->saveStoreLocale($store->getId(), $localesList[$existedStoreViewsCount % $localesListCount]);
307  }
308  }
309 
315  private function saveStoreLocale($storeId, $localeCode)
316  {
317  $this->scopeConfig->save(
318  \Magento\Directory\Helper\Data::XML_PATH_DEFAULT_LOCALE,
319  $localeCode,
321  $storeId
322  );
323  }
324 
331  private function getStoreCategoryId($storeGroupName)
332  {
333  if ($this->singleRootCategory) {
334  return $this->getDefaultCategoryId();
335  } else {
336  //Generating category for store
337  $category = $this->categoryFactory->create();
338  $categoryPath = Category::TREE_ROOT_ID;
339  $category->setName("Category " . $storeGroupName)
340  ->setPath($categoryPath)
341  ->setLevel(1)
342  ->setAvailableSortBy('name')
343  ->setDefaultSortBy('name')
344  ->setIsActive(true)
345  ->save();
346 
347  return $category->getId();
348  }
349  }
350 
354  public function getActionTitle()
355  {
356  return 'Generating websites, stores and store views';
357  }
358 
362  public function introduceParamLabels()
363  {
364  return [
365  'websites' => 'Websites',
366  'store_groups' => 'Store Groups Count',
367  'store_views' => 'Store Views Count'
368  ];
369  }
370 
376  private function getDefaultCategoryId()
377  {
378  if (null === $this->defaultParentCategoryId) {
379  $this->defaultParentCategoryId = $this->storeManager->getStore()->getRootCategoryId();
380  }
381  return $this->defaultParentCategoryId;
382  }
383 }
$storeManager
$storeCode
Definition: indexer.php:15
$localeConfig
Definition: locales.php:13
$storeName
Definition: logo.phtml:13
if(!isset($_GET['website_code'])) $websiteCode
Definition: website.php:11
__construct(FixtureModel $fixtureModel, StoreManager $storeManager, ManagerInterface $eventManager, CategoryFactory $categoryFactory, Config $localeConfig, Writer $scopeConfig)