Skip to content

Commit

Permalink
fix: changes post code review
Browse files Browse the repository at this point in the history
  • Loading branch information
clemblanco authored and jeromegamez committed Oct 5, 2023
1 parent 7a01421 commit 6ae431e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
9 changes: 1 addition & 8 deletions config/firebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* ------------------------------------------------------------------------
*/
'projects' => [

'app' => [
/*
* ------------------------------------------------------------------------
Expand Down Expand Up @@ -47,7 +46,6 @@
* first time you try to access a component of the Firebase Admin SDK.
*
*/

'credentials' => env('FIREBASE_CREDENTIALS', env('GOOGLE_APPLICATION_CREDENTIALS')),

/*
Expand Down Expand Up @@ -93,7 +91,6 @@
],

'dynamic_links' => [

/*
* Dynamic links can be built with any URL prefix registered on
*
Expand All @@ -105,9 +102,7 @@
* The value must be a valid domain, for example,
* https://example.page.link
*/

'default_domain' => env('FIREBASE_DYNAMIC_LINKS_DEFAULT_DOMAIN'),

],

/*
Expand All @@ -117,7 +112,6 @@
*/

'storage' => [

/*
* Your project's default storage bucket usually uses the project ID
* as its name. If you have multiple storage buckets and want to
Expand All @@ -126,7 +120,6 @@
*/

'default_bucket' => env('FIREBASE_STORAGE_DEFAULT_BUCKET'),

],

/*
Expand Down Expand Up @@ -185,7 +178,7 @@
*/
'timeout' => env('FIREBASE_HTTP_CLIENT_TIMEOUT'),

// 'guzzle_middlewares' => [],
'guzzle_middlewares' => [],
],
],
],
Expand Down
35 changes: 17 additions & 18 deletions src/FirebaseProjectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Arr;
use Kreait\Firebase\Exception\InvalidArgumentException;
use Kreait\Firebase\Factory;
use Kreait\Firebase\Http\HttpClientOptions;
Expand Down Expand Up @@ -48,7 +47,7 @@ protected function configuration(string $name): array
return $config;
}

protected function resolveCredentials(string $credentials): string
protected function resolveJsonCredentials(string $credentials): string
{
$isJsonString = \str_starts_with($credentials, '{');
$isAbsoluteLinuxPath = \str_starts_with($credentials, '/');
Expand All @@ -65,33 +64,31 @@ protected function configure(string $name): FirebaseProject

$config = $this->configuration($name);

if ($tenantId = Arr::get($config, 'auth.tenant_id')) {
if ($tenantId = $config['auth']['tenant_id'] ?? null) {
$factory = $factory->withTenantId($tenantId);
}

if ($credentials = Arr::get($config, 'credentials.file', Arr::get($config, 'credentials'))) {
if ($credentials = $config['credentials']['file'] ?? ($config['credentials'] ?? null)) {
if (is_string($credentials)) {
$factory = $factory->withServiceAccount($this->resolveCredentials($credentials));
$credentials = $this->resolveJsonCredentials($credentials);
}

if (is_array($credentials) && Arr::has($credentials, ['type', 'project_id'])) {
$factory = $factory->withServiceAccount($credentials);
}
$factory = $factory->withServiceAccount($credentials);
}

if ($databaseUrl = Arr::get($config, 'database.url')) {
if ($databaseUrl = $config['database']['url'] ?? null) {
$factory = $factory->withDatabaseUri($databaseUrl);
}

if ($authVariableOverride = Arr::get($config, 'database.auth_variable_override')) {
if ($authVariableOverride = $config['database']['auth_variable_override'] ?? null) {
$factory = $factory->withDatabaseAuthVariableOverride($authVariableOverride);
}

if ($defaultStorageBucket = Arr::get($config, 'storage.default_bucket')) {
if ($defaultStorageBucket = $config['storage']['default_bucket'] ?? null) {
$factory = $factory->withDefaultStorageBucket($defaultStorageBucket);
}

if ($cacheStore = Arr::get($config, 'cache_store')) {
if ($cacheStore = $config['cache_store'] ?? null) {
$cache = $this->app->make('cache')->store($cacheStore);

if ($cache instanceof CacheInterface) {
Expand All @@ -100,32 +97,34 @@ protected function configure(string $name): FirebaseProject
throw new InvalidArgumentException('The cache store must be an instance of a PSR-6 or PSR-16 cache');
}

$factory = $factory->withVerifierCache($cache)->withAuthTokenCache($cache);
$factory = $factory
->withVerifierCache($cache)
->withAuthTokenCache($cache);
}

if ($logChannel = Arr::get($config, 'logging.http_log_channel')) {
if ($logChannel = $config['logging']['http_log_channel'] ?? null) {
$factory = $factory->withHttpLogger(
$this->app->make('log')->channel($logChannel)
);
}

if ($logChannel = Arr::get($config, 'logging.http_debug_log_channel')) {
if ($logChannel = $config['logging']['http_debug_log_channel'] ?? null) {
$factory = $factory->withHttpDebugLogger(
$this->app->make('log')->channel($logChannel)
);
}

$options = HttpClientOptions::default();

if ($proxy = Arr::get($config, 'http_client_options.proxy')) {
if ($proxy = $config['http_client_options']['proxy'] ?? null) {
$options = $options->withProxy($proxy);
}

if ($timeout = Arr::get($config, 'http_client_options.timeout')) {
if ($timeout = $config['http_client_options']['timeout'] ?? null) {
$options = $options->withTimeOut((float) $timeout);
}

if ($middlewares = Arr::get($config, 'http_client_options.guzzle_middlewares')) {
if ($middlewares = $config['http_client_options']['guzzle_middlewares'] ?? null) {
$options = $options->withGuzzleMiddlewares($middlewares);
}

Expand Down
21 changes: 21 additions & 0 deletions tests/FirebaseProjectManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ public function credentials_can_be_configured_using_a_json_file(): void
$this->assertSame($credentials, $serviceAccount);
}

/**
* @test
*/
public function json_file_credentials_can_be_used_using_the_deprecated_configuration_entry(): void
{
// Reference credentials
$credentialsPath = \realpath(__DIR__ . '/_fixtures/service_account.json');
$credentials = \json_decode(\file_get_contents($credentialsPath), true);

// Set configuration and retrieve project
$projectName = 'app';
$this->app->config->set('firebase.projects.' . $projectName . '.credentials.file', \realpath(__DIR__ . '/_fixtures/service_account.json'));
$factory = $this->factoryForProject($projectName);

// Retrieve service account
$serviceAccount = $this->getAccessibleProperty($factory, 'serviceAccount')->getValue($factory);

// Validate value
$this->assertSame($credentials, $serviceAccount);
}

/**
* @test
*/
Expand Down

0 comments on commit 6ae431e

Please sign in to comment.