HistoricalSwapExchange::quoteHistorical()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
crap 6
1
<?php
2
3
namespace Jarobe\HistoricalMoney\Exchange;
4
5
use Jarobe\HistoricalMoney\Exception\UnsupportedHistoricalDateException;
6
use Jarobe\HistoricalMoney\HistoricalExchange;
7
use Money\Currency;
8
use Money\CurrencyPair;
9
use Money\Exception\UnresolvableCurrencyPairException;
10
use Swap\Swap;
11
use Exchanger\Exception\Exception as ExchangerException;
12
13
final class HistoricalSwapExchange implements HistoricalExchange
14
{
15
    /**
16
     * @var Swap
17
     */
18
    private $swap;
19
20
    /**
21
     * @param Swap $swap
22
     */
23
    public function __construct(Swap $swap)
24
    {
25
        $this->swap = $swap;
26
    }
27
28
    /**
29
     * Returns a currency pair for the passed currencies with the rate coming from a third-party source.
30
     *
31
     * @param \DateTimeInterface $dateTime
32
     * @param Currency           $baseCurrency
33
     * @param Currency           $counterCurrency
34
     *
35
     * @return CurrencyPair
36
     *
37
     * @throws UnsupportedHistoricalDateException When the provided date is not supported
38
     * @throws UnresolvableCurrencyPairException  When there is no currency pair available for the currencies
39
     */
40
    public function quoteHistorical(\DateTimeInterface $dateTime, Currency $baseCurrency, Currency $counterCurrency)
41
    {
42
        try {
43
            $rate = $this->swap->historical($baseCurrency->getCode().'/'.$counterCurrency->getCode(), $dateTime);
44
        } catch (ExchangerException $e) {
45
            throw UnresolvableCurrencyPairException::createFromCurrencies($baseCurrency, $counterCurrency);
46
        }
47
48
        return new CurrencyPair($baseCurrency, $counterCurrency, $rate->getValue());
49
    }
50
}
51