@@ -14,6 +14,7 @@ class Update extends Command
14
14
* @var string
15
15
*/
16
16
protected $ signature = 'currency:update
17
+ {--e|exchangeratesapi : Get rates from ExchangeRatesApi.io}
17
18
{--o|openexchangerates : Get rates from OpenExchangeRates.org}
18
19
{--g|google : Get rates from Google Finance} ' ;
19
20
@@ -45,6 +46,7 @@ public function __construct()
45
46
* Execute the console command for Laravel 5.4 and below
46
47
*
47
48
* @return void
49
+ * @throws \Exception
48
50
*/
49
51
public function fire ()
50
52
{
@@ -55,12 +57,18 @@ public function fire()
55
57
* Execute the console command.
56
58
*
57
59
* @return void
60
+ * @throws \Exception
58
61
*/
59
62
public function handle ()
60
63
{
61
64
// Get Settings
62
65
$ defaultCurrency = $ this ->currency ->config ('default ' );
63
66
67
+ if ($ this ->input ->getOption ('exchangeratesapi ' )) {
68
+ // Get rates from exchangeratesapi
69
+ return $ this ->updateFromExchangeRatesApi ($ defaultCurrency );
70
+ }
71
+
64
72
if ($ this ->input ->getOption ('google ' )) {
65
73
// Get rates from google
66
74
return $ this ->updateFromGoogle ($ defaultCurrency );
@@ -78,11 +86,43 @@ public function handle()
78
86
}
79
87
}
80
88
89
+ /**
90
+ * Fetch rates from the API
91
+ *
92
+ * @param $defaultCurrency
93
+ */
94
+ private function updateFromExchangeRatesApi ($ defaultCurrency )
95
+ {
96
+ $ this ->info ('Updating currency exchange rates from ExchangeRatesApi.io... ' );
97
+
98
+ // Make request
99
+ $ content = json_decode ($ this ->request ("https://api.exchangeratesapi.io/latest?base= {$ defaultCurrency }" ));
100
+
101
+ // Error getting content?
102
+ if (isset ($ content ->error )) {
103
+ $ this ->error ($ content ->description );
104
+
105
+ return ;
106
+ }
107
+
108
+ // Update each rate
109
+ foreach ($ content ->rates as $ code => $ value ) {
110
+ $ this ->currency ->getDriver ()->update ($ code , [
111
+ 'exchange_rate ' => $ value ,
112
+ ]);
113
+ }
114
+
115
+ $ this ->currency ->clearCache ();
116
+
117
+ $ this ->info ('Update! ' );
118
+ }
119
+
81
120
/**
82
121
* Fetch rates from the API
83
122
*
84
123
* @param $defaultCurrency
85
124
* @param $api
125
+ * @throws \Exception
86
126
*/
87
127
private function updateFromOpenExchangeRates ($ defaultCurrency , $ api )
88
128
{
0 commit comments