182 lines
5.3 KiB
PHP
182 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Http\Controllers\Admin\ProductController;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class PriceBroadcastService
|
|
{
|
|
private const LAST_HASH_KEY = 'price_broadcast:last_hash';
|
|
|
|
private const LIST_HASH_KEY = 'price_broadcast:list_hash';
|
|
|
|
/**
|
|
* برادکست SSE — payload کامل last/list وقتی داده عوض شود.
|
|
*
|
|
* @param array<string, mixed>|null $lastPayload
|
|
*/
|
|
public function broadcastAfterPriceUpdate(?array $lastPayload = null, bool $force = false): void
|
|
{
|
|
$stream = app(PriceStreamService::class);
|
|
if (! $stream->isEnabled()) {
|
|
return;
|
|
}
|
|
|
|
if ($lastPayload === null) {
|
|
$cache = app(LivePriceCacheService::class);
|
|
$lastPayload = $cache->get() ?? $cache->buildFromDatabase();
|
|
}
|
|
|
|
if ($force) {
|
|
if (is_array($lastPayload) && $lastPayload !== []) {
|
|
$stream->publish('last', $lastPayload);
|
|
}
|
|
|
|
$listPayload = $this->buildProductListPayload();
|
|
if ($listPayload !== null) {
|
|
$stream->publish('list', $listPayload);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
$lastChanged = is_array($lastPayload) && $lastPayload !== []
|
|
&& $this->rememberHashIfChanged(self::LAST_HASH_KEY, self::lastPriceIdentity($lastPayload));
|
|
|
|
if ($lastChanged && is_array($lastPayload)) {
|
|
$stream->publish('last', $lastPayload);
|
|
$this->publishListIfChanged(true);
|
|
} else {
|
|
$this->publishListIfChanged();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $payload
|
|
* @return array<string, int|bool>
|
|
*/
|
|
public static function lastPriceIdentity(array $payload): array
|
|
{
|
|
return [
|
|
'sell' => (int) ($payload['sell'] ?? 0),
|
|
'buy' => (int) ($payload['buy'] ?? 0),
|
|
'price_last' => (int) ($payload['price_last'] ?? 0),
|
|
'price' => (int) ($payload['price'] ?? 0),
|
|
'closed' => (bool) ($payload['closed'] ?? false),
|
|
'main_product' => (int) ($payload['main_product'] ?? 0),
|
|
'montzar' => (int) ($payload['montzar'] ?? 0),
|
|
'zaarbox' => (int) ($payload['zaarbox'] ?? 0),
|
|
];
|
|
}
|
|
|
|
public function broadcastLastIfChanged(?array $lastPayload): void
|
|
{
|
|
$stream = app(PriceStreamService::class);
|
|
if (! $stream->isEnabled()) {
|
|
return;
|
|
}
|
|
|
|
if (! is_array($lastPayload) || $lastPayload === []) {
|
|
return;
|
|
}
|
|
|
|
if (! $this->rememberHashIfChanged(self::LAST_HASH_KEY, self::lastPriceIdentity($lastPayload))) {
|
|
return;
|
|
}
|
|
|
|
$stream->publish('last', $lastPayload);
|
|
$this->publishListIfChanged(true);
|
|
}
|
|
|
|
public function buildListPayload(): ?array
|
|
{
|
|
return $this->buildProductListPayload();
|
|
}
|
|
|
|
public function broadcastListIfChanged(bool $force = false): bool
|
|
{
|
|
$stream = app(PriceStreamService::class);
|
|
if (! $stream->isEnabled()) {
|
|
return false;
|
|
}
|
|
|
|
return $this->publishListIfChanged($force);
|
|
}
|
|
|
|
private function publishListIfChanged(bool $force = false): bool
|
|
{
|
|
$listPayload = $this->buildProductListPayload();
|
|
if ($listPayload === null || $listPayload === []) {
|
|
return false;
|
|
}
|
|
|
|
if (! $force && ! $this->rememberHashIfChanged(self::LIST_HASH_KEY, $this->listIdentity($listPayload))) {
|
|
return false;
|
|
}
|
|
|
|
if ($force) {
|
|
$this->rememberHashIfChanged(self::LIST_HASH_KEY, $this->listIdentity($listPayload));
|
|
}
|
|
|
|
app(PriceStreamService::class)->publish('list', $listPayload);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @param array<int, array<string, mixed>> $listPayload
|
|
* @return array<int, array<string, int|bool>>
|
|
*/
|
|
private function listIdentity(array $listPayload): array
|
|
{
|
|
$identity = [];
|
|
|
|
foreach ($listPayload as $product) {
|
|
if (! is_array($product)) {
|
|
continue;
|
|
}
|
|
|
|
$identity[] = [
|
|
'id' => (int) ($product['id'] ?? 0),
|
|
'price' => (int) ($product['price'] ?? 0),
|
|
'price_profit' => (int) ($product['price_profit'] ?? 0),
|
|
'show' => (bool) ($product['show'] ?? true),
|
|
'active' => (bool) ($product['active'] ?? true),
|
|
];
|
|
}
|
|
|
|
return $identity;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed>|array<int, mixed> $payload
|
|
*/
|
|
private function rememberHashIfChanged(string $cacheKey, array $payload): bool
|
|
{
|
|
$hash = md5(json_encode($payload, JSON_UNESCAPED_UNICODE));
|
|
if (Cache::get($cacheKey) === $hash) {
|
|
return false;
|
|
}
|
|
|
|
Cache::put($cacheKey, $hash, now()->addHour());
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @return array<int, mixed>|null
|
|
*/
|
|
private function buildProductListPayload(): ?array
|
|
{
|
|
$request = Request::create('/customer/product/list', 'GET');
|
|
app()->instance('request', $request);
|
|
|
|
$response = app(ProductController::class)->listProduct();
|
|
$decoded = json_decode($response->getContent(), true);
|
|
|
|
return is_array($decoded) ? $decoded : null;
|
|
}
|
|
}
|