Completed
Push — master ( 094334...0b6e60 )
by Kamil
17:47
created

ProductContext   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

8 Methods

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