Files
boursecu_server/app/Services/LivePriceCacheService.php
T

113 lines
3.6 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;
}
/** پاسخ سریع بدون API خارجی — از آخرین رکورد prices + تنظیمات */
public function buildFromDatabase(): array
{
$config = app(ConfigService::class);
$display = app(ProductDisplayPriceService::class);
$last = Price::orderByDesc('id')->first();
$base = $last ? (int) $last->price : 0;
2026-06-14 12:01:20 +03:30
$provider = $config->priceProvider();
$montzarPrice = 0;
$zaarboxPrice = 0;
$mainProductPrice = 0;
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 {
$diffBuy = (int) $config->displayBuySpreadRial();
$diffSell = (int) $config->displaySellSpreadRial();
2026-06-14 12:01:20 +03:30
$openBuy = true;
$openSell = true;
if ($provider === 'montzar') {
$montzarPrice = $config->getInt('montzar_live_price', 0) ?: $base;
$montzarDiff = (int) $config->getInt('montzar_diff', 0);
$diffBuy += $montzarDiff;
$diffSell += $montzarDiff;
$openBuy = $config->getBool('montzar_open_buy', true);
$openSell = $config->getBool('montzar_open_sell', true);
} elseif ($provider === 'zaarbox') {
$zaarboxPrice = $base;
} elseif ($provider === 'main_product') {
$mainProductPrice = $base;
}
$buy = $openBuy ? (int) ceil($base + $diffBuy) : 0;
$sell = $openSell ? max(0, (int) ceil($base - $diffSell)) : 0;
2026-06-13 17:35:40 +03:30
$payload = [
'sell' => $sell,
'buy' => $buy,
'price_last' => $buy,
'price' => $base,
2026-06-14 12:01:20 +03:30
'main_product' => $mainProductPrice,
'montzar' => $montzarPrice,
'zaarbox' => $zaarboxPrice,
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();
2026-06-14 12:01:20 +03:30
$display->cacheLivePricePayload($payload);
2026-06-13 17:35:40 +03:30
return $payload;
}
}