From 1400b710dc5ff28809043133bddded2448d4d9d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Baconnier?= Date: Wed, 20 Jul 2022 14:50:08 +0200 Subject: [PATCH] Refactor Macros and silent the exception from setScopes --- src/LaravelZohoServiceProvider.php | 35 ++++--------------- src/Macros/ErrorsFromZoho.php | 33 +++++++++++++++++ src/Macros/HasErrorsFromZoho.php | 20 +++++++++++ src/Macros/WithZohoHeader.php | 28 +++++++++++++++ src/Repositories/StorageConfigRepository.php | 3 +- .../StorageConfigRepositoryTest.php | 6 ++-- 6 files changed, 93 insertions(+), 32 deletions(-) create mode 100644 src/Macros/ErrorsFromZoho.php create mode 100644 src/Macros/HasErrorsFromZoho.php create mode 100644 src/Macros/WithZohoHeader.php diff --git a/src/LaravelZohoServiceProvider.php b/src/LaravelZohoServiceProvider.php index 7bbeeff..5fd3847 100644 --- a/src/LaravelZohoServiceProvider.php +++ b/src/LaravelZohoServiceProvider.php @@ -8,6 +8,9 @@ use Illuminate\Support\ServiceProvider; use MelbaCh\LaravelZoho\Auth\ZohoAuthProvider; use MelbaCh\LaravelZoho\Controllers\ZohoAuthController; +use MelbaCh\LaravelZoho\Macros\ErrorsFromZoho; +use MelbaCh\LaravelZoho\Macros\HasErrorsFromZoho; +use MelbaCh\LaravelZoho\Macros\WithZohoHeader; use MelbaCh\LaravelZoho\Repositories\AccessTokenRepository; use MelbaCh\LaravelZoho\Repositories\ConfigRepository; use MelbaCh\LaravelZoho\Repositories\StorageAccessTokenRepository; @@ -27,35 +30,9 @@ public function boot() ], 'laravel-zoho-migrations'); } - Http::macro('withZohoHeader', function () { - $accessTokenRepository = app(AccessTokenRepository::class); - $headers = []; - if ($token = $accessTokenRepository->get()) { - $headers['Authorization'] = "Zoho-oauthtoken {$token->getToken()}"; - } - return Http::withHeaders($headers); - }); - - Response::macro('hasErrorsFromZoho', function () { - return $this->status() >= 400 || count($this->errorsFromZoho()); - }); - - Response::macro('errorsFromZoho', function () { - if ($this->status() >= 400) { - return $this->json() ?? []; - } - - return collect($this->json()) - ->flatten(1) - ->filter(function ($value) { - if (is_array($value) && array_key_exists('status', $value)) { - return $value['status'] === 'error'; - } - return null; - }) - ->values() - ->toArray(); - }); + Http::macro('withZohoHeader', app(WithZohoHeader::class)()); + Response::macro('hasErrorsFromZoho', app(HasErrorsFromZoho::class)()); + Response::macro('errorsFromZoho', app(ErrorsFromZoho::class)()); Route::get(config('zoho.url', '/oauth2/zoho'), [ZohoAuthController::class, 'requestToken']); } diff --git a/src/Macros/ErrorsFromZoho.php b/src/Macros/ErrorsFromZoho.php new file mode 100644 index 0000000..d6751ea --- /dev/null +++ b/src/Macros/ErrorsFromZoho.php @@ -0,0 +1,33 @@ +status() >= 400) { + return $this->json() ?? []; + } + + return collect($this->json()) + ->flatten(1) + ->filter(function ($value) { + if (is_array($value) && array_key_exists('status', $value)) { + return $value['status'] === 'error'; + } + return null; + }) + ->values() + ->toArray(); + }; + } +} \ No newline at end of file diff --git a/src/Macros/HasErrorsFromZoho.php b/src/Macros/HasErrorsFromZoho.php new file mode 100644 index 0000000..a7313ae --- /dev/null +++ b/src/Macros/HasErrorsFromZoho.php @@ -0,0 +1,20 @@ +status() >= 400 || count($this->errorsFromZoho()) > 0; + }; + } +} \ No newline at end of file diff --git a/src/Macros/WithZohoHeader.php b/src/Macros/WithZohoHeader.php new file mode 100644 index 0000000..1c2e0a8 --- /dev/null +++ b/src/Macros/WithZohoHeader.php @@ -0,0 +1,28 @@ +get()) { + $headers['Authorization'] = "Zoho-oauthtoken {$token->getToken()}"; + } + + return Http::withHeaders($headers); + }; + } +} \ No newline at end of file diff --git a/src/Repositories/StorageConfigRepository.php b/src/Repositories/StorageConfigRepository.php index e2896f6..e39a467 100644 --- a/src/Repositories/StorageConfigRepository.php +++ b/src/Repositories/StorageConfigRepository.php @@ -38,7 +38,8 @@ public function scopes(): array public function setScopes(array $scopes): ConfigRepository { - throw new \Exception('`setScopes` is not available when using DefaultConfigRepository'); + // `setScopes` is not available when using DefaultConfigRepository + return $this; } public function secret(): string|null diff --git a/tests/Repositories/StorageConfigRepositoryTest.php b/tests/Repositories/StorageConfigRepositoryTest.php index 0f7eeb5..932f510 100644 --- a/tests/Repositories/StorageConfigRepositoryTest.php +++ b/tests/Repositories/StorageConfigRepositoryTest.php @@ -52,9 +52,11 @@ public function it_cannot_set_the_scopes(): void { $repository = app(StorageConfigRepository::class); - // Not supported - $this->expectException(\Exception::class); + $scopes = $repository->scopes(); + $repository->setScopes(['my-new-scope', 'another-new-scope']); + + $this->assertEquals($scopes, $repository->scopes()); } /** @test */