Files
finance/app/Jobs/FetchCurrencyRates.php
Ümit Tunç 86f887a18b 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.
2025-01-17 22:58:34 +03:00

75 lines
2.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
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
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle()
{
$data = [];
$data['Update_Date'] = now()->format('Y-m-d H:i:s');
// Döviz kurları
$response = $this->fetchData('https://kur.doviz.com');
// DOM işlemleri için veri çekme
$dom = new \DOMDocument();
@$dom->loadHTML($response->body());
$xpath = new \DOMXPath($dom);
// Döviz kurlarını çekme
$elements = $xpath->query("//*[@data-socket-key]");
foreach ($elements as $element) {
$name = $element->getAttribute('data-socket-key');
$type = $element->getAttribute('data-socket-attr');
$value = NumberFormatter::commaToDot($element->nodeValue);
if (trim($name) !== '') {
if ($name == "JPY") $value = $value / 100;
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 = $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' => NumberFormatter::commaToDot($matches[1]),
'Selling' => NumberFormatter::commaToDot($matches[1]),
'Change' => "0.00",
'Type' => "Currency"
];
}
// JSON dosyasını kaydet
Storage::put('currency/today.json', json_encode($data, JSON_UNESCAPED_UNICODE));
return $data;
}
private function fetchData($url)
{
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);
}
}