Skip to content

Commit a9bfc43

Browse files
committed
move api_type in extractor/loader config, choose the right builder in extractor/loader builders, edit service to set all these new apiType variables
1 parent 642db8a commit a9bfc43

File tree

9 files changed

+106
-38
lines changed

9 files changed

+106
-38
lines changed

src/Builder/Client.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Kiboko\Plugin\Sylius\Builder;
66

7+
use Diglin\Sylius\ApiClient\SyliusAdminClientBuilder;
8+
use Diglin\Sylius\ApiClient\SyliusShopClientBuilder;
79
use Kiboko\Plugin\Sylius\MissingAuthenticationMethodException;
810
use PhpParser\Builder;
911
use PhpParser\Node;
@@ -18,8 +20,13 @@ final class Client implements Builder
1820
private ?Node\Expr $httpRequestFactory = null;
1921
private ?Node\Expr $httpStreamFactory = null;
2022
private ?Node\Expr $fileSystem = null;
23+
private string $apiType = '';
2124

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)
25+
public const API_ADMIN_KEY = 'admin';
26+
public const API_SHOP_KEY = 'shop';
27+
public const API_LEGACY_KEY = 'legacy';
28+
29+
public function __construct(private readonly Node\Expr $baseUrl, private readonly Node\Expr $clientId, private readonly Node\Expr $secret)
2330
{
2431
}
2532

@@ -31,6 +38,13 @@ public function withToken(Node\Expr $token, Node\Expr $refreshToken): self
3138
return $this;
3239
}
3340

41+
public function withApiType(string $apiType): self
42+
{
43+
$this->apiType = $apiType;
44+
45+
return $this;
46+
}
47+
3448
public function withPassword(Node\Expr $username, Node\Expr $password): self
3549
{
3650
$this->username = $username;
@@ -121,10 +135,9 @@ public function getNode(): Node\Expr\MethodCall
121135
private function getClientBuilderNode(): Node\Expr\MethodCall
122136
{
123137
$className = match ($this->apiType) {
124-
'admin' => \Diglin\Sylius\ApiClient\SyliusAdminClientBuilder::class,
125-
'store' => \Diglin\Sylius\ApiClient\SyliusShopClientBuilder::class,
126-
'legacy' => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder',
127-
default => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder',
138+
self::API_ADMIN_KEY => SyliusAdminClientBuilder::class,
139+
self::API_SHOP_KEY => SyliusShopClientBuilder::class,
140+
self::API_LEGACY_KEY => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder'
128141
};
129142

130143
return new Node\Expr\MethodCall(

src/Builder/Extractor.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ final class Extractor implements StepBuilderInterface
1212
{
1313
private ?Node\Expr $logger = null;
1414
private ?Node\Expr $client = null;
15+
private string $apiType;
1516

1617
public function __construct(private readonly Builder $capacity)
1718
{
@@ -24,6 +25,13 @@ public function withClient(Node\Expr $client): self
2425
return $this;
2526
}
2627

28+
public function withApiType(string $apiType): self
29+
{
30+
$this->apiType = $apiType;
31+
32+
return $this;
33+
}
34+
2735
public function withLogger(Node\Expr $logger): self
2836
{
2937
$this->logger = $logger;
@@ -55,18 +63,7 @@ class: new Node\Stmt\Class_(
5563
name: new Node\Identifier(name: '__construct'),
5664
subNodes: [
5765
'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC,
58-
'params' => [
59-
new Node\Param(
60-
var: new Node\Expr\Variable('client'),
61-
type: new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusLegacyClientInterface::class),
62-
flags: Node\Stmt\Class_::MODIFIER_PUBLIC,
63-
),
64-
new Node\Param(
65-
var: new Node\Expr\Variable('logger'),
66-
type: new Node\Name\FullyQualified(name: \Psr\Log\LoggerInterface::class),
67-
flags: Node\Stmt\Class_::MODIFIER_PUBLIC,
68-
),
69-
],
66+
'params' => $this->getParamsNode(),
7067
],
7168
),
7269
new Node\Stmt\ClassMethod(
@@ -133,4 +130,26 @@ class: new Node\Stmt\Class_(
133130
],
134131
);
135132
}
133+
134+
public function getParamsNode(): array
135+
{
136+
137+
$className = match ($this->apiType) {
138+
Client::API_ADMIN_KEY => \Diglin\Sylius\ApiClient\SyliusAdminClientInterface::class,
139+
Client::API_LEGACY_KEY => \Diglin\Sylius\ApiClient\SyliusLegacyClientInterface::class,
140+
Client::API_SHOP_KEY => \Diglin\Sylius\ApiClient\SyliusShopClientInterface::class,
141+
};
142+
return [
143+
new Node\Param(
144+
var: new Node\Expr\Variable('client'),
145+
type: new Node\Name\FullyQualified(name: $className),
146+
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
147+
),
148+
new Node\Param(
149+
var: new Node\Expr\Variable('logger'),
150+
type: new Node\Name\FullyQualified(name: \Psr\Log\LoggerInterface::class),
151+
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
152+
),
153+
];
154+
}
136155
}

src/Builder/Loader.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ final class Loader implements StepBuilderInterface
1212
{
1313
private ?Node\Expr $logger = null;
1414
private ?Node\Expr $client = null;
15+
private string $apiType;
1516

1617
public function __construct(private readonly Builder $capacity)
1718
{
@@ -24,6 +25,13 @@ public function withClient(Node\Expr $client): self
2425
return $this;
2526
}
2627

28+
public function withApiType(string $apiType): self
29+
{
30+
$this->apiType = $apiType;
31+
32+
return $this;
33+
}
34+
2735
public function withLogger(Node\Expr $logger): self
2836
{
2937
$this->logger = $logger;
@@ -55,18 +63,7 @@ class: new Node\Stmt\Class_(
5563
name: new Node\Identifier(name: '__construct'),
5664
subNodes: [
5765
'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC,
58-
'params' => [
59-
new Node\Param(
60-
var: new Node\Expr\Variable('client'),
61-
type: new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusLegacyClientInterface::class),
62-
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
63-
),
64-
new Node\Param(
65-
var: new Node\Expr\Variable('logger'),
66-
type: new Node\Name\FullyQualified(name: \Psr\Log\LoggerInterface::class),
67-
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
68-
),
69-
],
66+
'params' => $this->getParamsNode(),
7067
],
7168
),
7269
new Node\Stmt\ClassMethod(
@@ -139,4 +136,26 @@ class: new Node\Stmt\Class_(
139136
],
140137
);
141138
}
139+
140+
public function getParamsNode(): array
141+
{
142+
143+
$className = match ($this->apiType) {
144+
Client::API_ADMIN_KEY => \Diglin\Sylius\ApiClient\SyliusAdminClientInterface::class,
145+
Client::API_LEGACY_KEY => \Diglin\Sylius\ApiClient\SyliusLegacyClientInterface::class,
146+
Client::API_SHOP_KEY => \Diglin\Sylius\ApiClient\SyliusShopClientInterface::class,
147+
};
148+
return [
149+
new Node\Param(
150+
var: new Node\Expr\Variable('client'),
151+
type: new Node\Name\FullyQualified(name: $className),
152+
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
153+
),
154+
new Node\Param(
155+
var: new Node\Expr\Variable('logger'),
156+
type: new Node\Name\FullyQualified(name: \Psr\Log\LoggerInterface::class),
157+
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
158+
),
159+
];
160+
}
142161
}

src/Configuration.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Kiboko\Contract\Configurator\PluginConfigurationInterface;
88
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9+
use function Kiboko\Component\SatelliteToolbox\Configuration\asExpression;
10+
use function Kiboko\Component\SatelliteToolbox\Configuration\isExpression;
911

1012
final class Configuration implements PluginConfigurationInterface
1113
{
@@ -27,6 +29,14 @@ public function getConfigTreeBuilder(): TreeBuilder
2729
->arrayNode('expression_language')
2830
->scalarPrototype()->end()
2931
->end()
32+
->scalarNode('api_type')
33+
->isRequired()
34+
->cannotBeEmpty()
35+
->validate()
36+
->ifTrue(isExpression())
37+
->then(asExpression())
38+
->end()
39+
->end()
3040
->append(node: $extractor->getConfigTreeBuilder()->getRootNode())
3141
->append(node: $loader->getConfigTreeBuilder()->getRootNode())
3242
->append(node: $client->getConfigTreeBuilder()->getRootNode())

src/Configuration/Client.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@ 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()
7870
->scalarNode('api_url')
7971
->isRequired()
8072
->cannotBeEmpty()

src/Configuration/Extractor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui
165165
})
166166
->end()
167167
->children()
168+
->scalarNode('api_type')
169+
->isRequired()
170+
->cannotBeEmpty()
171+
->end()
168172
->scalarNode('type')
169173
->isRequired()
170174
->validate()

