Skip to content

Commit

Permalink
Use only one instance of GcpMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Mar 19, 2020
1 parent 7d11727 commit 90616d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/Firebase/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ class Factory
*/
protected $httpClientMiddlewares = [];

/**
* @var GcpMetadata
*/
protected $gcpMetadata;

protected static $databaseUriPattern = 'https://%s.firebaseio.com';

protected static $storageBucketNamePattern = '%s.appspot.com';
Expand All @@ -98,7 +103,8 @@ class Factory

public function __construct()
{
$this->serviceAccountDiscoverer = new Discoverer();
$this->gcpMetadata = new GcpMetadata(new Client());
$this->serviceAccountDiscoverer = new Discoverer([], $this->gcpMetadata);
$this->clock = new SystemClock();
}

Expand Down Expand Up @@ -415,7 +421,7 @@ protected function createGoogleAuthTokenMiddleware(): AuthTokenMiddleware
'client_id' => $serviceAccount->getClientId(),
'private_key' => $serviceAccount->getPrivateKey(),
]);
} elseif ((new GcpMetadata())->isAvailable()) {
} elseif ($this->gcpMetadata->isAvailable()) {
// @codeCoverageIgnoreStart
// We can't test this programatically when not on GCE/GCP
$credentials = new GCECredentials();
Expand Down
11 changes: 9 additions & 2 deletions src/Firebase/ServiceAccount/Discoverer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Google\Auth\CredentialsLoader;
use Kreait\Firebase\Exception\ServiceAccountDiscoveryFailed;
use Kreait\Firebase\ServiceAccount;
use Kreait\GcpMetadata;
use Throwable;

class Discoverer
Expand All @@ -16,12 +17,18 @@ class Discoverer
*/
private $methods;

/**
* @var GcpMetadata
*/
private $gcpMetadata;

/**
* @param callable[] $methods
*/
public function __construct(array $methods = null)
public function __construct(array $methods = null, GcpMetadata $gcpMetadata = null)
{
$this->methods = $methods ?? $this->getDefaultMethods();
$this->gcpMetadata = $gcpMetadata ?? new GcpMetadata();
}

public function getDefaultMethods(): array
Expand All @@ -30,7 +37,7 @@ public function getDefaultMethods(): array
new Discovery\FromEnvironmentVariable('FIREBASE_CREDENTIALS'),
new Discovery\FromEnvironmentVariable(CredentialsLoader::ENV_VAR),
new Discovery\FromGoogleWellKnownFile(),
new Discovery\OnGoogleCloudPlatform(),
new Discovery\OnGoogleCloudPlatform($this->gcpMetadata),
];
}

Expand Down

0 comments on commit 90616d2

Please sign in to comment.