From 19a3bae9188c0b3705e070c7f7cba9816ac92d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Abi-Khalil?= Date: Tue, 20 Feb 2024 15:24:45 +0100 Subject: [PATCH] Fix cache when we have time out with navitia. --- Cache/Navitia.php | 25 +++++++++++++++++++------ Service/NavitiaService.php | 5 ++++- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Cache/Navitia.php b/Cache/Navitia.php index 578caef..e5ad07e 100644 --- a/Cache/Navitia.php +++ b/Cache/Navitia.php @@ -84,7 +84,7 @@ private function checkPublicationDate(): void } $currentPublicationDate = $this->getPublicationDate(); - if ($currentPublicationDate !== $publicationDateInCache) { + if ($currentPublicationDate !== null && $currentPublicationDate !== $publicationDateInCache) { $cacheItem->set($currentPublicationDate); $this->cache->save($cacheItem); $this->cache->invalidateTags([$this->getCacheTag()]); @@ -119,10 +119,23 @@ public function setCacheItem(string $cacheKey, $data): void private function getPublicationDate(): string { - $url = $this->urlApi.'coverage/'.$this->coverage.'/'.self::PUBLICATION_DATE_API; - $ch = new CurlService($url, 6000, $this->token, $this->logger); - $curlResponse = $ch->process(); - $response = json_decode($curlResponse['response']); - return $response->status->publication_date; + try { + $url = $this->urlApi.'coverage/'.$this->coverage.'/'.self::PUBLICATION_DATE_API; + $ch = new CurlService($url, 6000, $this->token, $this->logger); + $curlResponse = $ch->process(); + $response = json_decode($curlResponse['response']); + + return $response->status->publication_date; + } catch (\Exception $e) { + $this->logger->warning( + 'Error while getting publication date', + [ + 'api' => $url, + 'error' => $e->getMessage() + ] + ); + + return null; + } } } diff --git a/Service/NavitiaService.php b/Service/NavitiaService.php index 5db3772..c3cdad8 100644 --- a/Service/NavitiaService.php +++ b/Service/NavitiaService.php @@ -186,9 +186,12 @@ private function getApiResponse( try { return $this->cache->getCachedItem($cacheKey); } catch (CacheItemNotFoundException $e) { + $ch = new CurlService($url, $this->timeout, $token, $this->logger); $result = $ch->process(); - $this->cache->setCacheItem($cacheKey, $result); + if ($result['httpCode'] === 200) { + $this->cache->setCacheItem($cacheKey, $result); + } } } else { $ch = new CurlService($url, $this->timeout, $token, $this->logger);