1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Setup\Fixtures;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Store\Model\StoreManager;
/**
* Generate categories.
* Support the following format:
* <categories>{amount of categories}</categories>
* <categories_nesting_level>{Nesting level of categories}</categories_nesting_level>
*
* If config "assign_entities_to_all_websites" set to "0" then all categories will be
* uniformly distributed per root categories, else all categories assigned to one root category
*/
class CategoriesFixture extends Fixture
{
/**
* @var StoreManager
*/
private $storeManager;
/**
* @var CategoryFactory
*/
private $categoryFactory;
/**
* @var CollectionFactory
*/
private $collectionFactory;
/**
* @var int
*/
private $firstLevelCategoryIndex;
/**
* @var array
*/
private $rootCategoriesIds;
/**
* @var int
*/
private $categoriesNumber;
/**
* @var int
*/
private $maxNestingLevel;
/**
* CategoriesFixture constructor.
* @param FixtureModel $fixtureModel
* @param StoreManager $storeManager
* @param CategoryFactory $categoryFactory
* @param CollectionFactory $collectionFactory
*/
public function __construct(
FixtureModel $fixtureModel,
StoreManager $storeManager,
CategoryFactory $categoryFactory,
CollectionFactory $collectionFactory
) {
parent::__construct($fixtureModel);
$this->storeManager = $storeManager;
$this->categoryFactory = $categoryFactory;
$this->collectionFactory = $collectionFactory;
}
/**
* @var int
*/
protected $priority = 20;
/**
* {@inheritdoc}
*/
public function execute()
{
$this->categoriesNumber = $this->getCategoriesAmount();
if (!$this->categoriesNumber) {
return;
}
$this->maxNestingLevel = $this->fixtureModel->getValue('categories_nesting_level', 3);
$categoriesNumberOnLevel = abs(ceil(pow($this->categoriesNumber, 1 / $this->maxNestingLevel) - 2));
foreach ($this->getRootCategoriesIds() as $parentCategoryId) {
$category = $this->categoryFactory->create();
$category->load($parentCategoryId);
// Need for generation url rewrites per all category store view
$category->setStoreId(\Magento\Store\Model\Store::DEFAULT_STORE_ID);
$categoryIndex = 1;
$this->generateCategories(
$category,
$categoriesNumberOnLevel,
1,
$categoryIndex
);
}
}
/**
* Generate categories
*
* @param Category $parentCategory
* @param int $categoriesNumberOnLevel
* @param int $nestingLevel
* @param int $categoryIndex
* @return void
*/
private function generateCategories(
Category $parentCategory,
$categoriesNumberOnLevel,
$nestingLevel,
&$categoryIndex
) {
$maxCategoriesNumberOnLevel = $nestingLevel === 1 ? $this->categoriesNumber : $categoriesNumberOnLevel;
for ($i = 0; $i < $maxCategoriesNumberOnLevel && $categoryIndex <= $this->categoriesNumber; $i++) {
try {
$category = clone $parentCategory;
$category->setId(null)
->setUrlKey(null)
->setUrlPath(null)
->setName($this->getCategoryName($parentCategory, $nestingLevel, $i))
->setParentId($parentCategory->getId())
->setLevel($parentCategory->getLevel() + 1)
->setAvailableSortBy('name')
->setIsAnchor($nestingLevel <= 2)
->setDefaultSortBy('name')
->setIsActive(true);
$category->save();
$categoryIndex++;
if ($nestingLevel < $this->maxNestingLevel) {
$this->generateCategories(
$category,
$categoriesNumberOnLevel,
$nestingLevel + 1,
$categoryIndex
);
}
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
$categoryIndex++;
continue;
} catch (\Magento\Framework\DB\Adapter\DuplicateException $e) {
$categoryIndex++;
continue;
}
}
}
/**
* Get category name based on parent category and current level
*
* @param Category $parentCategory
* @param int $nestingLevel
* @param int $index
* @return string
*/
private function getCategoryName($parentCategory, $nestingLevel, $index)
{
$categoryNameSuffix = $nestingLevel === 1 ? $this->getFirstLevelCategoryIndex() + $index : $index + 1;
return ($nestingLevel === 1 ? $this->getCategoryPrefix() . ' ' : $parentCategory->getName() . '.')
. $categoryNameSuffix;
}
/**
* Get ids of root categories
*
* @return int[]
*/
private function getRootCategoriesIds()
{
if (null === $this->rootCategoriesIds) {
$this->rootCategoriesIds = [];
foreach ($this->storeManager->getGroups() as $storeGroup) {
$this->rootCategoriesIds[] = $storeGroup->getRootCategoryId();
// in this case root category will be the same for all store groups
if ((bool)$this->fixtureModel->getValue('assign_entities_to_all_websites', false)) {
break;
}
}
}
return $this->rootCategoriesIds;
}
/**
* Get categories amount for generation
*
* @return int
*/
private function getCategoriesAmount()
{
$categoriesAmount = $this->collectionFactory->create()->getSize();
$rootCategories = count($this->getRootCategoriesIds());
$categoriesNumber = $this->fixtureModel->getValue('categories', 0) - ($categoriesAmount - $rootCategories - 1);
return max(
0,
ceil($categoriesNumber / $rootCategories)
);
}
/**
* Get next category index, which will be used as index of first-level category
*
* @return int
*/
private function getFirstLevelCategoryIndex()
{
if (null === $this->firstLevelCategoryIndex) {
$this->firstLevelCategoryIndex = $this->collectionFactory->create()
->addFieldToFilter('level', 2)
->getSize() + 1;
}
return $this->firstLevelCategoryIndex;
}
/**
* Get Category name prefix
*
* @return string
*/
private function getCategoryPrefix()
{
return 'Category';
}
/**
* {@inheritdoc}
*/
public function getActionTitle()
{
return 'Generating categories';
}
/**
* {@inheritdoc}
*/
public function introduceParamLabels()
{
return [
'categories' => 'Categories'
];
}
}