src/Configuration/Loader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui
118118
})
119119
->end()
120120
->children()
121+
->scalarNode('api_type')
122+
->isRequired()
123+
->cannotBeEmpty()
124+
->end()
121125
->scalarNode('type')
122126
->isRequired()
123127
->validate()

src/Factory/Client.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ 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']),
7877
);
7978

8079
if (isset($config['context'])) {
@@ -92,6 +91,10 @@ public function compile(array $config): Repository\Client
9291
}
9392
}
9493

94+
if (isset($config['api_type'])) {
95+
$clientBuilder->withApiType($config['api_type']);
96+
}
97+
9598
if (isset($config['password'])) {
9699
$clientBuilder->withPassword(
97100
compileValueWhenExpression($this->interpreter, $config['username']),

src/Service.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep
8989
$extractor = $extractorFactory->compile($config['extractor']);
9090
$extractorBuilder = $extractor->getBuilder();
9191

92+
$clientFactory->withApiType($config['extractor']['api_type']);
9293
$client = $clientFactory->compile($config['client']);
9394

9495
$extractorBuilder->withClient($client->getBuilder()->getNode());
96+
$extractorBuilder->withApiType($config['extractor']['api_type']);
9597

9698
$extractor->merge($client);
9799

@@ -103,9 +105,11 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep
103105
$loader = $loaderFactory->compile($config['loader']);
104106
$loaderBuilder = $loader->getBuilder();
105107

108+
$clientFactory->withApiType($config['loader']['api_type']);
106109
$client = $clientFactory->compile($config['client']);
107110

108111
$loaderBuilder->withClient($client->getBuilder()->getNode());
112+
$loaderBuilder->withApiType($config['loader']['api_type']);
109113

110114
$loader->merge($client);
111115

0 commit comments

Comments
 (0)