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.
This commit is contained in:
Ümit Tunç
2025-01-17 22:58:34 +03:00
parent 48da9891dc
commit 86f887a18b
+17 -21
View File
@@ -9,6 +9,7 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use App\Helpers\NumberFormatter;
class FetchCurrencyRates implements ShouldQueue class FetchCurrencyRates implements ShouldQueue
{ {
@@ -20,10 +21,7 @@ class FetchCurrencyRates implements ShouldQueue
$data['Update_Date'] = now()->format('Y-m-d H:i:s'); $data['Update_Date'] = now()->format('Y-m-d H:i:s');
// Döviz kurları // Döviz kurları
$response = Http::withHeaders([ $response = $this->fetchData('https://kur.doviz.com');
'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');
// DOM işlemleri için veri çekme // DOM işlemleri için veri çekme
$dom = new \DOMDocument(); $dom = new \DOMDocument();
@@ -35,44 +33,42 @@ class FetchCurrencyRates implements ShouldQueue
foreach ($elements as $element) { foreach ($elements as $element) {
$name = $element->getAttribute('data-socket-key'); $name = $element->getAttribute('data-socket-key');
$type = $element->getAttribute('data-socket-attr'); $type = $element->getAttribute('data-socket-attr');
$value = $this->virgulToNokta(trim($element->nodeValue)); $value = NumberFormatter::commaToDot($element->nodeValue);
if (trim($name) !== '') { if (trim($name) !== '') {
if ($name == "JPY") $value = $value / 100; if ($name == "JPY") $value = $value / 100;
if ($type == "bid") $data[$name]['Buying'] = $value; if (strlen($name) === 3) {
if ($type == "ask") $data[$name]['Selling'] = $value; if ($type == "bid") $data[$name]['Buying'] = (float)$value;
if ($type == "c") $data[$name]['Change'] = $value; if ($type == "ask") $data[$name]['Selling'] = (float)$value;
$data[$name]['Type'] = "Currency"; if ($type == "c") $data[$name]['Change'] = round((float)$value, 2);
$data[$name]['Type'] = "Currency";
}
} }
} }
// AZN kuru için // 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); preg_match('/(\d+\.\d+)\s+TRY/', $aznResponse->body(), $matches);
if (isset($matches[1])) { if (isset($matches[1])) {
$data['AZN'] = [ $data['AZN'] = [
'Buying' => $matches[1], 'Buying' => NumberFormatter::commaToDot($matches[1]),
'Selling' => $matches[1], 'Selling' => NumberFormatter::commaToDot($matches[1]),
'Change' => "0.00", 'Change' => "0.00",
'Type' => "Currency" '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 // JSON dosyasını kaydet
Storage::put('currency/today.json', json_encode($data, JSON_UNESCAPED_UNICODE)); Storage::put('currency/today.json', json_encode($data, JSON_UNESCAPED_UNICODE));
return $data; return $data;
} }
private function virgulToNokta($text) private function fetchData($url)
{ {
$text = str_replace(".", "", $text); return Http::withHeaders([
$text = str_replace(",", ".", $text); 'User-Agent' => 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10',
$text = str_replace("%", "", $text); 'Accept-Language' => 'en'
return $text; ])->get($url);
} }
} }