Skip to content

Commit

Permalink
Merge pull request #23 from innobraingmbh/feat/with-credentials
Browse files Browse the repository at this point in the history
feat: enable withCredentials call on Builder so config is only needed…
  • Loading branch information
kauffinger authored Nov 6, 2024
2 parents 0076d50 + f7fd895 commit 69bd638
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/Dtos/OnOfficeApiCredentials.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Innobrain\OnOfficeAdapter\Dtos;

readonly class OnOfficeApiCredentials
{
public function __construct(
public string $token,
public string $secret,
public string $apiClaim = ''
) {}
}
16 changes: 14 additions & 2 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeApiCredentials;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeRequest;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponse;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponsePage;
Expand Down Expand Up @@ -103,11 +104,22 @@ class Builder implements BuilderInterface
*/
protected array $beforeSendingCallbacks = [];

public function __construct() {}
public function __construct(
protected ?OnOfficeApiCredentials $credentials = null
) {}

public function withCredentials(string $token, string $secret, string $apiClaim = ''): static
{
$this->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
Expand Down
24 changes: 23 additions & 1 deletion src/Services/OnOfficeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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', '') ?? '';
}

Expand Down
62 changes: 62 additions & 0 deletions tests/Repositories/BaseRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 () {
Expand Down Expand Up @@ -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('');
});
});

0 comments on commit 69bd638

Please sign in to comment.