Skip to content

Commit

Permalink
Add support for caching Firebase authentication tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Dec 4, 2021
1 parent 0d698c4 commit 09db6eb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 19 additions & 3 deletions src/FirebaseProjectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 17 additions & 3 deletions tests/FirebaseProjectManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 09db6eb

Please sign in to comment.