Skip to content

Commit 6b0e402

Browse files
committed
add api_type flag to build the right api client version
1 parent 6e5410e commit 6b0e402

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

src/Builder/Client.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class Client implements Builder
1919
private ?Node\Expr $httpStreamFactory = null;
2020
private ?Node\Expr $fileSystem = null;
2121

22-
public function __construct(private readonly Node\Expr $baseUrl, private readonly Node\Expr $clientId, private readonly Node\Expr $secret)
22+
public function __construct(private readonly Node\Expr $baseUrl, private readonly Node\Expr $clientId, private readonly Node\Expr $secret, private readonly null|Node\Expr $apiType)
2323
{
2424
}
2525

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

7070
public function getNode(): Node\Expr\MethodCall
7171
{
72-
$instance = new Node\Expr\MethodCall(
73-
var: new Node\Expr\New_(
74-
new Node\Name\FullyQualified('Diglin\\Sylius\\ApiClient\\SyliusClientBuilder'),
75-
),
76-
name: new Node\Identifier('setBaseUri'),
77-
args: [
78-
new Node\Arg($this->baseUrl),
79-
],
80-
);
72+
$instance = $this->getClientBuilderNode();
8173

8274
if (null !== $this->httpClient) {
8375
$instance = new Node\Expr\MethodCall(
@@ -126,6 +118,26 @@ public function getNode(): Node\Expr\MethodCall
126118
);
127119
}
128120

121+
private function getClientBuilderNode(): Node\Expr\MethodCall
122+
{
123+
$className = match ($this->apiType) {
124+
'admin' => 'Diglin\\Sylius\\ApiClient\\SyliusAdminClientBuilder',
125+
'store' => 'Diglin\\Sylius\\ApiClient\\SyliusShopClientBuilder',
126+
'legacy' => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder',
127+
default => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder',
128+
};
129+
130+
return new Node\Expr\MethodCall(
131+
var: new Node\Expr\New_(
132+
new Node\Name\FullyQualified($className),
133+
),
134+
name: new Node\Identifier('setBaseUri'),
135+
args: [
136+
new Node\Arg($this->baseUrl),
137+
],
138+
);
139+
}
140+
129141
private function getFactoryMethod(): string
130142
{
131143
if (null !== $this->password) {

src/Configuration/Client.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui
6767
->end()
6868
->end()
6969
->end()
70+
->scalarNode('api_type')
71+
->isRequired()
72+
->cannotBeEmpty()
73+
->validate()
74+
->ifTrue(isExpression())
75+
->then(asExpression())
76+
->end()
77+
->end()
7078
->scalarNode('api_url')
7179
->isRequired()
7280
->cannotBeEmpty()

src/Factory/Client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function compile(array $config): Repository\Client
7474
compileValueWhenExpression($this->interpreter, $config['api_url']),
7575
compileValueWhenExpression($this->interpreter, $config['client_id']),
7676
compileValueWhenExpression($this->interpreter, $config['secret']),
77+
compileValueWhenExpression($this->interpreter, $config['api_type']),
7778
);
7879

7980
if (isset($config['context'])) {

0 commit comments

Comments
 (0)