Passed
Push — master ( 9453fd...f75e31 )
by Orkhan
02:37
created

Currencylayer::apiRates()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Orkhanahmadov\LaravelCurrencylayer;
4
5
use Carbon\Carbon;
6
use Illuminate\Contracts\Cache\Repository;
7
use Illuminate\Support\Arr;
8
use OceanApplications\currencylayer\client;
9
use Orkhanahmadov\LaravelCurrencylayer\Models\Currency;
10
11
class Currencylayer
12
{
13
    /**
14
     * @var client
15
     */
16
    private $client;
17
18
    /**
19
     * Currencylayer constructor.
20
     *
21
     * @param client $client
22
     */
23
    public function __construct(client $client)
24
    {
25
        $this->client = $client;
26
    }
27
28
    /**
29
     * @param Currency|string $source
30
     * @param mixed ...$currencies
31
     *
32
     * @return array|float
33
     */
34
    public function live($source, ...$currencies)
35
    {
36
        if (! $source instanceof Currency) {
37
            $source = Currency::firstOrCreate(['code' => $source]);
38
        }
39
40
        $currencies = Arr::flatten($currencies);
41
        $apiResponse = $this->apiRates($source, $currencies);
42
43
        $rates = $this->createRates($source, $apiResponse['quotes'], $apiResponse['timestamp']);
44
45
        return count($currencies) === 1 ? array_values($rates)[0] : $rates;
46
    }
47
48
    /**
49
     * @param Currency|string $source
50
     * @param Carbon|string $date
51
     * @param mixed ...$currencies
52
     *
53
     * @return array|float
54
     */
55
    public function rateFor($source, $date, ...$currencies)
56
    {
57
        if (! $source instanceof Currency) {
58
            $source = Currency::firstOrCreate(['code' => $source]);
59
        }
60
61
        if (! $date instanceof Carbon) {
62
            $date = Carbon::parse($date);
63
        }
64
65
        $currencies = Arr::flatten($currencies);
66
        $apiResponse = $this->apiRates($source, $currencies, $date);
67
68
        $rates = $this->createRates($source, $apiResponse['quotes'], $apiResponse['timestamp']);
69
70
        return count($currencies) === 1 ? array_values($rates)[0] : $rates;
71
    }
72
73
    /**
74
     * @param Currency $source
75
     * @param array $quotes
76
     * @param int $timestamp
77
     *
78
     * @return array
79
     */
80
    private function createRates(Currency $source, array $quotes, int $timestamp): array
81
    {
82
        $rates = [];
83
84
        foreach ($quotes as $code => $rate) {
85
            $targetCurrency = Currency::firstOrCreate(['code' => $targetCurrencyCode = substr($code, -3)]);
86
87
            $existingRate = $source->rates()->where([
88
                'target_currency_id' => $targetCurrency->id,
89
                'timestamp' => Carbon::parse($timestamp),
90
            ])->first();
91
92
            if ($existingRate) {
93
                $rates[$targetCurrencyCode] = $existingRate->rate;
94
            } else {
95
                $createdRate = $source->rates()->create([
96
                    'target_currency_id' => $targetCurrency->id,
97
                    'rate' => $rate,
98
                    'timestamp' => $timestamp,
99
                ]);
100
                $rates[$targetCurrencyCode] = $createdRate->rate;
101
            }
102
        }
103
104
        return $rates;
105
    }
106
107
    /**
108
     * @param Currency $source
109
     * @param array $currencies
110
     * @param Carbon|null $date
111
     *
112
     * @return array
113
     */
114
    private function apiRates(Currency $source, array $currencies, ?Carbon $date = null): array
115
    {
116
        $client = $this->client->source($source->code)->currencies(implode(',', $currencies));
0 ignored issues
show
Bug introduced by
The property code does not seem to exist on Orkhanahmadov\LaravelCurrencylayer\Models\Currency. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
117
118
        return $date ? $client->date($date->format('Y-m-d'))->historical() : $client->live();
119
    }
120
}
121