format('Y-m-d'), $fromCurrency->code, $toCurrency->code); $result = Requests::get($uri); $rate = 1.0; $content = null; if ($result->status_code !== 200) { Log::error(sprintf('Something went wrong. Received error code %d and body "%s" from FixerIO.', $result->status_code, $result->body)); } // get rate from body: if ($result->status_code === 200) { $content = json_decode($result->body, true); } if (!is_null($content)) { $code = $toCurrency->code; $rate = isset($content['rates'][$code]) ? $content['rates'][$code] : '1'; } // create new currency exchange rate object: $exchangeRate = new CurrencyExchangeRate; $exchangeRate->user()->associate($this->user); $exchangeRate->fromCurrency()->associate($fromCurrency); $exchangeRate->toCurrency()->associate($toCurrency); $exchangeRate->date = $date; $exchangeRate->rate = $rate; $exchangeRate->save(); return $exchangeRate; } /** * @param User $user * * @return mixed|void */ public function setUser(User $user) { $this->user = $user; } }