Skip to content

Commit

Permalink
add api_type flag to build the right api client version
Browse files Browse the repository at this point in the history
  • Loading branch information
JoMessina committed Oct 20, 2023
1 parent 6e5410e commit 6b0e402
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/Builder/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class Client implements Builder
private ?Node\Expr $httpStreamFactory = null;
private ?Node\Expr $fileSystem = null;

public function __construct(private readonly Node\Expr $baseUrl, private readonly Node\Expr $clientId, private readonly Node\Expr $secret)
public function __construct(private readonly Node\Expr $baseUrl, private readonly Node\Expr $clientId, private readonly Node\Expr $secret, private readonly null|Node\Expr $apiType)
{
}

Expand Down Expand Up @@ -69,15 +69,7 @@ public function withFileSystem(Node\Expr $fileSystem): self

public function getNode(): Node\Expr\MethodCall
{
$instance = new Node\Expr\MethodCall(
var: new Node\Expr\New_(
new Node\Name\FullyQualified('Diglin\\Sylius\\ApiClient\\SyliusClientBuilder'),
),
name: new Node\Identifier('setBaseUri'),
args: [
new Node\Arg($this->baseUrl),
],
);
$instance = $this->getClientBuilderNode();

if (null !== $this->httpClient) {
$instance = new Node\Expr\MethodCall(
Expand Down Expand Up @@ -126,6 +118,26 @@ public function getNode(): Node\Expr\MethodCall
);
}

private function getClientBuilderNode(): Node\Expr\MethodCall
{
$className = match ($this->apiType) {
'admin' => 'Diglin\\Sylius\\ApiClient\\SyliusAdminClientBuilder',
'store' => 'Diglin\\Sylius\\ApiClient\\SyliusShopClientBuilder',
'legacy' => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder',
default => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder',
};

return new Node\Expr\MethodCall(
var: new Node\Expr\New_(
new Node\Name\FullyQualified($className),
),
name: new Node\Identifier('setBaseUri'),
args: [
new Node\Arg($this->baseUrl),
],
);
}

private function getFactoryMethod(): string
{
if (null !== $this->password) {
Expand Down
8 changes: 8 additions & 0 deletions src/Configuration/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui
->end()
->end()
->end()
->scalarNode('api_type')
->isRequired()
->cannotBeEmpty()
->validate()
->ifTrue(isExpression())
->then(asExpression())
->end()
->end()
->scalarNode('api_url')
->isRequired()
->cannotBeEmpty()
Expand Down
1 change: 1 addition & 0 deletions src/Factory/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function compile(array $config): Repository\Client
compileValueWhenExpression($this->interpreter, $config['api_url']),
compileValueWhenExpression($this->interpreter, $config['client_id']),
compileValueWhenExpression($this->interpreter, $config['secret']),
compileValueWhenExpression($this->interpreter, $config['api_type']),
);

if (isset($config['context'])) {
Expand Down

0 comments on commit 6b0e402

Please sign in to comment.