10 use Magento\Catalog\Model\CategoryFactory;
11 use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
28 private $storeManager;
33 private $categoryFactory;
38 private $collectionFactory;
43 private $firstLevelCategoryIndex;
48 private $rootCategoriesIds;
53 private $categoriesNumber;
58 private $maxNestingLevel;
70 CategoryFactory $categoryFactory,
71 CollectionFactory $collectionFactory
75 $this->categoryFactory = $categoryFactory;
76 $this->collectionFactory = $collectionFactory;
89 $this->categoriesNumber = $this->getCategoriesAmount();
90 if (!$this->categoriesNumber) {
93 $this->maxNestingLevel = $this->fixtureModel->getValue(
'categories_nesting_level', 3);
95 $categoriesNumberOnLevel = abs(ceil(pow($this->categoriesNumber, 1 / $this->maxNestingLevel) - 2));
96 foreach ($this->getRootCategoriesIds() as $parentCategoryId) {
97 $category = $this->categoryFactory->create();
102 $this->generateCategories(
104 $categoriesNumberOnLevel,
120 private function generateCategories(
122 $categoriesNumberOnLevel,
126 $maxCategoriesNumberOnLevel = $nestingLevel === 1 ? $this->categoriesNumber : $categoriesNumberOnLevel;
127 for (
$i = 0;
$i < $maxCategoriesNumberOnLevel && $categoryIndex <= $this->categoriesNumber;
$i++) {
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')
142 if ($nestingLevel < $this->maxNestingLevel) {
143 $this->generateCategories(
145 $categoriesNumberOnLevel,
150 }
catch (\
Magento\Framework\Exception\AlreadyExistsException $e) {
153 }
catch (\
Magento\Framework\DB\Adapter\DuplicateException $e) {
168 private function getCategoryName($parentCategory, $nestingLevel,
$index)
170 $categoryNameSuffix = $nestingLevel === 1 ? $this->getFirstLevelCategoryIndex() +
$index :
$index + 1;
171 return ($nestingLevel === 1 ? $this->getCategoryPrefix() .
' ' : $parentCategory->getName() .
'.')
172 . $categoryNameSuffix;
180 private function getRootCategoriesIds()
182 if (
null === $this->rootCategoriesIds) {
183 $this->rootCategoriesIds = [];
184 foreach ($this->storeManager->getGroups() as
$storeGroup) {
185 $this->rootCategoriesIds[] =
$storeGroup->getRootCategoryId();
187 if ((
bool)$this->fixtureModel->getValue(
'assign_entities_to_all_websites',
false)) {
193 return $this->rootCategoriesIds;
201 private function getCategoriesAmount()
203 $categoriesAmount = $this->collectionFactory->create()->getSize();
204 $rootCategories = count($this->getRootCategoriesIds());
205 $categoriesNumber = $this->fixtureModel->getValue(
'categories', 0) - ($categoriesAmount - $rootCategories - 1);
209 ceil($categoriesNumber / $rootCategories)
218 private function getFirstLevelCategoryIndex()
220 if (
null === $this->firstLevelCategoryIndex) {
221 $this->firstLevelCategoryIndex = $this->collectionFactory->create()
222 ->addFieldToFilter(
'level', 2)
226 return $this->firstLevelCategoryIndex;
234 private function getCategoryPrefix()
244 return 'Generating categories';
253 'categories' =>
'Categories'
__construct(FixtureModel $fixtureModel, StoreManager $storeManager, CategoryFactory $categoryFactory, CollectionFactory $collectionFactory)