5
5
use Carbon \Carbon ;
6
6
use Illuminate \Contracts \Cache \Repository ;
7
7
use Illuminate \Support \Arr ;
8
+ use Illuminate \Support \Collection ;
8
9
use OceanApplications \currencylayer \client ;
9
10
use Orkhanahmadov \LaravelCurrencylayer \Models \Currency ;
10
11
@@ -29,6 +30,35 @@ public function __construct(client $client)
29
30
$ this ->client = $ client ;
30
31
}
31
32
33
+ public function live ($ source , ...$ currencies )
34
+ {
35
+ $ currencies = Arr::flatten ($ currencies );
36
+
37
+ $ response = $ this ->apiRate ($ source , implode (', ' , $ currencies ));
38
+
39
+ $ rates = [];
40
+ $ sourceCurrency = Currency::firstOrCreate (['code ' => $ source ]);
41
+ foreach ($ response ['quotes ' ] as $ code => $ rate ) {
42
+ $ targetCurrency = Currency::firstOrCreate (['code ' => $ targetCurrencyCode = substr ($ code , -3 )]);
43
+
44
+ $ createdRate = $ sourceCurrency ->rates ()->create ([
45
+ 'target_currency_id ' => $ targetCurrency ->id ,
46
+ 'rate ' => $ rate ,
47
+ 'timestamp ' => $ response ['timestamp ' ],
48
+ ]);
49
+ $ rates [$ targetCurrencyCode ] = $ createdRate ->rate ;
50
+ }
51
+
52
+ return count ($ currencies ) === 1 ? array_values ($ rates )[0 ] : $ rates ;
53
+ }
54
+
55
+ private function apiRate (string $ source , string $ currencies , ?string $ date = null ): array
56
+ {
57
+ $ client = $ this ->client ->source ($ source )->currencies ($ currencies );
58
+
59
+ return $ date ? $ client ->date ($ date )->historical () : $ client ->live ();
60
+ }
61
+
32
62
/**
33
63
* @param Carbon|string $date
34
64
*
@@ -51,65 +81,48 @@ public function date($date): self
51
81
// return $this;
52
82
// }
53
83
54
- /**
55
- * @param Currency|string $source
56
- * @param Currency|string $target
57
- */
58
- public function rate ($ source , $ target )
59
- {
60
- if (! $ source instanceof Currency) {
61
- $ source = Currency::where ('code ' , $ source )->first ();
62
- }
63
-
64
- if (! $ target instanceof Currency) {
65
- $ target = Currency::where ('code ' , $ target )->first ();
66
- }
67
-
68
- if (! $ source || ! $ target ) {
69
- throw new \InvalidArgumentException (
70
- 'Source or target currency is not available. Did you fetch all currencies? ' .
71
- 'Call currencies() method to fetch all available currencies. '
72
- );
73
- }
74
-
75
- $ source ->rates ()->where ('target_currency_id ' , $ target ->id )->latest ()->first ();
76
-
77
- // $currencies = Arr::flatten($currencies);
84
+ // /**
85
+ // * @param Currency|string $source
86
+ // * @param Currency|string $target
87
+ // * @param Carbon|string|null $date
88
+ // *
89
+ // * @return float
90
+ // */
91
+ // public function rate($source, $target, $date = null)
92
+ // {
93
+ // if (! $source instanceof Currency) {
94
+ // $source = Currency::where('code', $source)->first();
95
+ // }
78
96
//
79
- // return $this->client
80
- // ->source($source)
81
- // ->currencies(implode(',', $currencies))
82
- // ->live();
83
- }
84
-
85
- public function fetch (string $ source , ...$ currencies )
86
- {
87
- $ client = $ this ->client ->source ($ source )->currencies (implode (', ' , Arr::flatten ($ currencies )));
88
- $ response = $ this ->date ? $ client ->date ($ this ->date )->historical () : $ client ->live ();
89
-
90
- $ sourceCurrency = Currency::where ('code ' , $ response ['source ' ])->first ();
97
+ // if (! $target instanceof Currency) {
98
+ // $target = Currency::where('code', $target)->first();
99
+ // }
100
+ //
101
+ // if (! $source || ! $target) {
102
+ // throw new \InvalidArgumentException(
103
+ // 'Source or target currency is not available. Did you fetch all currencies? ' .
104
+ // 'Call currencies() method to fetch all available currencies.'
105
+ // );
106
+ // }
107
+ //
108
+ // if (! $date) {
109
+ // $this->fetch($source->code, $target->code);
110
+ // }
111
+ //
112
+ // return $source->rate($target)->rate;
113
+ // }
91
114
92
- foreach ($ response ['quotes ' ] as $ code => $ rate ) {
93
- $ targetCurrency = Currency::where ('code ' , substr ($ code , -3 ))->first ();
94
- if ($ sourceCurrency && $ targetCurrency ) {
95
- $ sourceCurrency ->targetRates ()->create ([
96
- 'target_currency_id ' => $ targetCurrency ->id ,
97
- 'rate ' => $ rate ,
98
- 'rate_for ' => $ response ['timestamp ' ],
99
- ]);
100
- }
101
- }
102
- }
103
115
104
- public function currencies ()
105
- {
106
- $ response = $ this ->client ->list ();
107
116
108
- foreach ($ response ['currencies ' ] as $ code => $ name ) {
109
- $ currency = Currency::where ('code ' , $ code )->first ();
110
- if (! $ currency ) {
111
- Currency::create (['code ' => $ code , 'name ' => $ name ]);
112
- }
113
- }
114
- }
117
+ // public function currencies()
118
+ // {
119
+ // $response = $this->client->list();
120
+ //
121
+ // foreach ($response['currencies'] as $code => $name) {
122
+ // $currency = Currency::where('code', $code)->first();
123
+ // if (! $currency) {
124
+ // Currency::create(['code' => $code, 'name' => $name]);
125
+ // }
126
+ // }
127
+ // }
115
128
}
0 commit comments