-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Macros and silent the exception from setScopes
- Loading branch information
1 parent
a007100
commit 1400b71
Showing
6 changed files
with
93 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters