Completed
Push — master ( 031900...586888 )
by Kamil
20:21
created

ProductContext::theStoreHasAProductOptionWithACode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 2
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 Doctrine\Common\Persistence\ObjectManager;
17
use Sylius\Component\Core\Model\ChannelInterface;
18
use Sylius\Component\Core\Model\ProductInterface;
19
use Sylius\Component\Core\Model\ProductVariantInterface;
20
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
21
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
22
use Sylius\Component\Resource\Factory\FactoryInterface;
23
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
24
25
/**
26
 * @author Arkadiusz Krakowiak <[email protected]>
27
 * @author Mateusz Zalewski <[email protected]>
28
 * @author Magdalena Banasiak <[email protected]>
29
 */
30
final class ProductContext implements Context
31
{
32
    /**
33
     * @var SharedStorageInterface
34
     */
35
    private $sharedStorage;
36
37
    /**
38
     * @var ProductRepositoryInterface
39
     */
40
    private $productRepository;
41
42
    /**
43
     * @var FactoryInterface
44
     */
45
    private $productFactory;
46
47
    /**
48
     * @var FactoryInterface
49
     */
50
    private $productVariantFactory;
51
52
    /**
53
     * @var ObjectManager
54
     */
55
    private $objectManager;
56
57
    /**
58
     * @param SharedStorageInterface $sharedStorage
59
     * @param ProductRepositoryInterface $productRepository
60
     * @param FactoryInterface $productFactory
61
     * @param FactoryInterface $productVariantFactory
62
     * @param ObjectManager $objectManager
63
     */
64
    public function __construct(
65
        SharedStorageInterface $sharedStorage,
66
        ProductRepositoryInterface $productRepository,
67
        FactoryInterface $productFactory,
68
        FactoryInterface $productVariantFactory,
69
        ObjectManager $objectManager
70
    ) {
71
        $this->sharedStorage = $sharedStorage;
72
        $this->productRepository = $productRepository;
73
        $this->productFactory = $productFactory;
74
        $this->productVariantFactory = $productVariantFactory;
75
        $this->objectManager = $objectManager;
76
    }
77
78
    /**
79
     * @Given /^the store has a product "([^"]+)"$/
80
     * @Given /^the store has a product "([^"]+)" priced at ("[^"]+")$/
81
     */
82
    public function storeHasAProductPricedAt($productName, $price = 0)
83
    {
84
        /** @var ProductInterface $product */
85
        $product = $this->productFactory->createNew();
86
87
        $product->setName($productName);
88
        $product->setPrice($price);
89
        $product->setDescription('Awesome '.$productName);
90
91
        $channel = $this->sharedStorage->get('channel');
92
        $product->addChannel($channel);
93
94
        $this->productRepository->add($product);
95
96
        $this->sharedStorage->set('product', $product);
97
    }
98
99
    /**
100
     * @Given /^the (product "[^"]+") has "([^"]+)" variant priced at ("[^"]+")$/
101
     * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+")$/
102
     */
103
    public function theProductHasVariantPricedAt(ProductInterface $product, $productVariantName, $price)
104
    {
105
        /** @var ProductVariantInterface $variant */
106
        $variant = $this->productVariantFactory->createNew();
107
108
        $variant->setPresentation($productVariantName);
109
        $variant->setPrice($price);
110
        $variant->setProduct($product);
111
        $product->addVariant($variant);
112
113
        $this->objectManager->flush();
114
115
        $this->sharedStorage->set('variant', $variant);
116
    }
117
118
    /**
119
     * @Given /^there is product "([^"]+)" available in ((?:this|that|"[^"]+") channel)$/
120
     */
121
    public function thereIsProductAvailableInGivenChannel($productName, ChannelInterface $channel)
122
    {
123
        /** @var ProductInterface $product */
124
        $product = $this->productFactory->createNew();
125
126
        $product->setName($productName);
127
        $product->setPrice(0);
128
        $product->setDescription('Awesome ' . $productName);
129
        $product->addChannel($channel);
130
131
        $this->productRepository->add($product);
132
    }
133
134
    /**
135
     * @Given /^([^"]+) belongs to ("[^"]+" tax category)$/
136
     */
137
    public function productBelongsToTaxCategory(ProductInterface $product, TaxCategoryInterface $taxCategory)
138
    {
139
        $product->getMasterVariant()->setTaxCategory($taxCategory);
140
        $this->objectManager->flush();
141
    }
142
143
    /**
144
     * @Given /^(it) comes in the following variations:$/
145
     */
146
    public function itComesInTheFollowingVariations(ProductInterface $product, TableNode $table)
147
    {
148
        foreach ($table->getHash() as $variantHash) {
149
            /** @var ProductVariantInterface $variant */
150
            $variant = $this->productVariantFactory->createNew();
151
152
            $variant->setPresentation($variantHash['name']);
153
            $variant->setPrice($this->getPriceFromString(str_replace(['$', '€', '£'], '', $variantHash['price'])));
154
            $variant->setProduct($product);
155
            $product->addVariant($variant);
156
        }
157
158
        $this->objectManager->flush();
159
    }
160
161
    /**
162
     * @Given /^("[^"]+" variant of product "[^"]+") belongs to ("[^"]+" tax category)$/
163
     */
164
    public function productVariantBelongsToTaxCategory(
165
        ProductVariantInterface $productVariant,
166
        TaxCategoryInterface $taxCategory
167
    ) {
168
        $productVariant->setTaxCategory($taxCategory);
169
        $this->objectManager->flush($productVariant);
170
    }
171
172
    /**
173
     * @param string $price
174
     *
175
     * @return int
176
     */
177
    private function getPriceFromString($price)
178
    {
179
        return (int) round(($price * 100), 2);
180
    }
181
}
182