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 Sylius\Behat\Service\SharedStorageInterface; |
16
|
|
|
use Sylius\Component\Currency\Model\CurrencyInterface; |
17
|
|
|
use Sylius\Component\Currency\Model\ExchangeRateInterface; |
18
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
19
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Jan Góralski <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class ExchangeRateContext implements Context |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var SharedStorageInterface |
28
|
|
|
*/ |
29
|
|
|
private $sharedStorage; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var FactoryInterface |
33
|
|
|
*/ |
34
|
|
|
private $exchangeRateFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var RepositoryInterface |
38
|
|
|
*/ |
39
|
|
|
private $exchangeRateRepository; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param SharedStorageInterface $sharedStorage |
43
|
|
|
* @param FactoryInterface $exchangeRateFactory |
44
|
|
|
* @param RepositoryInterface $exchangeRateRepository |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
SharedStorageInterface $sharedStorage, |
48
|
|
|
FactoryInterface $exchangeRateFactory, |
49
|
|
|
RepositoryInterface $exchangeRateRepository |
|
|
|
|
50
|
|
|
) { |
51
|
|
|
$this->sharedStorage = $sharedStorage; |
52
|
|
|
$this->exchangeRateFactory = $exchangeRateFactory; |
53
|
|
|
$this->exchangeRateRepository = $exchangeRateRepository; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @Given /^the store (?:|also )has an exchange rate ([0-9\.]+) with source (currency "[^"]+") and target (currency "[^"]+")$/ |
58
|
|
|
*/ |
59
|
|
|
public function thereIsAnExchangeRateWithSourceCurrencyAndTargetCurrency( |
60
|
|
|
$ratio, |
61
|
|
|
CurrencyInterface $sourceCurrency, |
62
|
|
|
CurrencyInterface $targetCurrency |
63
|
|
|
) { |
64
|
|
|
$exchangeRate = $this->createExchangeRate($sourceCurrency, $targetCurrency, $ratio); |
65
|
|
|
|
66
|
|
|
$this->saveExchangeRate($exchangeRate); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param CurrencyInterface $sourceCurrency |
71
|
|
|
* @param CurrencyInterface $targetCurrency |
72
|
|
|
* @param float $ratio |
73
|
|
|
* |
74
|
|
|
* @return ExchangeRateInterface |
75
|
|
|
*/ |
76
|
|
|
private function createExchangeRate(CurrencyInterface $sourceCurrency, CurrencyInterface $targetCurrency, $ratio = 1.00) |
77
|
|
|
{ |
78
|
|
|
/** @var ExchangeRateInterface $exchangeRate */ |
79
|
|
|
$exchangeRate = $this->exchangeRateFactory->createNew(); |
80
|
|
|
$exchangeRate->setSourceCurrency($sourceCurrency); |
81
|
|
|
$exchangeRate->setTargetCurrency($targetCurrency); |
82
|
|
|
$exchangeRate->setRatio((float) $ratio); |
83
|
|
|
|
84
|
|
|
return $exchangeRate; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param ExchangeRateInterface $exchangeRate |
89
|
|
|
*/ |
90
|
|
|
private function saveExchangeRate(ExchangeRateInterface $exchangeRate) |
91
|
|
|
{ |
92
|
|
|
$this->exchangeRateRepository->add($exchangeRate); |
93
|
|
|
$this->sharedStorage->set('exchange_rate', $exchangeRate); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.