|null $lastPayload */ public function broadcastAfterPriceUpdate(?array $lastPayload = null, bool $force = false): void { $ws = app(WebsocketBroadcastService::class); if (! $ws->isEnabled()) { return; } if ($force) { $ws->sendEvent('price_update', []); return; } if ($lastPayload === null) { $cache = app(LivePriceCacheService::class); $lastPayload = $cache->get() ?? $cache->buildFromDatabase(); } $lastChanged = is_array($lastPayload) && $lastPayload !== [] && $this->rememberHashIfChanged(self::LAST_HASH_KEY, $lastPayload); $listPayload = $this->buildProductListPayload(); $listChanged = $listPayload !== null && $this->rememberHashIfChanged(self::LIST_HASH_KEY, $listPayload); if ($lastChanged || $listChanged) { $ws->sendEvent('price_update', []); } } /** * @param array|array $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|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; } }