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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Sylius\Behat\Context\Transform; |
15
|
|
|
|
16
|
|
|
use Behat\Behat\Context\Context; |
17
|
|
|
use Sylius\Component\Currency\Converter\CurrencyNameConverterInterface; |
18
|
|
|
use Sylius\Component\Currency\Model\ExchangeRateInterface; |
19
|
|
|
use Sylius\Component\Currency\Repository\ExchangeRateRepositoryInterface; |
20
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
21
|
|
|
use Webmozart\Assert\Assert; |
22
|
|
|
|
23
|
|
|
final class ExchangeRateContext implements Context |
24
|
|
|
{ |
25
|
|
|
/** @var CurrencyNameConverterInterface */ |
26
|
|
|
private $currencyNameConverter; |
27
|
|
|
|
28
|
|
|
/** @var RepositoryInterface */ |
29
|
|
|
private $currencyRepository; |
30
|
|
|
|
31
|
|
|
/** @var ExchangeRateRepositoryInterface */ |
32
|
|
|
private $exchangeRateRepository; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
CurrencyNameConverterInterface $currencyNameConverter, |
36
|
|
|
RepositoryInterface $currencyRepository, |
37
|
|
|
ExchangeRateRepositoryInterface $exchangeRateRepository |
38
|
|
|
) { |
39
|
|
|
$this->currencyNameConverter = $currencyNameConverter; |
40
|
|
|
$this->currencyRepository = $currencyRepository; |
41
|
|
|
$this->exchangeRateRepository = $exchangeRateRepository; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @Transform /^exchange rate between "([^"]+)" and "([^"]+)"$/ |
46
|
|
|
*/ |
47
|
|
|
public function getExchangeRateByCurrencies( |
48
|
|
|
string $sourceCurrencyName, |
49
|
|
|
string $targetCurrencyName |
50
|
|
|
): ExchangeRateInterface { |
51
|
|
|
$sourceCurrencyCode = $this->currencyNameConverter->convertToCode($sourceCurrencyName); |
52
|
|
|
$targetCurrencyCode = $this->currencyNameConverter->convertToCode($targetCurrencyName); |
53
|
|
|
|
54
|
|
|
/** @var ExchangeRateInterface|null */ |
55
|
|
|
$exchangeRate = $this |
56
|
|
|
->exchangeRateRepository |
57
|
|
|
->findOneWithCurrencyPair($sourceCurrencyCode, $targetCurrencyCode) |
58
|
|
|
; |
59
|
|
|
|
60
|
|
|
Assert::notNull( |
61
|
|
|
$exchangeRate, |
62
|
|
|
sprintf( |
63
|
|
|
'ExchangeRate for %s and %s currencies does not exist.', |
64
|
|
|
$sourceCurrencyName, |
65
|
|
|
$targetCurrencyName |
66
|
|
|
) |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
return $exchangeRate; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|