1919use Psr \Http \Message \ResponseInterface ;
2020use Psr \Http \Message \StreamInterface ;
2121use Psr \Http \Message \UriInterface ;
22+ use Psr \Log \LoggerInterface ;
2223
2324class AbstractEndpoint
2425{
@@ -27,6 +28,8 @@ class AbstractEndpoint
2728
2829 private HttpMethodsClient $ httpClient ;
2930
31+ private ?LoggerInterface $ logger ;
32+
3033 private ?CacheItemPoolInterface $ cache ;
3134
3235 private bool $ cacheInvalidation = false ;
@@ -42,6 +45,7 @@ public function __construct(protected OpenWeatherMap $api)
4245 $ config = $ this ->api ->getConfig ();
4346
4447 $ this ->httpClient = $ config ->getHttpClientBuilder ()->getHttpClient ();
48+ $ this ->logger = $ config ->getLogger ();
4549 $ this ->cache = $ config ->getCache ();
4650 $ this ->measurementSystem = $ config ->getMeasurementSystem ();
4751 $ this ->language = $ config ->getLanguage ();
@@ -70,15 +74,19 @@ protected function sendRequest(
7074 if ($ this ->cache !== null ) {
7175 $ cacheKey = $ this ->getCacheKey ($ uri );
7276
73- // Invalidate cache (if exists) to force renewal
77+ // Invalidate cache to force new
7478 if ($ this ->cacheInvalidation === true ) {
79+ $ this ->logger ?->info('Cache invalidated ' , ['key ' => $ cacheKey ]);
80+
7581 $ this ->cache ->deleteItem ($ cacheKey );
7682 }
7783
7884 $ cacheItem = $ this ->cache ->getItem ($ cacheKey );
7985
80- // If cache does not exist...
81- if (!$ cacheItem ->isHit ()) {
86+ if ($ cacheItem ->isHit ()) {
87+ $ this ->logger ?->info(\sprintf ('Cache hit: %s %s ' , $ method , $ uri ), ['key ' => $ cacheKey ]);
88+ }
89+ else {
8290 $ response = ResponseMediator::toArray (
8391 $ this ->handleRequest ($ method , $ uri , $ headers , $ body )
8492 );
@@ -87,6 +95,8 @@ protected function sendRequest(
8795 $ cacheItem ->expiresAfter ($ this ->cacheTtl );
8896
8997 $ this ->cache ->save ($ cacheItem );
98+
99+ $ this ->logger ?->info('Cached response ' , ['ttl ' => $ this ->cacheTtl , 'key ' => $ cacheKey ]);
90100 }
91101
92102 return $ cacheItem ->get ();
0 commit comments