diff --git a/src/Dtos/OnOfficeApiCredentials.php b/src/Dtos/OnOfficeApiCredentials.php new file mode 100644 index 0000000..9d40a53 --- /dev/null +++ b/src/Dtos/OnOfficeApiCredentials.php @@ -0,0 +1,14 @@ +credentials = new OnOfficeApiCredentials(token: $token, secret: $secret, apiClaim: $apiClaim); + + return $this; + } protected function getOnOfficeService(): OnOfficeService { - return $this->onOfficeService ?? $this->createOnOfficeService(); + return tap($this->onOfficeService ?? $this->createOnOfficeService(), + fn (OnOfficeService $service) => $service->setCredentials($this->credentials) + ); } protected function createOnOfficeService(): OnOfficeService diff --git a/src/Services/OnOfficeService.php b/src/Services/OnOfficeService.php index 8c3bcab..34649f7 100644 --- a/src/Services/OnOfficeService.php +++ b/src/Services/OnOfficeService.php @@ -12,6 +12,7 @@ use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; +use Innobrain\OnOfficeAdapter\Dtos\OnOfficeApiCredentials; use Innobrain\OnOfficeAdapter\Dtos\OnOfficeRequest; use Innobrain\OnOfficeAdapter\Enums\OnOfficeAction; use Innobrain\OnOfficeAdapter\Enums\OnOfficeError; @@ -24,20 +25,41 @@ class OnOfficeService use OnOfficeDefaultFieldConst; use OnOfficeParameterConst; - public function __construct() {} + public function __construct( + private ?OnOfficeApiCredentials $credentials = null + ) {} + + public function setCredentials(?OnOfficeApiCredentials $credentials): static + { + $this->credentials = $credentials; + + return $this; + } public function getToken(): string { + if ($this->credentials instanceof OnOfficeApiCredentials) { + return $this->credentials->token; + } + return Config::get('onoffice.token', '') ?? ''; } public function getSecret(): string { + if ($this->credentials instanceof OnOfficeApiCredentials) { + return $this->credentials->secret; + } + return Config::get('onoffice.secret', '') ?? ''; } public function getApiClaim(): string { + if ($this->credentials instanceof OnOfficeApiCredentials) { + return $this->credentials->apiClaim; + } + return Config::get('onoffice.api_claim', '') ?? ''; } diff --git a/tests/Repositories/BaseRepositoryTest.php b/tests/Repositories/BaseRepositoryTest.php index d2be56e..e01c186 100644 --- a/tests/Repositories/BaseRepositoryTest.php +++ b/tests/Repositories/BaseRepositoryTest.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Http; use Innobrain\OnOfficeAdapter\Dtos\OnOfficeRequest; use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponse; @@ -12,6 +13,7 @@ use Innobrain\OnOfficeAdapter\Exceptions\OnOfficeException; use Innobrain\OnOfficeAdapter\Facades\Testing\RecordFactories\BaseFactory; use Innobrain\OnOfficeAdapter\Repositories\BaseRepository; +use Innobrain\OnOfficeAdapter\Services\OnOfficeService; use Symfony\Component\VarDumper\VarDumper; describe('stray requests', function () { @@ -563,3 +565,63 @@ VarDumper::setHandler(null); }); }); + +describe('custom credentials', function () { + it('can set custom credentials', function () { + $builder = new BaseRepository; + + $builder = $builder + ->query() + ->withCredentials('token', 'secret', 'claim'); + + $m = new ReflectionMethod($builder, 'getOnOfficeService'); + $m->setAccessible(true); + /* @var OnOfficeService $onOfficeService */ + $onOfficeService = $m->invoke($builder); + + expect($onOfficeService->getToken())->toBe('token') + ->and($onOfficeService->getSecret())->toBe('secret') + ->and($onOfficeService->getApiClaim())->toBe('claim'); + }); + + it('can set custom credentials twice', function () { + $builder = new BaseRepository; + $builder + ->query() + ->withCredentials('token', 'secret', 'claim'); + + $builder = new BaseRepository; + $builder = $builder + ->query() + ->withCredentials('token2', 'secret2', 'claim2'); + + $m = new ReflectionMethod($builder, 'getOnOfficeService'); + $m->setAccessible(true); + /* @var OnOfficeService $onOfficeService */ + $onOfficeService = $m->invoke($builder); + + expect($onOfficeService->getToken())->toBe('token2') + ->and($onOfficeService->getSecret())->toBe('secret2') + ->and($onOfficeService->getApiClaim())->toBe('claim2'); + }); + + it('will reset to default', function () { + $builder = new BaseRepository; + $builder + ->query() + ->withCredentials('token', 'secret', 'claim'); + + $builder = new BaseRepository; + $builder = $builder + ->query(); + + $m = new ReflectionMethod($builder, 'getOnOfficeService'); + $m->setAccessible(true); + + /* @var OnOfficeService $onOfficeService */ + $onOfficeService = $m->invoke($builder); + expect($onOfficeService->getToken())->toBe(Config::get('onoffice.token')) + ->and($onOfficeService->getSecret())->toBe(Config::get('onoffice.secret')) + ->and($onOfficeService->getApiClaim())->toBe(''); + }); +});