Skip to content

Commit

Permalink
Refactor Macros and silent the exception from setScopes
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaconnier committed Jul 20, 2022
1 parent a007100 commit 1400b71
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 32 deletions.
35 changes: 6 additions & 29 deletions src/LaravelZohoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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']);
}
Expand Down
33 changes: 33 additions & 0 deletions src/Macros/ErrorsFromZoho.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace MelbaCh\LaravelZoho\Macros;

use Illuminate\Http\Response;

/**
*
* @mixin Response
* @return array
*/
class ErrorsFromZoho
{
public function __invoke()
{
return function (): array {
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();
};
}
}
20 changes: 20 additions & 0 deletions src/Macros/HasErrorsFromZoho.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace MelbaCh\LaravelZoho\Macros;

use Illuminate\Http\Response;

/**
*
* @mixin Response
* @return bool
*/
class HasErrorsFromZoho
{
public function __invoke()
{
return function (): bool {
return $this->status() >= 400 || count($this->errorsFromZoho()) > 0;
};
}
}
28 changes: 28 additions & 0 deletions src/Macros/WithZohoHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace MelbaCh\LaravelZoho\Macros;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
use MelbaCh\LaravelZoho\Repositories\AccessTokenRepository;

/**
*
* @mixin \Illuminate\Http\Client\PendingRequest
* @return \Illuminate\Http\Client\PendingRequest
*/
class WithZohoHeader
{
public function __invoke()
{
return function (): PendingRequest {
$accessTokenRepository = app(AccessTokenRepository::class);
$headers = [];
if ($token = $accessTokenRepository->get()) {
$headers['Authorization'] = "Zoho-oauthtoken {$token->getToken()}";
}

return Http::withHeaders($headers);
};
}
}
3 changes: 2 additions & 1 deletion src/Repositories/StorageConfigRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions tests/Repositories/StorageConfigRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 1400b71

Please sign in to comment.