This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateExchangeRates.php
81 lines (72 loc) · 2.06 KB
/
UpdateExchangeRates.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace PWWEB\Core\Jobs;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use Log;
use PWWEB\Core\Models\Currency;
use PWWEB\Core\Models\ExchangeRate;
class UpdateExchangeRates implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Date to run the update for.
*
* @var string
*/
protected $date;
/**
* Create a new job instance.
*
* @param Carbon $date [description]
*
* @return void
*/
public function __construct(Carbon $date)
{
$this->date = $date->toDateString();
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// $ecb = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
$api = 'https://api.exchangeratesapi.io/'.$this->date.'?base=GBP';
$response = Http::get($api);
if (true === $response->successful()) {
$data = $response->json();
$rates = [];
$currencies = Currency::select('id', 'iso')->get();
foreach ($currencies as $currency) {
if (true === isset($data['rates'][$currency->iso])) {
$rates[] = [
'currency_id' => $currency->id,
'rate' => $data['rates'][$currency->iso],
'date' => $this->date,
];
}
}
ExchangeRate::upsert($rates, ['currency_id', 'date'], ['rate']);
Log::info('Exchange Rates Updated');
} else {
Log::error('Exchange Rate API Failed.');
}//end if
}
/**
* The unique ID of the job.
*
* @return string
*/
public function uniqueId(): string
{
return $this->date;
}
}