Skip to content

Commit 09db6eb

Browse files
committed
Add support for caching Firebase authentication tokens
1 parent 0d698c4 commit 09db6eb

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# CHANGELOG
22

33
## Unreleased
4+
### Added
5+
* Added support for caching the authentication tokens used for connecting to the Firebase servers.
46

57
## 3.3.0 - 2021-11-29
68
### Added

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"php": "^7.2|^8.0",
1515
"kreait/firebase-php": "^5.24",
1616
"illuminate/contracts": "^6.0|^7.0|^8.0",
17-
"illuminate/support": "^6.0|^7.0|^8.0"
17+
"illuminate/support": "^6.0|^7.0|^8.0",
18+
"symfony/cache": "^5.4|^6.0"
1819
},
1920
"require-dev": {
2021
"orchestra/testbench": "^4.0|^5.0|^6.0",

src/FirebaseProjectManager.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
use Kreait\Firebase\Exception\InvalidArgumentException;
99
use Kreait\Firebase\Factory;
1010
use Kreait\Firebase\Http\HttpClientOptions;
11+
use Psr\Cache\CacheItemPoolInterface;
12+
use Psr\SimpleCache\CacheInterface;
13+
use Symfony\Component\Cache\Adapter\Psr16Adapter;
14+
use Symfony\Component\Cache\Psr16Cache;
1115

1216
class FirebaseProjectManager
1317
{
@@ -93,9 +97,21 @@ protected function configure(string $name): FirebaseProject
9397
}
9498

9599
if ($cacheStore = $config['cache_store'] ?? null) {
96-
$factory = $factory->withVerifierCache(
97-
$this->app->make('cache')->store($cacheStore)
98-
);
100+
$cache = $this->app->make('cache')->store($cacheStore);
101+
102+
if ($cache instanceof CacheItemPoolInterface) {
103+
$psr6Cache = $cache;
104+
$psr16Cache = new Psr16Cache($cache);
105+
} elseif ($cache instanceof CacheInterface) {
106+
$psr6Cache = new Psr16Adapter($cache);
107+
$psr16Cache = $cache;
108+
} else {
109+
throw new InvalidArgumentException("The cache store must be an instance of a PSR-6 or PSR-16 cache");
110+
}
111+
112+
$factory = $factory
113+
->withVerifierCache($psr16Cache)
114+
->withAuthTokenCache($psr6Cache);
99115
}
100116

101117
if ($logChannel = $config['logging']['http_log_channel'] ?? null) {

tests/FirebaseProjectManagerTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
namespace Kreait\Laravel\Firebase\Tests;
66

7-
use Illuminate\Contracts\Cache\Repository;
87
use Kreait\Firebase;
98
use Kreait\Firebase\Exception\InvalidArgumentException;
109
use Kreait\Firebase\Factory;
1110
use Kreait\Laravel\Firebase\FirebaseProjectManager;
11+
use Psr\Cache\CacheItemPoolInterface;
12+
use Psr\SimpleCache\CacheInterface;
1213
use ReflectionObject;
1314

1415
/**
@@ -302,14 +303,27 @@ public function http_client_options_can_be_configured(): void
302303
/**
303304
* @test
304305
*/
305-
public function it_uses_the_laravel_cache(): void
306+
public function it_uses_the_laravel_cache_as_verifier_cache(): void
306307
{
307308
$projectName = $this->app->config->get('firebase.default');
308309
$factory = $this->factoryForProject($projectName);
309310

310311
$property = $this->getAccessibleProperty($factory, 'verifierCache');
311312

312-
$this->assertInstanceOf(Repository::class, $property->getValue($factory));
313+
$this->assertInstanceOf(CacheInterface::class, $property->getValue($factory));
314+
}
315+
316+
/**
317+
* @test
318+
*/
319+
public function it_uses_the_laravel_cache_as_auth_token_cache(): void
320+
{
321+
$projectName = $this->app->config->get('firebase.default');
322+
$factory = $this->factoryForProject($projectName);
323+
324+
$property = $this->getAccessibleProperty($factory, 'authTokenCache');
325+
326+
$this->assertInstanceOf(CacheItemPoolInterface::class, $property->getValue($factory));
313327
}
314328

315329
private function getAccessibleProperty(object $object, string $propertyName): \ReflectionProperty

0 commit comments

Comments
 (0)