Skip to content

Commit 59f0e2c

Browse files
committed
Use dependency injection for API Exception converters
1 parent 36ba6fa commit 59f0e2c

12 files changed

+47
-38
lines changed

src/Firebase/AppCheck/ApiClient.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
*/
2020
final class ApiClient
2121
{
22-
private readonly AppCheckApiExceptionConverter $errorHandler;
23-
2422
public function __construct(
2523
private readonly ClientInterface $client,
24+
private readonly AppCheckApiExceptionConverter $errorHandler,
2625
) {
27-
$this->errorHandler = new AppCheckApiExceptionConverter();
2826
}
2927

3028
/**

src/Firebase/Auth/ApiClient.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ class ApiClient
3838

3939
private readonly AuthResourceUrlBuilder $authResourceUrlBuilder;
4040

41-
private readonly AuthApiExceptionConverter $errorHandler;
42-
4341
/**
4442
* @param non-empty-string $projectId
4543
* @param non-empty-string|null $tenantId
@@ -50,9 +48,8 @@ public function __construct(
5048
private readonly ClientInterface $client,
5149
private readonly GuzzleHandler $signInHandler,
5250
private readonly ClockInterface $clock,
51+
private readonly AuthApiExceptionConverter $errorHandler,
5352
) {
54-
$this->errorHandler = new AuthApiExceptionConverter();
55-
5653
$this->awareAuthResourceUrlBuilder = $tenantId !== null
5754
? TenantAwareAuthResourceUrlBuilder::forProjectAndTenant($projectId, $tenantId)
5855
: ProjectAwareAuthResourceUrlBuilder::forProject($projectId);

src/Firebase/Database/ApiClient.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
*/
2020
class ApiClient
2121
{
22-
protected DatabaseApiExceptionConverter $errorHandler;
23-
2422
public function __construct(
2523
private readonly ClientInterface $client,
2624
private readonly UrlBuilder $resourceUrlBuilder,
25+
private readonly DatabaseApiExceptionConverter $errorHandler,
2726
) {
28-
$this->errorHandler = new DatabaseApiExceptionConverter();
2927
}
3028

3129
/**

src/Firebase/Exception/AppCheckApiExceptionConverter.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818
*/
1919
final class AppCheckApiExceptionConverter
2020
{
21-
private readonly ErrorResponseParser $responseParser;
22-
23-
public function __construct()
21+
public function __construct(private readonly ErrorResponseParser $responseParser)
2422
{
25-
$this->responseParser = new ErrorResponseParser();
2623
}
2724

2825
public function convertException(Throwable $exception): AppCheckException

src/Firebase/Exception/AuthApiExceptionConverter.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@
3232
*/
3333
class AuthApiExceptionConverter
3434
{
35-
private readonly ErrorResponseParser $responseParser;
36-
37-
public function __construct()
35+
public function __construct(private readonly ErrorResponseParser $responseParser)
3836
{
39-
$this->responseParser = new ErrorResponseParser();
4037
}
4138

4239
public function convertException(Throwable $exception): AuthException

src/Firebase/Exception/DatabaseApiExceptionConverter.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
*/
2121
class DatabaseApiExceptionConverter
2222
{
23-
private readonly ErrorResponseParser $responseParser;
24-
25-
public function __construct()
23+
public function __construct(private readonly ErrorResponseParser $responseParser)
2624
{
27-
$this->responseParser = new ErrorResponseParser();
2825
}
2926

3027
public function convertException(Throwable $exception): DatabaseException

src/Firebase/Exception/RemoteConfigApiExceptionConverter.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@
2222
*/
2323
class RemoteConfigApiExceptionConverter
2424
{
25-
private readonly ErrorResponseParser $responseParser;
26-
27-
public function __construct()
25+
public function __construct(private readonly ErrorResponseParser $responseParser)
2826
{
29-
$this->responseParser = new ErrorResponseParser();
3027
}
3128

3229
public function convertException(Throwable $exception): RemoteConfigException

src/Firebase/Factory.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@
2828
use Kreait\Firebase\Auth\CustomTokenViaGoogleCredentials;
2929
use Kreait\Firebase\Auth\SignIn\GuzzleHandler;
3030
use Kreait\Firebase\Database\UrlBuilder;
31+
use Kreait\Firebase\Exception\AppCheckApiExceptionConverter;
32+
use Kreait\Firebase\Exception\AuthApiExceptionConverter;
33+
use Kreait\Firebase\Exception\DatabaseApiExceptionConverter;
3134
use Kreait\Firebase\Exception\InvalidArgumentException;
3235
use Kreait\Firebase\Exception\MessagingApiExceptionConverter;
36+
use Kreait\Firebase\Exception\RemoteConfigApiExceptionConverter;
3337
use Kreait\Firebase\Exception\RuntimeException;
38+
use Kreait\Firebase\Http\ErrorResponseParser;
3439
use Kreait\Firebase\Http\HttpClientOptions;
3540
use Kreait\Firebase\Http\Middleware;
3641
use Kreait\Firebase\JWT\IdTokenVerifier;
@@ -119,6 +124,8 @@ final class Factory
119124

120125
private HttpClientOptions $httpClientOptions;
121126

127+
private ErrorResponseParser $errorResponseParser;
128+
122129
/**
123130
* @var array<non-empty-string, mixed>
124131
*/
@@ -139,6 +146,7 @@ public function __construct()
139146
$this->defaultCache = new InMemoryCache($this->clock);
140147
$this->httpFactory = new HttpFactory();
141148
$this->httpClientOptions = HttpClientOptions::default();
149+
$this->errorResponseParser = new ErrorResponseParser();
142150
}
143151

144152
/**
@@ -375,7 +383,7 @@ public function createAppCheck(): Contract\AppCheck
375383
);
376384

377385
return new AppCheck(
378-
new AppCheck\ApiClient($http),
386+
new AppCheck\ApiClient($http, new AppCheckApiExceptionConverter($this->errorResponseParser)),
379387
new AppCheckTokenGenerator(
380388
$serviceAccount->clientEmail,
381389
$serviceAccount->privateKey,
@@ -392,7 +400,14 @@ public function createAuth(): Contract\Auth
392400
$httpClient = $this->createApiClient();
393401

394402
$signInHandler = new GuzzleHandler($projectId, $httpClient);
395-
$authApiClient = new ApiClient($projectId, $this->tenantId, $httpClient, $signInHandler, $this->clock);
403+
$authApiClient = new ApiClient(
404+
$projectId,
405+
$this->tenantId,
406+
$httpClient,
407+
$signInHandler,
408+
$this->clock,
409+
new AuthApiExceptionConverter($this->errorResponseParser),
410+
);
396411
$customTokenGenerator = $this->createCustomTokenGenerator();
397412
$idTokenVerifier = $this->createIdTokenVerifier();
398413
$sessionCookieVerifier = $this->createSessionCookieVerifier();
@@ -413,7 +428,11 @@ public function createDatabase(): Contract\Database
413428

414429
return new Database(
415430
GuzzleUtils::uriFor($databaseUrl),
416-
new Database\ApiClient($http, $resourceUrlBuilder),
431+
new Database\ApiClient(
432+
$http,
433+
$resourceUrlBuilder,
434+
new DatabaseApiExceptionConverter($this->errorResponseParser),
435+
),
417436
);
418437
}
419438

@@ -423,7 +442,13 @@ public function createRemoteConfig(): Contract\RemoteConfig
423442
'base_uri' => "https://firebaseremoteconfig.googleapis.com/v1/projects/{$this->getProjectId()}/remoteConfig",
424443
]);
425444

426-
return new RemoteConfig(new RemoteConfig\ApiClient($this->getProjectId(), $http));
445+
return new RemoteConfig(
446+
new RemoteConfig\ApiClient(
447+
$this->getProjectId(),
448+
$http,
449+
new RemoteConfigApiExceptionConverter($this->errorResponseParser),
450+
),
451+
);
427452
}
428453

429454
public function createMessaging(): Contract\Messaging

src/Firebase/RemoteConfig/ApiClient.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
*/
1919
class ApiClient
2020
{
21-
private readonly RemoteConfigApiExceptionConverter $errorHandler;
22-
2321
private readonly string $baseUri;
2422

25-
public function __construct(string $projectId, private readonly ClientInterface $client)
26-
{
23+
public function __construct(
24+
string $projectId,
25+
private readonly ClientInterface $client,
26+
private readonly RemoteConfigApiExceptionConverter $errorHandler,
27+
) {
2728
$this->baseUri = "https://firebaseremoteconfig.googleapis.com/v1/projects/{$projectId}/remoteConfig";
28-
$this->errorHandler = new RemoteConfigApiExceptionConverter();
2929
}
3030

3131
/**

tests/Unit/Exception/AppCheckApiExceptionConverterTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Kreait\Firebase\Exception\AppCheck\AppCheckError;
1515
use Kreait\Firebase\Exception\AppCheck\PermissionDenied;
1616
use Kreait\Firebase\Exception\AppCheckApiExceptionConverter;
17+
use Kreait\Firebase\Http\ErrorResponseParser;
1718
use PHPUnit\Framework\Attributes\DataProvider;
1819
use PHPUnit\Framework\Attributes\Test;
1920
use PHPUnit\Framework\TestCase;
@@ -30,7 +31,7 @@ final class AppCheckApiExceptionConverterTest extends TestCase
3031

3132
protected function setUp(): void
3233
{
33-
$this->converter = new AppCheckApiExceptionConverter();
34+
$this->converter = new AppCheckApiExceptionConverter(new ErrorResponseParser());
3435
}
3536

3637
#[Test]

0 commit comments

Comments
 (0)