Skip to content

Commit

Permalink
Simplify connector
Browse files Browse the repository at this point in the history
  • Loading branch information
mdpoulter committed Nov 2, 2023
1 parent ce6e8b2 commit 0dfac46
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/VendConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace SimpleSquid\Vend;

use Composer\InstalledVersions;
use Saloon\Contracts\Authenticator;
use Saloon\Helpers\OAuth2\OAuthConfig;
use Saloon\Http\Connector;
use Saloon\Traits\OAuth2\AuthorizationCodeGrant;
use Saloon\Traits\Plugins\AcceptsJson;
use SimpleSquid\Vend\Common\Exceptions\AuthenticationDetailsMissingException;
use SimpleSquid\Vend\Common\Exceptions\DomainPrefixMissingException;
use SimpleSquid\Vend\Common\Responses\VendResponse;

Expand All @@ -18,29 +18,26 @@ abstract class VendConnector extends Connector

protected ?string $response = VendResponse::class;

protected ?string $domainPrefix;

/**
* @var callable(): ?\Saloon\Contracts\Authenticator
*/
protected $defaultAuth;

public function __construct(
?string $clientId = null,
?string $clientSecret = null,
?string $redirectUri = null,
?string $personalToken = null,
protected ?string $domainPrefix = null,
) {
if (! is_null($personalToken)) {
$this->withTokenAuth($personalToken);

return;
}

if (! is_null($clientId) && ! is_null($clientSecret) && ! is_null($redirectUri)) {
$this->oauthConfig()
->setClientId($clientId)
->setClientSecret($clientSecret)
->setRedirectUri($redirectUri);

if (is_null($clientId) || is_null($clientSecret) || is_null($redirectUri)) {
return;
}

throw new AuthenticationDetailsMissingException();
$this->oauthConfig()
->setClientId($clientId)
->setClientSecret($clientSecret)
->setRedirectUri($redirectUri);
}

public function resolveBaseUrl(): string
Expand All @@ -59,6 +56,14 @@ public function withDomainPrefix(string $domainPrefix): static
return $this;
}

/**
* @param callable(): ?\Saloon\Contracts\Authenticator $callable
*/
public function authenticateWith(callable $callable): static
{
$this->defaultAuth = $callable;

Check failure on line 64 in src/VendConnector.php

View workflow job for this annotation

GitHub Actions / Run code formatters and analysis

Method SimpleSquid\Vend\VendConnector::authenticateWith() should return static(SimpleSquid\Vend\VendConnector) but return statement is missing.
}

protected function defaultHeaders(): array
{
return [
Expand All @@ -73,4 +78,9 @@ protected function defaultOauthConfig(): OAuthConfig
->setTokenEndpoint('/1.0/token')
->setUserEndpoint('/2.0/retailer');
}

protected function defaultAuth(): ?Authenticator
{
return isset($this->defaultAuth) ? ($this->defaultAuth)() : null;

Check failure on line 84 in src/VendConnector.php

View workflow job for this annotation

GitHub Actions / Run code formatters and analysis

Property SimpleSquid\Vend\VendConnector::$defaultAuth (callable) in isset() is not nullable.
}
}

0 comments on commit 0dfac46

Please sign in to comment.