Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoriesFixture.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Setup\Fixtures;
8 
10 use Magento\Catalog\Model\CategoryFactory;
11 use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
13 
24 {
28  private $storeManager;
29 
33  private $categoryFactory;
34 
38  private $collectionFactory;
39 
43  private $firstLevelCategoryIndex;
44 
48  private $rootCategoriesIds;
49 
53  private $categoriesNumber;
54 
58  private $maxNestingLevel;
59 
67  public function __construct(
69  StoreManager $storeManager,
70  CategoryFactory $categoryFactory,
71  CollectionFactory $collectionFactory
72  ) {
73  parent::__construct($fixtureModel);
74  $this->storeManager = $storeManager;
75  $this->categoryFactory = $categoryFactory;
76  $this->collectionFactory = $collectionFactory;
77  }
78 
82  protected $priority = 20;
83 
87  public function execute()
88  {
89  $this->categoriesNumber = $this->getCategoriesAmount();
90  if (!$this->categoriesNumber) {
91  return;
92  }
93  $this->maxNestingLevel = $this->fixtureModel->getValue('categories_nesting_level', 3);
94 
95  $categoriesNumberOnLevel = abs(ceil(pow($this->categoriesNumber, 1 / $this->maxNestingLevel) - 2));
96  foreach ($this->getRootCategoriesIds() as $parentCategoryId) {
97  $category = $this->categoryFactory->create();
98  $category->load($parentCategoryId);
99  // Need for generation url rewrites per all category store view
100  $category->setStoreId(\Magento\Store\Model\Store::DEFAULT_STORE_ID);
101  $categoryIndex = 1;
102  $this->generateCategories(
103  $category,
104  $categoriesNumberOnLevel,
105  1,
106  $categoryIndex
107  );
108  }
109  }
110 
120  private function generateCategories(
121  Category $parentCategory,
122  $categoriesNumberOnLevel,
123  $nestingLevel,
124  &$categoryIndex
125  ) {
126  $maxCategoriesNumberOnLevel = $nestingLevel === 1 ? $this->categoriesNumber : $categoriesNumberOnLevel;
127  for ($i = 0; $i < $maxCategoriesNumberOnLevel && $categoryIndex <= $this->categoriesNumber; $i++) {
128  try {
129  $category = clone $parentCategory;
130  $category->setId(null)
131  ->setUrlKey(null)
132  ->setUrlPath(null)
133  ->setName($this->getCategoryName($parentCategory, $nestingLevel, $i))
134  ->setParentId($parentCategory->getId())
135  ->setLevel($parentCategory->getLevel() + 1)
136  ->setAvailableSortBy('name')
137  ->setIsAnchor($nestingLevel <= 2)
138  ->setDefaultSortBy('name')
139  ->setIsActive(true);
140  $category->save();
141  $categoryIndex++;
142  if ($nestingLevel < $this->maxNestingLevel) {
143  $this->generateCategories(
144  $category,
145  $categoriesNumberOnLevel,
146  $nestingLevel + 1,
147  $categoryIndex
148  );
149  }
150  } catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
151  $categoryIndex++;
152  continue;
153  } catch (\Magento\Framework\DB\Adapter\DuplicateException $e) {
154  $categoryIndex++;
155  continue;
156  }
157  }
158  }
159 
168  private function getCategoryName($parentCategory, $nestingLevel, $index)
169  {
170  $categoryNameSuffix = $nestingLevel === 1 ? $this->getFirstLevelCategoryIndex() + $index : $index + 1;
171  return ($nestingLevel === 1 ? $this->getCategoryPrefix() . ' ' : $parentCategory->getName() . '.')
172  . $categoryNameSuffix;
173  }
174 
180  private function getRootCategoriesIds()
181  {
182  if (null === $this->rootCategoriesIds) {
183  $this->rootCategoriesIds = [];
184  foreach ($this->storeManager->getGroups() as $storeGroup) {
185  $this->rootCategoriesIds[] = $storeGroup->getRootCategoryId();
186  // in this case root category will be the same for all store groups
187  if ((bool)$this->fixtureModel->getValue('assign_entities_to_all_websites', false)) {
188  break;
189  }
190  }
191  }
192 
193  return $this->rootCategoriesIds;
194  }
195 
201  private function getCategoriesAmount()
202  {
203  $categoriesAmount = $this->collectionFactory->create()->getSize();
204  $rootCategories = count($this->getRootCategoriesIds());
205  $categoriesNumber = $this->fixtureModel->getValue('categories', 0) - ($categoriesAmount - $rootCategories - 1);
206 
207  return max(
208  0,
209  ceil($categoriesNumber / $rootCategories)
210  );
211  }
212 
218  private function getFirstLevelCategoryIndex()
219  {
220  if (null === $this->firstLevelCategoryIndex) {
221  $this->firstLevelCategoryIndex = $this->collectionFactory->create()
222  ->addFieldToFilter('level', 2)
223  ->getSize() + 1;
224  }
225 
226  return $this->firstLevelCategoryIndex;
227  }
228 
234  private function getCategoryPrefix()
235  {
236  return 'Category';
237  }
238 
242  public function getActionTitle()
243  {
244  return 'Generating categories';
245  }
246 
250  public function introduceParamLabels()
251  {
252  return [
253  'categories' => 'Categories'
254  ];
255  }
256 }
$storeManager
__construct(FixtureModel $fixtureModel, StoreManager $storeManager, CategoryFactory $categoryFactory, CollectionFactory $collectionFactory)
$i
Definition: gallery.phtml:31
$index
Definition: list.phtml:44