|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace VictorAvelar\Geld\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
|
8
|
|
|
use Illuminate\Support\Facades\Log; |
|
9
|
|
|
use VictorAvelar\Fixer\Resources\LatestRatesResource; |
|
10
|
|
|
use VictorAvelar\Geld\Events\RatesUpdated; |
|
11
|
|
|
|
|
12
|
|
|
class UpdateExchangeRatesCommand extends Command |
|
13
|
|
|
{ |
|
14
|
|
|
protected $signature = 'geld:update |
|
15
|
|
|
{--dry : Execute without saving to the database} |
|
16
|
|
|
{--no-history : Do not track the history for this execution}'; |
|
17
|
|
|
|
|
18
|
|
|
protected $description = 'Updates the exchange rates on currencies table'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var LatestRatesResource |
|
22
|
|
|
*/ |
|
23
|
|
|
private $latestRatesResource; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* UpdateExchangeRatesCommand constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param LatestRatesResource $latestRatesResource |
|
29
|
|
|
*/ |
|
30
|
15 |
|
public function __construct(LatestRatesResource $latestRatesResource) |
|
31
|
|
|
{ |
|
32
|
15 |
|
parent::__construct(); |
|
33
|
15 |
|
$this->latestRatesResource = $latestRatesResource; |
|
34
|
15 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Execute the update exchange rates workflow. |
|
38
|
|
|
* |
|
39
|
|
|
* @throws GuzzleException |
|
40
|
|
|
*/ |
|
41
|
12 |
|
public function handle() |
|
42
|
|
|
{ |
|
43
|
12 |
|
$rates = $this->latestRatesResource->execute(); |
|
44
|
|
|
|
|
45
|
12 |
|
$info = json_decode($rates->getBody()->getContents(), true); |
|
46
|
|
|
|
|
47
|
12 |
|
if ($this->option('dry')) { |
|
48
|
|
|
$this->printRates($info); |
|
49
|
|
|
} else { |
|
50
|
12 |
|
$this->saveExchangeRates($info['rates']); |
|
51
|
12 |
|
event(new RatesUpdated()); |
|
52
|
|
|
} |
|
53
|
12 |
|
Log::info('Currency import completed'); |
|
54
|
12 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param $info |
|
58
|
|
|
*/ |
|
59
|
|
|
private function printRates($info): void |
|
60
|
|
|
{ |
|
61
|
|
|
$this->info("Rates pulled at {$info['timestamp']}"); |
|
62
|
|
|
foreach ($info['rates'] as $code => $rate) { |
|
63
|
|
|
$this->info(sprintf('Current rate for %s is %f', $code, $rate)); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Saves the information to the database. |
|
69
|
|
|
* @param array $rates |
|
70
|
|
|
*/ |
|
71
|
12 |
|
private function saveExchangeRates(array $rates) |
|
72
|
|
|
{ |
|
73
|
12 |
|
$cu = now(); |
|
74
|
12 |
|
$data = []; |
|
75
|
12 |
|
$all = empty(config('geld.supported')); |
|
76
|
12 |
|
foreach ($rates as $code => $rate) { |
|
77
|
12 |
|
if ($all or in_array($code, config('geld.supported'))) { |
|
78
|
|
|
$record = [ |
|
79
|
12 |
|
'code' => $code, |
|
80
|
12 |
|
'rate' => $rate, |
|
81
|
12 |
|
'created_at' => $cu, |
|
82
|
12 |
|
'updated_at' => $cu |
|
83
|
|
|
]; |
|
84
|
12 |
|
Db::table('currencies') |
|
85
|
12 |
|
->updateOrInsert( |
|
86
|
12 |
|
['code' => $code], |
|
87
|
4 |
|
$record |
|
88
|
|
|
); |
|
89
|
12 |
|
$data[] = $record; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
12 |
|
if (!$this->option('no-history') or !config('geld.history_mode')) { |
|
94
|
12 |
|
Db::table('currency_history')->insert($data); |
|
95
|
|
|
} |
|
96
|
12 |
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|