self::CACHE_TTL) { @unlink($file); return null; } $content = @file_get_contents($file); if ($content === false) { return null; } $data = @json_decode($content, true); if (!is_array($data)) { @unlink($file); return null; } return $data; } private static function writeCacheFile($type, $data) { $file = self::getCacheFilePath($type); $content = json_encode($data); @file_put_contents($file, $content, LOCK_EX); } private static function fetchAndCachePrices() { try { $response = Api::external("GET", "/pricesall?compress=0&output=json", array("timeout" => 300)); if ($response['code'] !== 200) { Log::write("SyncCache: Price API error code " . $response['code'], Log::ERROR, Log::CONTEXT_CRONJOB); return array(); } $decoded = json_decode($response['body'], true); $priceItems = isset($decoded[0]) ? $decoded[0] : $decoded; if (!is_array($priceItems)) { Log::write("SyncCache: Invalid price API response", Log::ERROR, Log::CONTEXT_CRONJOB); return array(); } $map = array(); foreach ($priceItems as $item) { $skus = isset($item['sku']) ? $item['sku'] : array(); if (!is_array($skus)) $skus = array($skus); $basePrice = isset($item['prezzo unita']) ? floatval(str_replace(',', '.', $item['prezzo unita'])) : null; if ($basePrice !== null) { foreach ($skus as $sku) { if ($sku) { $map[$sku] = array('base_price' => $basePrice); } } } } self::writeCacheFile('prices', $map); Log::write("SyncCache: Cached " . count($map) . " price entries to file", Log::INFO, Log::CONTEXT_CRONJOB); return $map; } catch (\Throwable $e) { Log::write("SyncCache: Failed to fetch prices - " . $e->getMessage(), Log::ERROR, Log::CONTEXT_CRONJOB); return array(); } } private static function fetchAndCacheQuantities() { try { $response = Api::external("GET", "/stockall?compress=0&output=json", array("timeout" => 300)); if ($response['code'] !== 200) { Log::write("SyncCache: Quantity API error code " . $response['code'], Log::ERROR, Log::CONTEXT_CRONJOB); return array(); } $decoded = json_decode($response['body'], true); if (!is_array($decoded)) { Log::write("SyncCache: Invalid quantity API response", Log::ERROR, Log::CONTEXT_CRONJOB); return array(); } $map = array(); foreach ($decoded as $sku => $stockData) { if (is_array($stockData)) { $map[$sku] = $stockData; } } self::writeCacheFile('quantities', $map); Log::write("SyncCache: Cached " . count($map) . " quantity entries to file", Log::INFO, Log::CONTEXT_CRONJOB); return $map; } catch (\Throwable $e) { Log::write("SyncCache: Failed to fetch quantities - " . $e->getMessage(), Log::ERROR, Log::CONTEXT_CRONJOB); return array(); } } }