Files
boursecu_server/app/Services/LivePriceCacheService.php
T

104 lines
3.4 KiB
PHP
Raw Normal View History

2026-06-13 17:35:40 +03:30
<?php
namespace App\Services;
use App\Models\Price;
use Illuminate\Support\Facades\Cache;
class LivePriceCacheService
{
public const FULL_RESPONSE_KEY = 'live_price_full_response';
public const TTL_SECONDS = 15;
public function get(): ?array
{
$cached = Cache::get(self::FULL_RESPONSE_KEY);
if (! is_array($cached) || ! isset($cached['body'], $cached['stored_at'])) {
return null;
}
if (now()->diffInSeconds($cached['stored_at']) > self::TTL_SECONDS) {
return null;
}
return $cached['body'];
}
public function put(array $body): array
{
Cache::put(self::FULL_RESPONSE_KEY, [
'body' => $body,
'stored_at' => now(),
], now()->addSeconds(self::TTL_SECONDS + 5));
app(ProductDisplayPriceService::class)->cacheLivePricePayload($body);
return $body;
}
2026-06-14 12:33:29 +03:30
/** پاسخ سریع بدون API خارجی — از آخرین رکورد prices + تنظیمات (همان فرمول lastPrice) */
2026-06-13 17:35:40 +03:30
public function buildFromDatabase(): array
{
$config = app(ConfigService::class);
$display = app(ProductDisplayPriceService::class);
$last = Price::orderByDesc('id')->first();
2026-06-14 12:33:29 +03:30
$base = $last ? (float) $last->price : 0.0;
2026-06-14 12:01:20 +03:30
$provider = $config->priceProvider();
2026-06-13 17:35:40 +03:30
if ($base <= 0) {
$payload = [
'sell' => 0,
'buy' => 0,
'price_last' => 0,
'price' => 0,
'main_product' => 0,
'montzar' => 0,
'zaarbox' => 0,
'closed' => true,
'time' => now(),
];
} else {
2026-06-14 12:33:29 +03:30
$montzarSource = 0;
$zaarboxSource = 0;
$mainProductSource = 0;
$priceSource = 'database_last';
2026-06-14 12:01:20 +03:30
if ($provider === 'montzar') {
2026-06-14 12:33:29 +03:30
$liveMontzar = (float) $config->get('montzar_live_price', 0);
$montzarSource = (int) ($liveMontzar > 0 ? $liveMontzar : $base);
// DB از آخرین tick montzar پر شده — همان فرمول نمایش معامله
$priceSource = 'montzar';
2026-06-14 12:01:20 +03:30
} elseif ($provider === 'zaarbox') {
2026-06-14 12:33:29 +03:30
$zaarboxSource = (int) $base;
2026-06-14 12:01:20 +03:30
} elseif ($provider === 'main_product') {
2026-06-14 12:33:29 +03:30
$mainProductSource = (int) $base;
2026-06-14 12:01:20 +03:30
}
2026-06-14 12:33:29 +03:30
$trade = $config->buildTradeDisplayPrices($base, $priceSource);
2026-06-13 17:35:40 +03:30
$payload = [
2026-06-14 12:33:29 +03:30
'sell' => $trade['sell'],
'buy' => $trade['buy'],
'price_last' => $trade['priceLast'],
'price' => (int) $base,
'main_product' => $mainProductSource,
'montzar' => $montzarSource,
'zaarbox' => $zaarboxSource,
2026-06-13 17:35:40 +03:30
'closed' => false,
'time' => now(),
];
}
$payload['up'] = $config->resolvedDailyBaseExtremes()['up'] > 0
? [
'sell' => max(0, (int) ceil($config->resolvedDailyBaseExtremes()['down'] - $config->displaySellSpreadRial())),
'buy' => (int) ceil($config->resolvedDailyBaseExtremes()['up'] + $config->displayBuySpreadRial()),
]
: ['sell' => $payload['sell'] ?? 0, 'buy' => $payload['buy'] ?? 0];
$payload['last_historical'] = $display->resolveHistoricalBasePrice();
return $payload;
}
}