Completed
Push — symfony3-fqcn-sylius ( 26083b...02605f )
by Kamil
20:07
created

ProductContext::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 31
nc 1
nop 15

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Behat\Gherkin\Node\TableNode;
16
use Behat\Mink\Element\NodeElement;
17
use Doctrine\Common\Persistence\ObjectManager;
18
use Sylius\Component\Attribute\Factory\AttributeFactoryInterface;
19
use Sylius\Component\Core\Formatter\StringInflector;
20
use Sylius\Component\Core\Model\ChannelInterface;
21
use Sylius\Component\Core\Model\ImageInterface;
22
use Sylius\Component\Core\Model\ProductInterface;
23
use Sylius\Component\Core\Model\ProductTranslationInterface;
24
use Sylius\Component\Core\Model\ProductVariantInterface;
25
use Sylius\Component\Core\Pricing\Calculators;
26
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
27
use Sylius\Behat\Service\SharedStorageInterface;
28
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
29
use Sylius\Component\Product\Factory\ProductFactoryInterface;
30
use Sylius\Component\Product\Generator\SlugGeneratorInterface;
31
use Sylius\Component\Product\Model\ProductAttributeInterface;
32
use Sylius\Component\Product\Model\ProductAttributeValueInterface;
33
use Sylius\Component\Product\Model\ProductOptionInterface;
34
use Sylius\Component\Product\Model\ProductOptionValueInterface;
35
use Sylius\Component\Resource\Factory\FactoryInterface;
36
use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
37
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
38
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface;
39
use Symfony\Component\HttpFoundation\File\UploadedFile;
40
41
/**
42
 * @author Arkadiusz Krakowiak <[email protected]>
43
 * @author Mateusz Zalewski <[email protected]>
44
 * @author Magdalena Banasiak <[email protected]>
45
 */
