From 86f887a18b909c9f9208c0fab01dfa85b95b88b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Cmit=20Tun=C3=A7?= Date: Fri, 17 Jan 2025 22:58:34 +0300 Subject: [PATCH] Refactor FetchCurrencyRates job to improve data fetching and processing - Introduced a new helper method `fetchData` to streamline HTTP requests for currency data. - Replaced direct HTTP calls with the new `fetchData` method for better code organization. - Utilized `NumberFormatter` for consistent number formatting, replacing the previous `virgulToNokta` method. - Enhanced data handling for currency rates, ensuring proper type casting and rounding for values. - Removed commented-out code related to gold rates to clean up the implementation. These changes enhance the maintainability and readability of the currency fetching logic. --- app/Jobs/FetchCurrencyRates.php | 38 +++++++++++++++------------------ 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/app/Jobs/FetchCurrencyRates.php b/app/Jobs/FetchCurrencyRates.php index 367ff5b..d1642cf 100644 --- a/app/Jobs/FetchCurrencyRates.php +++ b/app/Jobs/FetchCurrencyRates.php @@ -9,6 +9,7 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Storage; +use App\Helpers\NumberFormatter; class FetchCurrencyRates implements ShouldQueue { @@ -20,10 +21,7 @@ class FetchCurrencyRates implements ShouldQueue $data['Update_Date'] = now()->format('Y-m-d H:i:s'); // Döviz kurları - $response = Http::withHeaders([ - 'User-Agent' => 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10', - 'Accept-Language' => 'en' - ])->get('https://kur.doviz.com'); + $response = $this->fetchData('https://kur.doviz.com'); // DOM işlemleri için veri çekme $dom = new \DOMDocument(); @@ -35,44 +33,42 @@ class FetchCurrencyRates implements ShouldQueue foreach ($elements as $element) { $name = $element->getAttribute('data-socket-key'); $type = $element->getAttribute('data-socket-attr'); - $value = $this->virgulToNokta(trim($element->nodeValue)); + $value = NumberFormatter::commaToDot($element->nodeValue); if (trim($name) !== '') { if ($name == "JPY") $value = $value / 100; - if ($type == "bid") $data[$name]['Buying'] = $value; - if ($type == "ask") $data[$name]['Selling'] = $value; - if ($type == "c") $data[$name]['Change'] = $value; - $data[$name]['Type'] = "Currency"; + if (strlen($name) === 3) { + if ($type == "bid") $data[$name]['Buying'] = (float)$value; + if ($type == "ask") $data[$name]['Selling'] = (float)$value; + if ($type == "c") $data[$name]['Change'] = round((float)$value, 2); + $data[$name]['Type'] = "Currency"; + } } } // AZN kuru için - $aznResponse = Http::get('https://wise.com/tr/currency-converter/azn-to-try-rate?amount=1'); + $aznResponse = $this->fetchData('https://wise.com/tr/currency-converter/azn-to-try-rate?amount=1'); preg_match('/(\d+\.\d+)\s+TRY/', $aznResponse->body(), $matches); if (isset($matches[1])) { $data['AZN'] = [ - 'Buying' => $matches[1], - 'Selling' => $matches[1], + 'Buying' => NumberFormatter::commaToDot($matches[1]), + 'Selling' => NumberFormatter::commaToDot($matches[1]), 'Change' => "0.00", 'Type' => "Currency" ]; } - // Altın kurları - $goldResponse = Http::get('https://altin.doviz.com'); - // ... Altın kurları için benzer DOM işlemleri ... - // JSON dosyasını kaydet Storage::put('currency/today.json', json_encode($data, JSON_UNESCAPED_UNICODE)); return $data; } - private function virgulToNokta($text) + private function fetchData($url) { - $text = str_replace(".", "", $text); - $text = str_replace(",", ".", $text); - $text = str_replace("%", "", $text); - return $text; + return Http::withHeaders([ + 'User-Agent' => 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10', + 'Accept-Language' => 'en' + ])->get($url); } }