diff --git a/CHANGELOG.md b/CHANGELOG.md index 68d9d14..50333cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # CHANGELOG ## Unreleased +### Added +* Added support for caching the authentication tokens used for connecting to the Firebase servers. ## 3.3.0 - 2021-11-29 ### Added diff --git a/composer.json b/composer.json index 1659bff..7ae83a5 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "php": "^7.2|^8.0", "kreait/firebase-php": "^5.24", "illuminate/contracts": "^6.0|^7.0|^8.0", - "illuminate/support": "^6.0|^7.0|^8.0" + "illuminate/support": "^6.0|^7.0|^8.0", + "symfony/cache": "^5.4|^6.0" }, "require-dev": { "orchestra/testbench": "^4.0|^5.0|^6.0", diff --git a/src/FirebaseProjectManager.php b/src/FirebaseProjectManager.php index 3b75285..b2268b7 100644 --- a/src/FirebaseProjectManager.php +++ b/src/FirebaseProjectManager.php @@ -8,6 +8,10 @@ use Kreait\Firebase\Exception\InvalidArgumentException; use Kreait\Firebase\Factory; use Kreait\Firebase\Http\HttpClientOptions; +use Psr\Cache\CacheItemPoolInterface; +use Psr\SimpleCache\CacheInterface; +use Symfony\Component\Cache\Adapter\Psr16Adapter; +use Symfony\Component\Cache\Psr16Cache; class FirebaseProjectManager { @@ -93,9 +97,21 @@ protected function configure(string $name): FirebaseProject } if ($cacheStore = $config['cache_store'] ?? null) { - $factory = $factory->withVerifierCache( - $this->app->make('cache')->store($cacheStore) - ); + $cache = $this->app->make('cache')->store($cacheStore); + + if ($cache instanceof CacheItemPoolInterface) { + $psr6Cache = $cache; + $psr16Cache = new Psr16Cache($cache); + } elseif ($cache instanceof CacheInterface) { + $psr6Cache = new Psr16Adapter($cache); + $psr16Cache = $cache; + } else { + throw new InvalidArgumentException("The cache store must be an instance of a PSR-6 or PSR-16 cache"); + } + + $factory = $factory + ->withVerifierCache($psr16Cache) + ->withAuthTokenCache($psr6Cache); } if ($logChannel = $config['logging']['http_log_channel'] ?? null) { diff --git a/tests/FirebaseProjectManagerTest.php b/tests/FirebaseProjectManagerTest.php index cefce9b..f3b628c 100644 --- a/tests/FirebaseProjectManagerTest.php +++ b/tests/FirebaseProjectManagerTest.php @@ -4,11 +4,12 @@ namespace Kreait\Laravel\Firebase\Tests; -use Illuminate\Contracts\Cache\Repository; use Kreait\Firebase; use Kreait\Firebase\Exception\InvalidArgumentException; use Kreait\Firebase\Factory; use Kreait\Laravel\Firebase\FirebaseProjectManager; +use Psr\Cache\CacheItemPoolInterface; +use Psr\SimpleCache\CacheInterface; use ReflectionObject; /** @@ -302,14 +303,27 @@ public function http_client_options_can_be_configured(): void /** * @test */ - public function it_uses_the_laravel_cache(): void + public function it_uses_the_laravel_cache_as_verifier_cache(): void { $projectName = $this->app->config->get('firebase.default'); $factory = $this->factoryForProject($projectName); $property = $this->getAccessibleProperty($factory, 'verifierCache'); - $this->assertInstanceOf(Repository::class, $property->getValue($factory)); + $this->assertInstanceOf(CacheInterface::class, $property->getValue($factory)); + } + + /** + * @test + */ + public function it_uses_the_laravel_cache_as_auth_token_cache(): void + { + $projectName = $this->app->config->get('firebase.default'); + $factory = $this->factoryForProject($projectName); + + $property = $this->getAccessibleProperty($factory, 'authTokenCache'); + + $this->assertInstanceOf(CacheItemPoolInterface::class, $property->getValue($factory)); } private function getAccessibleProperty(object $object, string $propertyName): \ReflectionProperty