DummyProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 66
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A latest() 0 10 3
B historical() 0 26 5
1
<?php
2
3
namespace Ultraleet\CurrencyRates\Providers;
4
5
use Ultraleet\CurrencyRates\AbstractProvider;
6
use Ultraleet\CurrencyRates\Result;
7
use DateTime;
8
use InvalidArgumentException;
9
10
class DummyProvider extends AbstractProvider
11
{
12
13
    /**
14
     * @var array List of currencies to use. Mimics the currencies provided by fixer.io (ECB).
15
     */
16
    private $currencies = [
17
        'AUD', 'BGN', 'BRL', 'CAD', 'CHF', 'CNY', 'CZK', 'DKK', 'EUR', 'GBP',
18
        'HKD', 'HRK', 'HUF', 'IDR', 'ILS', 'INR', 'JPY', 'KRW', 'MXN', 'MYR',
19
        'NOK', 'NZD', 'PHP', 'PLN', 'RON', 'RUB', 'SEK', 'SGD', 'THB', 'TRY',
20
        'USD', 'ZAR'
21
    ];
22
23
    /**
24
     * Get latest currency exchange rates.
25
     *
26
     * @param  string  $base
27
     * @param  array   $targets
28
     * @return \Ultraleet\CurrencyRates\Contracts\Result
29
     */
30
    public function latest($base = 'EUR', $targets = [])
31
    {
32
        $date = date('Y-m-d');
33
        $day = date('w');
34
35
        if (!$day || $day == 6) { // saturday or sunday
36
            $date = date('Y-m-d', strtotime('last Friday'));
37
        }
38
39
        return $this->historical(new DateTime($date), $base, $targets);
40
    }
41
42
    /**
43
     * Get historical currency exchange rates.
44
     *
45
     * @param  \DateTime $date
46
     * @param  string    $base
47
     * @param  array     $targets
48
     * @return \Ultraleet\CurrencyRates\Contracts\Result
49
     */
50
    public function historical($date, $base = 'EUR', $targets = [])
51
    {
52
        $key = array_search($base, $this->currencies);
53
        if ($key === false) {
54
            throw new InvalidArgumentException("Invalid base currency specified: $base");
55
        }
56
57
        if (empty($targets)) {
58
            $currencies = $this->currencies;
59
            unset($currencies[$key]);
60
        } else {
61
            $currencies = $targets;
62
        }
63
64
        // generate dummy results
65
        $res = [];
66
        foreach ($currencies as $currency) {
67
            if (!in_array($currency, $this->currencies)) {
68
                throw new InvalidArgumentException("Invalid target currency specified: $currency");
69
            }
70
71
            $res[$currency] = mt_rand(10000, 150000) / 100000;
72
        }
73
74
        // return the results
75
        return new Result($base, $date, $res);
76
    }
77
}
78