Completed
Push — master ( b81f06...ddd2b8 )
by Florian
11s
created

Swap::latest()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 4
Code Lines 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of Swap.
5
 *
6
 * (c) Florian Voutzinos <[email protected]>
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 Swap;
13
14
use Exchanger\Contract\ExchangeRateProvider;
15
use Exchanger\ExchangeRateQueryBuilder;
16
17
/**
18
 * Swap is an easy to use facade to retrieve exchange rates from various services.
19
 *
20
 * @author Florian Voutzinos <[email protected]>
21
 */
22
class Swap
23
{
24
    /**
25
     * The exchange rate provider.
26
     *
27
     * @var ExchangeRateProvider
28
     */
29
    private $exchangeRateProvider;
30
31
    /**
32
     * Creates a new Swap.
33
     *
34
     * @param ExchangeRateProvider $exchangeRateProvider
35
     */
36
    public function __construct(ExchangeRateProvider $exchangeRateProvider)
37
    {
38
        $this->exchangeRateProvider = $exchangeRateProvider;
39
    }
40
41
    /**
42
     * Quotes a currency pair.
43
     *
44
     * @param string $currencyPair The currency pair like "EUR/USD"
45
     * @param array  $options      An array of query options
46
     *
47
     * @return \Exchanger\ExchangeRate
48
     */
49
    public function latest($currencyPair, array $options = [])
50
    {
51
        return $this->quote($currencyPair, null, $options);
52
    }
53
54
    /**
55
     * Quotes a currency pair.
56
     *
57
     * @param string             $currencyPair The currency pair like "EUR/USD"
58
     * @param \DateTimeInterface $date         An optional date for historical rates
59
     * @param array              $options      An array of query options
60
     *
61
     * @return \Exchanger\ExchangeRate
62
     */
63
    public function historical($currencyPair, \DateTimeInterface $date, array $options = [])
64
    {
65
        return $this->quote($currencyPair, $date, $options);
66
    }
67
68
    /**
69
     * Quotes a currency pair.
70
     *
71
     * @param string             $currencyPair The currency pair like "EUR/USD"
72
     * @param \DateTimeInterface $date         An optional date for historical rates
73
     * @param array              $options      An array of query options
74
     *
75
     * @return \Exchanger\ExchangeRate
76
     */
77
    private function quote($currencyPair, \DateTimeInterface $date = null, array $options = [])
78
    {
79
        $exchangeQueryBuilder = new ExchangeRateQueryBuilder($currencyPair);
80
81
        if (null !== $date) {
82
            $exchangeQueryBuilder->setDate($date);
83
        }
84
85
        foreach ($options as $name => $value) {
86
            $exchangeQueryBuilder->addOption($name, $value);
87
        }
88
89
        $query = $exchangeQueryBuilder->build();
90
91
        return $this->exchangeRateProvider->getExchangeRate($query);
92
    }
93
}
94