Completed
Push — master ( 031900...425710 )
by Paweł
17s
created

thisProductOptionHasTheOptionValueWithCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 3
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 Doctrine\Common\Persistence\ObjectManager;
16
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
17
use Sylius\Component\Product\Model\OptionInterface;
18
use Sylius\Component\Product\Model\OptionValueInterface;
19
use Sylius\Component\Resource\Factory\FactoryInterface;
20
use Sylius\Component\Variation\Repository\OptionRepositoryInterface;
21
22
/**
23
 * @author Grzegorz Sadowski <[email protected]>
24
 */
25
final class ProductOptionContext implements Context
26
{
27
    /**
28
     * @var SharedStorageInterface
29
     */
30
    private $sharedStorage;
31
32
    /**
33
     * @var OptionRepositoryInterface
34
     */
35
    private $productOptionRepository;
36
37
    /**
38
     * @var FactoryInterface
39
     */
40
    private $productOptionFactory;
41
42
    /**
43
     * @var FactoryInterface
44
     */
45
    private $productOptionValueFactory;
46
47
    /**
48
     * @var ObjectManager
49
     */
50
    private $objectManager;
51
52
    /**
53
     * @param SharedStorageInterface $sharedStorage
54
     * @param OptionRepositoryInterface $productOptionRepository
55
     * @param FactoryInterface $productOptionFactory
56
     * @param FactoryInterface $productOptionValueFactory
57
     * @param ObjectManager $objectManager
58
     */
59
    public function __construct(
60
        SharedStorageInterface $sharedStorage,
61
        OptionRepositoryInterface $productOptionRepository,
62
        FactoryInterface $productOptionFactory,
63
        FactoryInterface $productOptionValueFactory,
64
        ObjectManager $objectManager
65
    ) {
66
        $this->sharedStorage = $sharedStorage;
67
        $this->productOptionRepository = $productOptionRepository;
68
        $this->productOptionFactory = $productOptionFactory;
69
        $this->productOptionValueFactory = $productOptionValueFactory;
70
        $this->objectManager = $objectManager;
71
    }
72
73
    /**
74
     * @Given the store has a product option :productOptionName with a code :productOptionCode
75
     */
76
    public function theStoreHasAProductOptionWithACode($productOptionName, $productOptionCode)
77
    {
78
        $productOption = $this->productOptionFactory->createNew();
79
        $productOption->setCode($productOptionCode);
80
        $productOption->setName($productOptionName);
81
82
        $this->sharedStorage->set('product_option', $productOption);
83
        $this->productOptionRepository->add($productOption);
84
    }
85
86
    /**
87
     * @Given /^(this product option) has(?:| also) the "([^"]+)" option value with code "([^"]+)"$/
88
     */
89
    public function thisProductOptionHasTheOptionValueWithCode(
90
        OptionInterface $productOption,
91
        $productOptionValueName,
92
        $productOptionValueCode
93
    ) {
94
        $productOptionValue = $this->createProductOptionValue($productOptionValueName, $productOptionValueCode);
95
        $productOption->addValue($productOptionValue);
96
97
        $this->objectManager->flush();
98
    }
99
100
    /**
101
     * @param string $value
102
     * @param string $code
103
     *
104
     * @return OptionValueInterface
105
     */
106
    private function createProductOptionValue($value, $code)
107
    {
108
        $productOptionValue = $this->productOptionValueFactory->createNew();
109
        $productOptionValue->setValue($value);
110
        $productOptionValue->setCode($code);
111
112
        return $productOptionValue;
113
    }
114
}
115