46
final class ProductContext implements Context
47
{
48
    /**
49
     * @var SharedStorageInterface
50
     */
51
    private $sharedStorage;
52
53
    /**
54
     * @var ProductRepositoryInterface
55
     */
56
    private $productRepository;
57
58
    /**
59
     * @var ProductFactoryInterface
60
     */
61
    private $productFactory;
62
63
    /**
64
     * @var FactoryInterface
65
     */
66
    private $productTranslationFactory;
67
68
    /**
69
     * @var AttributeFactoryInterface
70
     */
71
    private $productAttributeFactory;
72
73
    /**
74
     * @var FactoryInterface
75
     */
76
    private $productVariantFactory;
77
78
    /**
79
     * @var FactoryInterface
80
     */
81
    private $attributeValueFactory;
82
83
    /**
84
     * @var FactoryInterface
85
     */
86
    private $productOptionFactory;
87
88
    /**
89
     * @var FactoryInterface
90
     */
91
    private $productOptionValueFactory;
92
93
    /**
94
     * @var FactoryInterface
95
     */
96
    private $productImageFactory;
97
98
    /**
99
     * @var ObjectManager
100
     */
101
    private $objectManager;
102
103
    /**
104
     * @var ProductVariantResolverInterface
105
     */
106
    private $defaultVariantResolver;
107
108
    /**
109
     * @var ImageUploaderInterface
110
     */
111
    private $imageUploader;
112
113
    /**
114
     * @var SlugGeneratorInterface
115
     */
116
    private $slugGenerator;
117
118
    /**
119
     * @var array
120
     */
121
    private $minkParameters;
122
123
    /**
124
     * @param SharedStorageInterface $sharedStorage
125
     * @param ProductRepositoryInterface $productRepository
126
     * @param ProductFactoryInterface $productFactory
127
     * @param FactoryInterface $productTranslationFactory
128
     * @param AttributeFactoryInterface $productAttributeFactory
129
     * @param FactoryInterface $attributeValueFactory
130
     * @param FactoryInterface $productVariantFactory
131
     * @param FactoryInterface $productOptionFactory
132
     * @param FactoryInterface $productOptionValueFactory
133
     * @param FactoryInterface $productImageFactory
134
     * @param ObjectManager $objectManager
135
     * @param ProductVariantResolverInterface $defaultVariantResolver
136
     * @param ImageUploaderInterface $imageUploader
137
     * @param SlugGeneratorInterface $slugGenerator
138
     * @param array $minkParameters
139
     */
140
    public function __construct(
141
        SharedStorageInterface $sharedStorage,
142
        ProductRepositoryInterface $productRepository,
143
        ProductFactoryInterface $productFactory,
144
        FactoryInterface $productTranslationFactory,
145
        AttributeFactoryInterface $productAttributeFactory,
146
        FactoryInterface $attributeValueFactory,
147
        FactoryInterface $productVariantFactory,
148
        FactoryInterface $productOptionFactory,
149
        FactoryInterface $productOptionValueFactory,
150
        FactoryInterface $productImageFactory,
151
        ObjectManager $objectManager,
152
        ProductVariantResolverInterface $defaultVariantResolver,
153
        ImageUploaderInterface $imageUploader,
154
        SlugGeneratorInterface $slugGenerator,
155
        array $minkParameters
156
    ) {
157
        $this->sharedStorage = $sharedStorage;
158
        $this->productRepository = $productRepository;
159
        $this->productFactory = $productFactory;
160
        $this->productTranslationFactory = $productTranslationFactory;
161
        $this->productAttributeFactory = $productAttributeFactory;
162
        $this->attributeValueFactory = $attributeValueFactory;
163
        $this->productVariantFactory = $productVariantFactory;
164
        $this->productOptionFactory = $productOptionFactory;
165
        $this->productOptionValueFactory = $productOptionValueFactory;
166
        $this->productImageFactory = $productImageFactory;
167
        $this->objectManager = $objectManager;
168
        $this->defaultVariantResolver = $defaultVariantResolver;
169
        $this->imageUploader = $imageUploader;
170
        $this->slugGenerator = $slugGenerator;
171
        $this->minkParameters = $minkParameters;
172
    }
173
174
    /**
175
     * @Given the store has a product :productName
176
     * @Given the store has a :productName product
177
     * @Given I added a product :productName
178
     * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+")$/
179
     */
180
    public function storeHasAProductPricedAt($productName, $price = 0)
181
    {
182
        $product = $this->createProduct($productName, $price);
183
184
        $product->setDescription('Awesome '.$productName);
185
186
        if ($this->sharedStorage->has('channel')) {
187
            $product->addChannel($this->sharedStorage->get('channel'));
188
        }
189
190
        $this->saveProduct($product);
191
    }
192
193
    /**
194
     * @Given the store( also) has a product :productName with code :code
195
     * @Given the store( also) has a product :productName with code :code, created at :date
196
     */
197
    public function storeHasProductWithCode($productName, $code, $date = null)
198
    {
199
        $product = $this->createProduct($productName, 0, $date);
200
201
        $product->setCode($code);
202
203
        if ($this->sharedStorage->has('channel')) {
204
            $product->addChannel($this->sharedStorage->get('channel'));
205
        }
206
207
        $this->saveProduct($product);
208
    }
209
210
    /**
211
     * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+") available in (channel "[^"]+") and (channel "[^"]+")$/
212
     */
213
    public function storeHasAProductPricedAtAvailableInChannels($productName, $price = 0, ...$channels)
214
    {
215
        $product = $this->createProduct($productName, $price);
216
217
        $product->setDescription('Awesome '.$productName);
218
219
        foreach ($channels as $channel) {
220
            $product->addChannel($channel);
221
        }
222
223
        $this->saveProduct($product);
224
    }
225
226
    /**
227
     * @Given /^(this product) is named "([^"]+)" (in the "([^"]+)" locale)$/
228
     * @Given /^the (product "[^"]+") is named "([^"]+)" (in the "([^"]+)" locale)$/
229
     */
230
    public function thisProductIsNamedIn(ProductInterface $product, $name, $locale)
231
    {
232
        /** @var ProductTranslationInterface $translation */
233
        $translation = $this->productTranslationFactory->createNew();
234
        $translation->setLocale($locale);
235
        $translation->setName($name);
236
        $translation->setSlug($this->slugGenerator->generate($name));
237
238
        $product->addTranslation($translation);
0 ignored issues
show
Documentation introduced by
$translation is of type object<Sylius\Component\...ctTranslationInterface>, but the function expects a object<Sylius\Component\...l\TranslationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
239
240
        $this->objectManager->flush();
241
    }
242
243
    /**
244
     * @Given the store has a :productName configurable product
245
     */
246
    public function storeHasAConfigurableProduct($productName)
247
    {
248
        /** @var ProductInterface $product */
249
        $product = $this->productFactory->createNew();
250
251
        $product->setName($productName);
252
        $product->setCode(StringInflector::nameToUppercaseCode($productName));
253
        $product->setSlug($this->slugGenerator->generate($productName));
254
255
        $product->setDescription('Awesome '.$productName);
256
257
        if ($this->sharedStorage->has('channel')) {
258
            $channel = $this->sharedStorage->get('channel');
259
            $product->addChannel($channel);
260
        }
261
262
        $this->saveProduct($product);
263
    }
264
265
    /**
266
     * @Given the store has( also) :firstProductName and :secondProductName products
267
     * @Given the store has( also) :firstProductName, :secondProductName and :thirdProductName products
268
     * @Given the store has( also) :firstProductName, :secondProductName, :thirdProductName and :fourthProductName products
269
     */
270
    public function theStoreHasProducts(...$productsNames)
271
    {
272
        foreach ($productsNames as $productName) {
273
            $this->saveProduct($this->createProduct($productName));
274
        }
275
    }
276
277
    /**
278
     * @Given /^(this channel) has "([^"]+)", "([^"]+)", "([^"]+)" and "([^"]+)" products$/
279
     */
280
    public function thisChannelHasProducts(ChannelInterface $channel, ...$productsNames)
281
    {
282
        foreach ($productsNames as $productName) {
283
            $product = $this->createProduct($productName);
284
            $product->addChannel($channel);
285
286
            $this->saveProduct($product);
287
        }
288
    }
289
290
    /**
291
     * @Given /^the (product "[^"]+") has(?:| a) "([^"]+)" variant priced at ("[^"]+")$/
292
     * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+")$/
293
     */
294
    public function theProductHasVariantPricedAt(ProductInterface $product, $productVariantName, $price)
295
    {
296
        $this->createProductVariant(
297
            $product,
298
            $productVariantName,
299
            $price,
300
            StringInflector::nameToUppercaseCode($productVariantName)
301
        );
302
    }
303
304
    /**
305
     * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") identified by "([^"]+)"$/
306
     */
307
    public function theProductHasVariantPricedAtIdentifiedBy(
308
        ProductInterface $product,
309
        $productVariantName,
310
        $price,
311
        $code
312
    ) {
313
        $this->createProductVariant($product, $productVariantName, $price, $code);
314
    }
315
316
    /**
317
     * @Given /^there is product "([^"]+)" available in ((?:this|that|"[^"]+") channel)$/
318
     * @Given /^the store has a product "([^"]+)" available in ("([^"]+)" channel)$/
319
     */
320
    public function thereIsProductAvailableInGivenChannel($productName, ChannelInterface $channel)
321
    {
322
        $product = $this->createProduct($productName);
323
324
        $product->setDescription('Awesome ' . $productName);
325
        $product->addChannel($channel);
326
327
        $this->saveProduct($product);
328
    }
329
330
    /**
331
     * @Given /^([^"]+) belongs to ("[^"]+" tax category)$/
332
     */
333
    public function productBelongsToTaxCategory(ProductInterface $product, TaxCategoryInterface $taxCategory)
334
    {
335
        /** @var ProductVariantInterface $variant */
336
        $variant = $this->defaultVariantResolver->getVariant($product);
337
        $variant->setTaxCategory($taxCategory);
338
339
        $this->objectManager->flush();
340
    }
341
342
    /**
343
     * @Given /^(it) comes in the following variations:$/
344
     */
345
    public function itComesInTheFollowingVariations(ProductInterface $product, TableNode $table)
346
    {
347
        foreach ($table->getHash() as $variantHash) {
348
            /** @var ProductVariantInterface $variant */
349
            $variant = $this->productVariantFactory->createNew();
350
351
            $variant->setName($variantHash['name']);
352
            $variant->setCode(StringInflector::nameToUppercaseCode($variantHash['name']));
353
            $variant->setPrice($this->getPriceFromString(str_replace(['$', '€', '£'], '', $variantHash['price'])));
354
            $variant->setProduct($product);
355
            $product->addVariant($variant);
356
        }
357
358
        $this->objectManager->flush();
359
    }
360
361
    /**
362
     * @Given /^("[^"]+" variant of product "[^"]+") belongs to ("[^"]+" tax category)$/
363
     */
364
    public function productVariantBelongsToTaxCategory(
365
        ProductVariantInterface $productVariant,
366
        TaxCategoryInterface $taxCategory
367
    ) {
368
        $productVariant->setTaxCategory($taxCategory);
369
        $this->objectManager->flush($productVariant);
370
    }
371
372
    /**
373
     * @Given /^(this product) has ([^"]+) attribute "([^"]+)" with value "([^"]+)"$/
374
     */
375
    public function thisProductHasAttributeWithValue(ProductInterface $product, $productAttributeType, $productAttributeName, $value)
376
    {
377
        $attribute = $this->createProductAttribute($productAttributeType,$productAttributeName);
378
        $attributeValue = $this->createProductAttributeValue($value, $attribute);
379
        $product->addAttribute($attributeValue);
380
381
        $this->objectManager->flush();
382
    }
383
384
    /**
385
     * @Given /^(this product) has percent attribute "([^"]+)" with value ([^"]+)%$/
386
     */
387
    public function thisProductHasPercentAttributeWithValue(ProductInterface $product, $productAttributeName, $value)
388
    {
389
        $attribute = $this->createProductAttribute('percent',$productAttributeName);
390
        $attributeValue = $this->createProductAttributeValue($value/100, $attribute);
391
        $product->addAttribute($attributeValue);
392
393
        $this->objectManager->flush();
394
    }
395
396
    /**
397
     * @Given /^(this product) has ([^"]+) attribute "([^"]+)" set to "([^"]+)"$/
398
     */
399
    public function thisProductHasCheckboxAttributeWithValue(ProductInterface $product, $productAttributeType, $productAttributeName, $value)
400
    {
401
        $attribute = $this->createProductAttribute($productAttributeType, $productAttributeName);
402
        $booleanValue = ('Yes' === $value);
403
        $attributeValue = $this->createProductAttributeValue($booleanValue, $attribute);
0 ignored issues
show
Documentation introduced by
$booleanValue is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
404
        $product->addAttribute($attributeValue);
405
406
        $this->objectManager->flush();
407
    }
408
409
    /**
410
     * @Given /^(this product) has percent attribute "([^"]+)" at position (\d+)$/
411
     */
412
    public function thisProductHasPercentAttributeWithValueAtPosition(ProductInterface $product, $productAttributeName, $position)
413
    {
414
        $attribute = $this->createProductAttribute('percent',$productAttributeName);
415
        $attribute->setPosition($position);
416
        $attributeValue = $this->createProductAttributeValue(rand(1,100)/100, $attribute);
417
418
        $product->addAttribute($attributeValue);
419
420
        $this->objectManager->flush();
421
    }
422
423
    /**
424
     * @Given /^(this product) has ([^"]+) attribute "([^"]+)" with date "([^"]+)"$/
425
     */
426
    public function thisProductHasDateTimeAttributeWithDate(ProductInterface $product, $productAttributeType, $productAttributeName, $date)
427
    {
428
        $attribute = $this->createProductAttribute($productAttributeType, $productAttributeName);
429
        $attributeValue = $this->createProductAttributeValue(new \DateTime($date), $attribute);
0 ignored issues
show
Documentation introduced by
new \DateTime($date) is of type object<DateTime>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
430
431
        $product->addAttribute($attributeValue);
432
433
        $this->objectManager->flush();
434
    }
435
436
    /**
437
     * @Given /^(this product) has option "([^"]+)" with values "([^"]+)" and "([^"]+)"$/
438
     * @Given /^(this product) has option "([^"]+)" with values "([^"]+)", "([^"]+)" and "([^"]+)"$/
439
     */
440
    public function thisProductHasOptionWithValues(ProductInterface $product, $optionName, ...$values)
441
    {
442
        /** @var ProductOptionInterface $option */
443
        $option = $this->productOptionFactory->createNew();
444
445
        $option->setName($optionName);
446
        $option->setCode(StringInflector::nameToUppercaseCode($optionName));
447
448
        $this->sharedStorage->set(sprintf('%s_option', $optionName), $option);
449
450
        foreach ($values as $key => $value) {
451
            $optionValue = $this->addProductOption($option, $value, StringInflector::nameToUppercaseCode($value));
452
            $this->sharedStorage->set(sprintf('%s_option_%s_value', $value, strtolower($optionName)), $optionValue);
453
        }
454
455
        $product->addOption($option);
456
        $product->setVariantSelectionMethod(ProductInterface::VARIANT_SELECTION_MATCH);
457
458
        $this->objectManager->persist($option);
459
        $this->objectManager->flush();
460
    }
461
462
    /**
463
     * @Given /^there (?:is|are) (\d+) unit(?:|s) of (product "([^"]+)") available in the inventory$/
464
     * @When product :product quantity is changed to :quantity
465
     */
466
    public function thereIsQuantityOfProducts($quantity, ProductInterface $product)
467
    {
468
        /** @var ProductVariantInterface $productVariant */
469
        $productVariant = $this->defaultVariantResolver->getVariant($product);
470
        $productVariant->setOnHand($quantity);
471
472
        $this->objectManager->flush();
473
    }
474
475
    /**
476
     * @Given /^the (product "([^"]+)") is out of stock$/
477
     */
478
    public function theProductIsOutOfStock(ProductInterface $product)
479
    {
480
        /** @var ProductVariantInterface $productVariant */
481
        $productVariant = $this->defaultVariantResolver->getVariant($product);
482
        $productVariant->setTracked(true);
483
        $productVariant->setOnHand(0);
484
485
        $this->objectManager->flush();
486
    }
487
488
    /**
489
     * @When other customer has bought :quantity :product products by this time
490
     */
491
    public function otherCustomerHasBoughtProductsByThisTime($quantity, ProductInterface $product)
492
    {
493
        /** @var ProductVariantInterface $productVariant */
494
        $productVariant = $this->defaultVariantResolver->getVariant($product);
495
        $productVariant->setOnHand($productVariant->getOnHand() - $quantity);
496
497
        $this->objectManager->flush();
498
    }
499
500
    /**
501
     * @Given /^(this product) is tracked by the inventory$/
502
     * @Given /^(?:|the )("[^"]+" product) is(?:| also) tracked by the inventory$/
503
     */
504
    public function thisProductIsTrackedByTheInventory(ProductInterface $product)
505
    {
506
        /** @var ProductVariantInterface $productVariant */
507
        $productVariant = $this->defaultVariantResolver->getVariant($product);
508
        $productVariant->setTracked(true);
509
510
        $this->objectManager->flush();
511
    }
512
513
    /**
514
     * @Given /^(this product) is available in "([^"]+)" ([^"]+) priced at ("[^"]+")$/
515
     */
516
    public function thisProductIsAvailableInSize(ProductInterface $product, $optionValueName, $optionName, $price)
517
    {
518
        /** @var ProductVariantInterface $variant */
519
        $variant = $this->productVariantFactory->createNew();
520
521
        $optionValue = $this->sharedStorage->get(sprintf('%s_option_%s_value', $optionValueName, $optionName));
522
523
        $variant->addOptionValue($optionValue);
524
        $variant->setPrice($price);
525
        $variant->setCode(sprintf("%s_%s", $product->getCode(), $optionValueName));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal %s_%s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
526
527
        $product->addVariant($variant);
528
        $this->objectManager->flush();
529
    }
530
531
    /**
532
     * @Given /^(this product) has (this product option)$/
533
     * @Given /^(this product) has a ("[^"]+" option)$/
534
     * @Given /^(this product) has an ("[^"]+" option)$/
535
     */
536
    public function thisProductHasThisProductOption(ProductInterface $product, ProductOptionInterface $option)
537
    {
538
        $product->addOption($option);
539
540
        $this->objectManager->flush();
541
    }
542
543
    /**
544
     * @Given /^there are ([^"]+) units of ("[^"]+" variant of product "[^"]+") available in the inventory$/
545
     */
546
    public function thereAreItemsOfProductInVariantAvailableInTheInventory($quantity, ProductVariantInterface $productVariant)
547
    {
548
        $productVariant->setTracked(true);
549
        $productVariant->setOnHand($quantity);
550
551
        $this->objectManager->flush();
552
    }
553
554
    /**
555
     * @Given /^the ("[^"]+" product variant) is tracked by the inventory$/
556
     */
557
    public function theProductVariantIsTrackedByTheInventory(ProductVariantInterface $productVariant)
558
    {
559
        $productVariant->setTracked(true);
560
561
        $this->objectManager->flush();
562
    }
563
564
    /**
565
     * @Given /^(this product)'s price is ("[^"]+")$/
566
     * @Given /^the (product "[^"]+") changed its price to ("[^"]+")$/
567
     */
568
    public function theProductChangedItsPriceTo(ProductInterface $product, $price)
569
    {
570
        /** @var ProductVariantInterface $productVariant */
571
        $productVariant = $this->defaultVariantResolver->getVariant($product);
572
        $productVariant->setPrice($price);
573
574
        $this->objectManager->flush();
575
    }
576
577
    /**
578
     * @Given /^(this product) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/
579
     * @Given /^the ("[^"]+" product) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/
580
     */
581
    public function thisProductHasAnImageWithACode(ProductInterface $product, $imagePath, $imageCode)
582
    {
583
        $filesPath = $this->getParameter('files_path');
584
585
        /** @var ImageInterface $productImage */
586
        $productImage = $this->productImageFactory->createNew();
587
        $productImage->setFile(new UploadedFile($filesPath.$imagePath, basename($imagePath)));
588
        $productImage->setCode($imageCode);
589
        $this->imageUploader->upload($productImage);
590
591
        $product->addImage($productImage);
592
593
        $this->objectManager->flush($product);
594
    }
595
596
    /**
597
     * @Given /^(it) has different prices for different channels and currencies$/
598
     */
599
    public function itHasDifferentPricesForDifferentChannelsAndCurrencies(ProductInterface $product)
600
    {
601
        /** @var ProductVariantInterface $variant */
602
        $variant = $this->defaultVariantResolver->getVariant($product);
603
604
        $variant->setPricingCalculator(Calculators::CHANNEL_AND_CURRENCY_BASED);
605
    }
606
607
    /**
608
     * @Given /^(it) has price ("[^"]+") for ("[^"]+" channel) and "([^"]+)" currency$/
609
     */
610
    public function itHasPriceForChannelAndCurrency(
611
        ProductInterface $product,
612
        $price,
613
        ChannelInterface $channel,
614
        $currency
615
    ) {
616
        /** @var ProductVariantInterface $variant */
617
        $variant = $this->defaultVariantResolver->getVariant($product);
618
619
        $pricingConfiguration = $variant->getPricingConfiguration();
620
        $pricingConfiguration[$channel->getCode()][$currency] = $price;
621
622
        $variant->setPricingConfiguration($pricingConfiguration);
623
624
        $this->objectManager->flush();
625
    }
626
627
    /**
628
     * @Given /^(this product) belongs to ("([^"]+)" shipping category)$/
629
     */
630
    public function thisProductBelongsToShippingCategory(ProductInterface $product, ShippingCategoryInterface $shippingCategory)
631
    {
632
        $product->setShippingCategory($shippingCategory);
633
        $this->objectManager->flush();
634
    }
635
636
    /**
637
     * @param string $type
638
     * @param string $name
639
     * @param string|null $code
640
     *
641
     * @return ProductAttributeInterface
642
     */
643
    private function createProductAttribute($type, $name, $code = null)
644
    {
645
        $productAttribute = $this->productAttributeFactory->createTyped($type);
646
647
        if (null === $code) {
648
            $code = StringInflector::nameToCode($name);
649
        }
650
651
        $productAttribute->setCode($code);
652
        $productAttribute->setName($name);
653
654
        $this->objectManager->persist($productAttribute);
655
656
        return $productAttribute;
657
    }
658
659
    /**
660
     * @param string $value
661
     * @param ProductAttributeInterface $attribute
662
     *
663
     * @return ProductAttributeValueInterface
664
     */
665
    private function createProductAttributeValue($value, ProductAttributeInterface $attribute)
666
    {
667
        /** @var ProductAttributeValueInterface $attributeValue */
668
        $attributeValue = $this->attributeValueFactory->createNew();
669
        $attributeValue->setAttribute($attribute);
670
        $attributeValue->setValue($value);
671
672
        $this->objectManager->persist($attributeValue);
673
674
        return $attributeValue;
675
    }
676
677
    /**
678
     * @param string $price
679
     *
680
     * @return int
681
     */
682
    private function getPriceFromString($price)
683
    {
684
        return (int) round(($price * 100), 2);
685
    }
686
687
    /**
688
     * @param string $productName
689
     * @param int $price
690
     * @param string $date
0 ignored issues
show
Documentation introduced by
Should the type for parameter $date not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
691
     *
692
     * @return ProductInterface
693
     */
694
    private function createProduct($productName, $price = 0, $date = null)
695
    {
696
        /** @var ProductInterface $product */
697
        $product = $this->productFactory->createWithVariant();
698
699
        $product->setName($productName);
700
        $product->setCode(StringInflector::nameToUppercaseCode($productName));
701
        $product->setSlug($this->slugGenerator->generate($productName));
702
        $product->setCreatedAt(new \DateTime($date));
703
704
        /** @var ProductVariantInterface $productVariant */
705
        $productVariant = $this->defaultVariantResolver->getVariant($product);
706
        $productVariant->setPrice($price);
707
        $productVariant->setCode($product->getCode());
708
709
        return $product;
710
    }
711
712
    /**
713
     * @param ProductOptionInterface $option
714
     * @param string $value
715
     * @param string $code
716
     *
717
     * @return ProductOptionValueInterface
718
     */
719
    private function addProductOption(ProductOptionInterface $option, $value, $code)
720
    {
721
        /** @var ProductOptionValueInterface $optionValue */
722
        $optionValue = $this->productOptionValueFactory->createNew();
723
724
        $optionValue->setValue($value);
725
        $optionValue->setCode($code);
726
        $optionValue->setOption($option);
727
728
        $option->addValue($optionValue);
729
730
        return $optionValue;
731
    }
732
733
    /**
734
     * @param ProductInterface $product
735
     */
736
    private function saveProduct(ProductInterface $product)
737
    {
738
        $this->productRepository->add($product);
739
        $this->sharedStorage->set('product', $product);
740
    }
741
742
    /**
743
     * @param string $name
744
     *
745
     * @return NodeElement
746
     */
747
    private function getParameter($name)
748
    {
749
        return isset($this->minkParameters[$name]) ? $this->minkParameters[$name] : null;
750
    }
751
752
    /**
753
     * @param ProductInterface $product
754
     * @param $productVariantName
755
     * @param int $price
756
     * @param string $code
757
     */
758
    private function createProductVariant(ProductInterface $product, $productVariantName, $price, $code)
759
    {
760
        $product->setVariantSelectionMethod(ProductInterface::VARIANT_SELECTION_CHOICE);
761
762
        /** @var ProductVariantInterface $variant */
763
        $variant = $this->productVariantFactory->createNew();
764
765
        $variant->setName($productVariantName);
766
        $variant->setCode($code);
767
        $variant->setPrice($price);
768
        $variant->setProduct($product);
769
        $product->addVariant($variant);
770
771
        $this->objectManager->flush();
772
773
        $this->sharedStorage->set('variant', $variant);
774
    }
775
}
776