Completed
Push — master ( 5bd0a6...5cfde8 )
by Kamil
20:21
created

ProductContext   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 11
Bugs 3 Features 2
Metric Value
wmc 12
c 11
b 3
f 2
lcom 1
cbo 7
dl 0
loc 167
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getProductByName() 0 9 2
A getProductVariantByNameAndProduct() 0 11 2
A storeHasAProductPricedAt() 0 14 1
A thereIsProductAvailableInGivenChannel() 0 11 1
A productBelongsToTaxCategory() 0 5 1
A itComesInTheFollowingVariations() 0 15 2
A productVariantBelongsToTaxCategory() 0 5 1
A getPriceFromString() 0 4 1
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\Test\Services\SharedStorageInterface;
21
use Sylius\Component\Resource\Factory\FactoryInterface;
22
use Sylius\Component\Resource\Repository\RepositoryInterface;
23
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
24
25
/**
26
 * @author Arkadiusz Krakowiak <[email protected]>
27
 * @author Mateusz Zalewski <[email protected]>
28
 */
29
final class ProductContext implements Context
30
{
31
    /**
32
     * @var SharedStorageInterface
33
     */
34
    private $sharedStorage;
35
36
    /**
37
     * @var RepositoryInterface
38
     */
39
    private $productRepository;
40
41
    /**
42
     * @var RepositoryInterface
43
     */
44
    private $productVariantRepository;
45
46
    /**
47
     * @var FactoryInterface
48
     */
49
    private $productFactory;
50
51
    /**
52
     * @var FactoryInterface
53
     */
54
    private $productVariantFactory;
55
56
    /**
57
     * @var ObjectManager
58
     */
59
    private $objectManager;
60
61
    /**
62
     * @param SharedStorageInterface $sharedStorage
63
     * @param RepositoryInterface $productRepository
64
     * @param RepositoryInterface $productVariantRepository
65
     * @param FactoryInterface $productFactory
66
     * @param FactoryInterface $productVariantFactory
67
     * @param ObjectManager $objectManager
68
     */
69
    public function __construct(
70
        SharedStorageInterface $sharedStorage,
71
        RepositoryInterface $productRepository,
72
        RepositoryInterface $productVariantRepository,
73
        FactoryInterface $productFactory,
74
        FactoryInterface $productVariantFactory,
75
        ObjectManager $objectManager
76
    ) {
77
        $this->sharedStorage = $sharedStorage;
78
        $this->productRepository = $productRepository;
79
        $this->productVariantRepository = $productVariantRepository;
80
        $this->productFactory = $productFactory;
81
        $this->productVariantFactory = $productVariantFactory;
82
        $this->objectManager = $objectManager;
83
    }
84
85
    /**
86
     * @Transform /^product "([^"]+)"$/
87
     * @Transform /^"([^"]+)" product$/
88
     * @Transform :product
89
     */
90
    public function getProductByName($productName)
91
    {
92
        $product = $this->productRepository->findOneBy(['name' => $productName]);
93
        if (null === $product) {
94
            throw new \InvalidArgumentException(sprintf('Product with name "%s" does not exist', $productName));
95
        }
96
97
        return $product;
98
    }
99
100
    /**
101
     * @Transform /^"([^"]+)" variant of product "([^"]+)"$/
102
     */
103
    public function getProductVariantByNameAndProduct($variantName, $productName)
104
    {
105
        $product = $this->getProductByName($productName);
106
107
        $productVariant = $this->productVariantRepository->findOneBy(['presentation' => $variantName, 'object' => $product]);
108
        if (null === $productVariant) {
109
            throw new \InvalidArgumentException(sprintf('Product variant with name "%s" of product "%s" does not exist', $variantName, $productName));
110
        }
111
112
        return $productVariant;
113
    }
114
115
    /**
116
     * @Given /^the store has a product "([^"]+)"$/
117
     * @Given /^the store has a product "([^"]+)" priced at "(?:€|£|\$)([^"]+)"$/
118
     */
119
    public function storeHasAProductPricedAt($productName, $price = '0.00')
120
    {
121
        $product = $this->productFactory->createNew();
122
        $product->setName($productName);
123
        $product->setPrice($this->getPriceFromString($price));
124
        $product->setDescription('Awesome '.$productName);
125
126
        $channel = $this->sharedStorage->getCurrentResource('channel');
127
        $product->addChannel($channel);
128
129
        $this->productRepository->add($product);
130
131
        $this->sharedStorage->setCurrentResource('product', $product);
132
    }
133
134
    /**
135
     * @Given /^there is product "([^"]+)" available in ((?:this|that|"[^"]+") channel)$/
136
     */
137
    public function thereIsProductAvailableInGivenChannel($productName, ChannelInterface $channel)
138
    {
139
        /** @var ProductInterface $product */
140
        $product = $this->productFactory->createNew();
141
        $product->setName($productName);
142
        $product->setPrice(0);
143
        $product->setDescription('Awesome ' . $productName);
144
        $product->addChannel($channel);
145
146
        $this->productRepository->add($product);
147
    }
148
149
    /**
150
     * @Given /^(product "[^"]+") belongs to ("[^"]+" tax category)$/
151
     */
152
    public function productBelongsToTaxCategory(ProductInterface $product, TaxCategoryInterface $taxCategory)
153
    {
154
        $product->getMasterVariant()->setTaxCategory($taxCategory);
155
        $this->objectManager->flush();
156
    }
157
158
    /**
159
     * @Given /^it comes in the following variations:$/
160
     */
161
    public function itComesInTheFollowingVariations(TableNode $table)
162
    {
163
        $currentProduct = $this->sharedStorage->getCurrentResource('product');
164
165
        foreach ($table->getHash() as $variantHash) {
166
            $variant = $this->productVariantFactory->createNew();
167
            $variant->setPresentation($variantHash['name']);
168
            $variant->setPrice($this->getPriceFromString(str_replace(['$', '€', '£'], '', $variantHash['price'])));
169
            $variant->setProduct($currentProduct);
170
171
            $this->productRepository->add($variant);
172
        }
173
174
        $this->objectManager->flush();
175
    }
176
177
    /**
178
     * @Given /^("[^"]+" variant of product "[^"]+") belongs to ("[^"]+" tax category)$/
179
     */
180
    public function productVariantBelongsToTaxCategory(ProductVariantInterface $productVariant, TaxCategoryInterface $taxCategory)
181
    {
182
        $productVariant->setTaxCategory($taxCategory);
183
        $this->objectManager->flush($productVariant);
184
    }
185
186
    /**
187
     * @param string $price
188
     *
189
     * @return int
190
     */
191
    private function getPriceFromString($price)
192
    {
193
        return (int) round(($price * 100), 2);
194
    }
195
}
196