1 | <?php |
||
37 | class ProductExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface |
||
38 | { |
||
39 | /** |
||
40 | * @var FactoryInterface |
||
41 | */ |
||
42 | private $productFactory; |
||
43 | |||
44 | /** |
||
45 | * @var FactoryInterface |
||
46 | */ |
||
47 | private $productVariantFactory; |
||
48 | |||
49 | /** |
||
50 | * @var FactoryInterface |
||
51 | */ |
||
52 | private $channelPricingFactory; |
||
53 | |||
54 | /** |
||
55 | * @var FactoryInterface |
||
56 | */ |
||
57 | private $productTaxonFactory; |
||
58 | |||
59 | /** |
||
60 | * @var ProductVariantGeneratorInterface |
||
61 | */ |
||
62 | private $variantGenerator; |
||
63 | |||
64 | /** |
||
65 | * @var FactoryInterface |
||
66 | */ |
||
67 | private $productAttributeValueFactory; |
||
68 | |||
69 | /** |
||
70 | * @var FactoryInterface |
||
71 | */ |
||
72 | private $productImageFactory; |
||
73 | |||
74 | /** |
||
75 | * @var ImageUploaderInterface |
||
76 | */ |
||
77 | private $imageUploader; |
||
78 | |||
79 | /** |
||
80 | * @var SlugGeneratorInterface |
||
81 | */ |
||
82 | private $slugGenerator; |
||
83 | |||
84 | /** |
||
85 | * @var RepositoryInterface |
||
86 | */ |
||
87 | private $taxonRepository; |
||
88 | |||
89 | /** |
||
90 | * @var RepositoryInterface |
||
91 | */ |
||
92 | private $productAttributeRepository; |
||
93 | |||
94 | /** |
||
95 | * @var RepositoryInterface |
||
96 | */ |
||
97 | private $productOptionRepository; |
||
98 | |||
99 | /** |
||
100 | * @var RepositoryInterface |
||
101 | */ |
||
102 | private $channelRepository; |
||
103 | |||
104 | /** |
||
105 | * @var RepositoryInterface |
||
106 | */ |
||
107 | private $localeRepository; |
||
108 | |||
109 | /** |
||
110 | * @var \Faker\Generator |
||
111 | */ |
||
112 | private $faker; |
||
113 | |||
114 | /** |
||
115 | * @var OptionsResolver |
||
116 | */ |
||
117 | private $optionsResolver; |
||
118 | |||
119 | /** |
||
120 | * @param FactoryInterface $productFactory |
||
121 | * @param FactoryInterface $productVariantFactory |
||
122 | * @param FactoryInterface $channelPricing |
||
123 | * @param ProductVariantGeneratorInterface $variantGenerator |
||
124 | * @param FactoryInterface $productAttributeValueFactory |
||
125 | * @param FactoryInterface $productImageFactory |
||
126 | * @param FactoryInterface $productTaxonFactory |
||
127 | * @param ImageUploaderInterface $imageUploader |
||
128 | * @param SlugGeneratorInterface $slugGenerator |
||
129 | * @param RepositoryInterface $taxonRepository |
||
130 | * @param RepositoryInterface $productAttributeRepository |
||
131 | * @param RepositoryInterface $productOptionRepository |
||
132 | * @param RepositoryInterface $channelRepository |
||
133 | * @param RepositoryInterface $localeRepository |
||
134 | */ |
||
135 | public function __construct( |
||
136 | FactoryInterface $productFactory, |
||
137 | FactoryInterface $productVariantFactory, |
||
138 | FactoryInterface $channelPricing, |
||
139 | ProductVariantGeneratorInterface $variantGenerator, |
||
140 | FactoryInterface $productAttributeValueFactory, |
||
141 | FactoryInterface $productImageFactory, |
||
142 | FactoryInterface $productTaxonFactory, |
||
143 | ImageUploaderInterface $imageUploader, |
||
144 | SlugGeneratorInterface $slugGenerator, |
||
145 | RepositoryInterface $taxonRepository, |
||
146 | RepositoryInterface $productAttributeRepository, |
||
147 | RepositoryInterface $productOptionRepository, |
||
148 | RepositoryInterface $channelRepository, |
||
149 | RepositoryInterface $localeRepository |
||
150 | ) { |
||
151 | $this->productFactory = $productFactory; |
||
152 | $this->productVariantFactory = $productVariantFactory; |
||
153 | $this->channelPricingFactory = $channelPricing; |
||
154 | $this->variantGenerator = $variantGenerator; |
||
155 | $this->productAttributeValueFactory = $productAttributeValueFactory; |
||
156 | $this->productImageFactory = $productImageFactory; |
||
157 | $this->productTaxonFactory = $productTaxonFactory; |
||
158 | $this->imageUploader = $imageUploader; |
||
159 | $this->slugGenerator = $slugGenerator; |
||
160 | $this->taxonRepository = $taxonRepository; |
||
161 | $this->productAttributeRepository = $productAttributeRepository; |
||
162 | $this->productOptionRepository = $productOptionRepository; |
||
163 | $this->channelRepository = $channelRepository; |
||
164 | $this->localeRepository = $localeRepository; |
||
165 | |||
166 | $this->faker = \Faker\Factory::create(); |
||
167 | $this->optionsResolver = new OptionsResolver(); |
||
168 | |||
169 | $this->configureOptions($this->optionsResolver); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | public function create(array $options = []): ProductInterface |
||
176 | { |
||
177 | $options = $this->optionsResolver->resolve($options); |
||
178 | |||
179 | /** @var ProductInterface $product */ |
||
180 | $product = $this->productFactory->createNew(); |
||
181 | $product->setVariantSelectionMethod($options['variant_selection_method']); |
||
182 | $product->setCode($options['code']); |
||
183 | $product->setEnabled($options['enabled']); |
||
184 | $product->setMainTaxon($options['main_taxon']); |
||
185 | $product->setCreatedAt($this->faker->dateTimeBetween('-1 week', 'now')); |
||
186 | |||
187 | $this->createTranslations($product, $options); |
||
188 | $this->createRelations($product, $options); |
||
189 | $this->createVariants($product, $options); |
||
190 | $this->createImages($product, $options); |
||
191 | $this->createProductTaxons($product, $options); |
||
192 | |||
193 | return $product; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | protected function configureOptions(OptionsResolver $resolver): void |
||
200 | { |
||
201 | $resolver |
||
202 | ->setDefault('name', function (Options $options): string { |
||
|
|||
203 | return $this->faker->words(3, true); |
||
204 | }) |
||
205 | |||
206 | ->setDefault('code', function (Options $options): string { |
||
207 | return StringInflector::nameToCode($options['name']); |
||
208 | }) |
||
209 | |||
210 | ->setDefault('enabled', function (Options $options): bool { |
||
211 | return $this->faker->boolean(90); |
||
212 | }) |
||
213 | ->setAllowedTypes('enabled', 'bool') |
||
214 | |||
215 | ->setDefault('short_description', function (Options $options): string { |
||
216 | return $this->faker->paragraph; |
||
217 | }) |
||
218 | |||
219 | ->setDefault('description', function (Options $options): string { |
||
220 | return $this->faker->paragraphs(3, true); |
||
221 | }) |
||
222 | |||
223 | ->setDefault('main_taxon', LazyOption::randomOne($this->taxonRepository)) |
||
224 | ->setAllowedTypes('main_taxon', ['null', 'string', TaxonInterface::class]) |
||
225 | ->setNormalizer('main_taxon', LazyOption::findOneBy($this->taxonRepository, 'code')) |
||
226 | |||
227 | ->setDefault('taxons', LazyOption::randomOnes($this->taxonRepository, 3)) |
||
228 | ->setAllowedTypes('taxons', 'array') |
||
229 | ->setNormalizer('taxons', LazyOption::findBy($this->taxonRepository, 'code')) |
||
230 | |||
231 | ->setDefault('channels', LazyOption::randomOnes($this->channelRepository, 3)) |
||
232 | ->setAllowedTypes('channels', 'array') |
||
233 | ->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code')) |
||
234 | |||
235 | ->setDefault('variant_selection_method', ProductInterface::VARIANT_SELECTION_MATCH) |
||
236 | ->setAllowedValues('variant_selection_method', [ProductInterface::VARIANT_SELECTION_MATCH, ProductInterface::VARIANT_SELECTION_CHOICE]) |
||
237 | |||
238 | ->setDefault('product_attributes', []) |
||
239 | ->setAllowedTypes('product_attributes', 'array') |
||
240 | ->setNormalizer('product_attributes', function (Options $options, array $productAttributes): array { |
||
241 | $productAttributesValues = []; |
||
242 | foreach ($productAttributes as $code => $value) { |
||
243 | foreach ($this->getLocales() as $localeCode) { |
||
244 | /** @var ProductAttributeInterface $productAttribute */ |
||
245 | $productAttribute = $this->productAttributeRepository->findOneBy(['code' => $code]); |
||
246 | |||
247 | Assert::notNull($productAttribute); |
||
248 | |||
249 | /** @var ProductAttributeValueInterface $productAttributeValue */ |
||
250 | $productAttributeValue = $this->productAttributeValueFactory->createNew(); |
||
251 | $productAttributeValue->setAttribute($productAttribute); |
||
252 | $productAttributeValue->setValue($value ?: $this->getRandomValueForProductAttribute($productAttribute)); |
||
253 | $productAttributeValue->setLocaleCode($localeCode); |
||
254 | |||
255 | $productAttributesValues[] = $productAttributeValue; |
||
256 | } |
||
257 | } |
||
258 | |||
259 | return $productAttributesValues; |
||
260 | }) |
||
261 | |||
262 | ->setDefault('product_options', []) |
||
263 | ->setAllowedTypes('product_options', 'array') |
||
264 | ->setNormalizer('product_options', LazyOption::findBy($this->productOptionRepository, 'code')) |
||
265 | |||
266 | ->setDefault('images', []) |
||
267 | ->setAllowedTypes('images', 'array') |
||
268 | |||
269 | ->setDefault('shipping_required', true) |
||
270 | ; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @param ProductInterface $product |
||
275 | * @param array $options |
||
276 | */ |
||
277 | private function createTranslations(ProductInterface $product, array $options): void |
||
289 | |||
290 | /** |
||
291 | * @param ProductInterface $product |
||
292 | * @param array $options |
||
293 | */ |
||
294 | private function createRelations(ProductInterface $product, array $options): void |
||
308 | |||
309 | /** |
||
310 | * @param ProductInterface $product |
||
311 | * @param array $options |
||
312 | */ |
||
313 | private function createVariants(ProductInterface $product, array $options): void |
||
339 | |||
340 | /** |
||
341 | * @param ProductVariantInterface $productVariant |
||
342 | * @param string $channelCode |
||
343 | */ |
||
344 | private function createChannelPricings(ProductVariantInterface $productVariant, string $channelCode): void |
||
353 | |||
354 | /** |
||
355 | * @param ProductInterface $product |
||
356 | * @param array $options |
||
357 | */ |
||
358 | private function createImages(ProductInterface $product, array $options): void |
||
374 | |||
375 | /** |
||
376 | * @param ProductInterface $product |
||
377 | * @param array $options |
||
378 | */ |
||
379 | private function createProductTaxons(ProductInterface $product, array $options): void |
||
389 | |||
390 | /** |
||
391 | * @return iterable |
||
392 | */ |
||
393 | private function getLocales(): iterable |
||
401 | |||
402 | /** |
||
403 | * @param ProductAttributeInterface $productAttribute |
||
404 | * |
||
405 | * @return mixed |
||
406 | * |
||
407 | * @throws \BadMethodCallException |
||
408 | */ |
||
409 | private function getRandomValueForProductAttribute(ProductAttributeInterface $productAttribute) |
||
439 | } |
||
440 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.