Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WebsiteCategoryProvider.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Setup\Fixtures;
8 
10 
15 {
19  private $categoriesPerWebsite;
20 
24  private $fixtureConfig;
25 
29  private $resourceConnection;
30 
34  private $connection;
35 
39  private $websites;
40 
44  private $categories;
45 
50  public function __construct(
51  FixtureConfig $fixtureConfig,
52  ResourceConnection $resourceConnection
53  ) {
54  $this->fixtureConfig = $fixtureConfig;
55  $this->resourceConnection = $resourceConnection;
56  }
57 
65  public function getWebsiteIds($productIndex)
66  {
67  if ($this->isAssignToAllWebsites()) {
68  return $this->getAllWebsites();
69  } else {
70  $categoriesPerWebsite = $this->getCategoriesAndWebsites();
71  if (!count($categoriesPerWebsite)) {
72  throw new \Exception('Cannot find categories. Please, be sure that you have generated categories');
73  }
74  return [$categoriesPerWebsite[$productIndex % count($categoriesPerWebsite)]['website']];
75  }
76  }
77 
84  public function getCategoryId($productIndex)
85  {
86  if ($this->isAssignToAllWebsites()) {
87  $categories = $this->getAllCategories();
88  return $categories[$productIndex % count($categories)];
89  } else {
90  $categoriesPerWebsite = $this->getCategoriesAndWebsites();
91  return $categoriesPerWebsite[$productIndex % count($categoriesPerWebsite)]['category'];
92  }
93  }
94 
98  private function getCategoriesAndWebsites()
99  {
100  if (null === $this->categoriesPerWebsite) {
101  $select = $this->getConnection()->select()
102  ->from(
103  ['c' => $this->resourceConnection->getTableName('catalog_category_entity')],
104  ['category' => 'entity_id']
105  )->join(
106  ['sg' => $this->resourceConnection->getTableName('store_group')],
107  "c.path like concat('1/', sg.root_category_id, '/%')",
108  ['website' => 'website_id']
109  );
110  $this->categoriesPerWebsite = $this->getConnection()->fetchAll($select);
111  }
112 
113  return $this->categoriesPerWebsite;
114  }
115 
119  private function isAssignToAllWebsites()
120  {
121  return (bool)$this->fixtureConfig->getValue('assign_entities_to_all_websites', false);
122  }
123 
127  private function getAllWebsites()
128  {
129  if (null === $this->websites) {
130  $this->websites = array_unique(array_column($this->getCategoriesAndWebsites(), 'website'));
131  }
132 
133  return $this->websites;
134  }
135 
139  private function getAllCategories()
140  {
141  if (null === $this->categories) {
142  $this->categories = array_values(array_unique(array_column($this->getCategoriesAndWebsites(), 'category')));
143  }
144 
145  return $this->categories;
146  }
147 
151  private function getConnection()
152  {
153  if (null === $this->connection) {
154  $this->connection = $this->resourceConnection->getConnection();
155  }
156 
157  return $this->connection;
158  }
159 }
__construct(FixtureConfig $fixtureConfig, ResourceConnection $resourceConnection)