Completed
Push — master ( 62f72e...c36855 )
by Kamil
83:57 queued 64:20
created

CurrencyContext::itUsesTheCurrencyByDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 7
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 Doctrine\Common\Collections\ArrayCollection;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Sylius\Behat\Service\SharedStorageInterface;
18
use Sylius\Component\Core\Currency\CurrencyStorageInterface;
19
use Sylius\Component\Core\Model\ChannelInterface;
20
use Sylius\Component\Currency\Model\CurrencyInterface;
21
use Sylius\Component\Resource\Factory\FactoryInterface;
22
use Sylius\Component\Resource\Repository\RepositoryInterface;
23
24
/**
25
 * @author Arkadiusz Krakowiak <[email protected]>
26
 */
27
final class CurrencyContext implements Context
28
{
29
    /**
30
     * @var SharedStorageInterface
31
     */
32
    private $sharedStorage;
33
34
    /**
35
     * @var RepositoryInterface
36
     */
37
    private $currencyRepository;
38
39
    /**
40
     * @var FactoryInterface
41
     */
42
    private $currencyFactory;
43
44
    /**
45
     * @var CurrencyStorageInterface
46
     */
47
    private $currencyStorage;
48
49
    /**
50
     * @var ObjectManager
51
     */
52
    private $currencyManager;
53
54
    /**
55
     * @var ObjectManager
56
     */
57
    private $channelManager;
58
59
    /**
60
     * @param SharedStorageInterface $sharedStorage
61
     * @param RepositoryInterface $currencyRepository
62
     * @param FactoryInterface $currencyFactory
63
     * @param CurrencyStorageInterface $currencyStorage
64
     * @param ObjectManager $currencyManager
65
     * @param ObjectManager $channelManager
66
     */
67
    public function __construct(
68
        SharedStorageInterface $sharedStorage,
69
        RepositoryInterface $currencyRepository,
70
        FactoryInterface $currencyFactory,
71
        CurrencyStorageInterface $currencyStorage,
72
        ObjectManager $currencyManager,
73
        ObjectManager $channelManager
74
    ) {
75
        $this->sharedStorage = $sharedStorage;
76
        $this->currencyRepository = $currencyRepository;
77
        $this->currencyFactory = $currencyFactory;
78
        $this->currencyStorage = $currencyStorage;
79
        $this->currencyManager = $currencyManager;
80
        $this->channelManager = $channelManager;
81
    }
82
83
    /**
84
     * @Given the store has currency :currencyCode
85
     */
86
    public function theStoreHasCurrency($currencyCode)
87
    {
88
        $currency = $this->createCurrency($currencyCode);
89
        $currency->setEnabled(true);
90
91
        $this->saveCurrency($currency);
92
    }
93
94
    /**
95
     * @Given the store has currency :currencyCode, :secondCurrencyCode
96
     */
97
    public function theStoreHasCurrencyAnd($currencyCode, $secondCurrencyCode)
98
    {
99
        $this->saveCurrency($this->createCurrency($currencyCode));
100
        $this->saveCurrency($this->createCurrency($secondCurrencyCode));
101
    }
102
103
    /**
104
     * @Given the store has disabled currency :currencyCode
105
     * @Given the currency :currencyCode is disabled (as well)
106
     * @Given the currency :currencyCode gets disabled
107
     * @Given the currency :currencyCode has been disabled
108
     */
109
    public function theStoreHasDisabledCurrency($currencyCode)
110
    {
111
        $currency = $this->provideCurrency($currencyCode);
112
        $currency->setEnabled(false);
113
114
        $this->saveCurrency($currency);
115
    }
116
117
    /**
118
     * @Given the store has currency :currencyCode with exchange rate :exchangeRate
119
     */
120
    public function theStoreHasCurrencyWithExchangeRate($currencyCode, $exchangeRate)
121
    {
122
        $currency = $this->createCurrency($currencyCode, (float) $exchangeRate);
123
        $currency->setEnabled(true);
124
125
        $this->saveCurrency($currency);
126
    }
127
128
    /**
129
     * @Given /^(that channel) allows to shop using the "([^"]+)" currency$/
130
     * @Given /^(that channel) allows to shop using "([^"]+)" and "([^"]+)" currencies$/
131
     * @Given /^(that channel) allows to shop using "([^"]+)", "([^"]+)" and "([^"]+)" currencies$/
132
     */
133
    public function thatChannelAllowsToShopUsingAndCurrencies(ChannelInterface $channel, ...$currenciesCodes)
134
    {
135
        foreach ($channel->getCurrencies() as $currency) {
136
            $channel->removeCurrency($currency);
137
        }
138
139
        foreach ($currenciesCodes as $currencyCode) {
140
            $channel->addCurrency($this->provideCurrency($currencyCode));
141
        }
142
143
        $this->channelManager->flush();
144
    }
145
146
    /**
147
     * @Given /^(that channel)(?: also|) allows to shop using the "([^"]+)" currency with exchange rate ([0-9\.]+)$/
148
     */
149
    public function thatChannelAllowsToShopUsingCurrency(ChannelInterface $channel, $currencyCode, $exchangeRate = 1.0)
150
    {
151
        $currency = $this->createCurrency($currencyCode, $exchangeRate);
152
        $channel->addCurrency($currency);
153
        $this->saveCurrency($currency);
154
155
        $this->channelManager->flush();
156
    }
157
158
    /**
159
     * @Given /^the exchange rate for (currency "[^"]+") was changed to ([0-9\.]+)$/
160
     * @Given /^the ("[^"]+" currency) has an exchange rate of ([0-9\.]+)$/
161
     */
162
    public function theExchangeRateForWasChangedTo(CurrencyInterface $currency, $exchangeRate)
163
    {
164
        $currency->setExchangeRate($exchangeRate);
165
        $this->saveCurrency($currency);
166
    }
167
168
    /**
169
     * @param CurrencyInterface $currency
170
     */
171
    private function saveCurrency(CurrencyInterface $currency)
172
    {
173
        $this->sharedStorage->set('currency', $currency);
174
        $this->currencyRepository->add($currency);
175
    }
176
177
    /**
178
     * @param $currencyCode
179
     * @param float $exchangeRate
180
     *
181
     * @return CurrencyInterface
182
     */
183
    private function createCurrency($currencyCode, $exchangeRate = 1.0)
184
    {
185
        /** @var CurrencyInterface $currency */
186
        $currency = $this->currencyFactory->createNew();
187
        $currency->setCode($currencyCode);
188
        $currency->setExchangeRate($exchangeRate);
189
190
        return $currency;
191
    }
192
193
    /**
194
     * @param string $currencyCode
195
     *
196
     * @return CurrencyInterface
197
     */
198
    private function provideCurrency($currencyCode)
199
    {
200
        $currency = $this->currencyRepository->findOneBy(['code' => $currencyCode]);
201
        if (null === $currency) {
202
            /** @var CurrencyInterface $currency */
203
            $currency = $this->createCurrency($currencyCode);
204
205
            $this->currencyRepository->add($currency);
206
        }
207
208
        return $currency;
209
    }
210
}
211