Skip to content

Commit

Permalink
move api_type in extractor/loader config, choose the right builder in…
Browse files Browse the repository at this point in the history
… extractor/loader builders, edit service to set all these new apiType variables
  • Loading branch information
JoMessina committed Oct 25, 2023
1 parent 642db8a commit a9bfc43
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 38 deletions.
23 changes: 18 additions & 5 deletions src/Builder/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Kiboko\Plugin\Sylius\Builder;

use Diglin\Sylius\ApiClient\SyliusAdminClientBuilder;
use Diglin\Sylius\ApiClient\SyliusShopClientBuilder;
use Kiboko\Plugin\Sylius\MissingAuthenticationMethodException;
use PhpParser\Builder;
use PhpParser\Node;
Expand All @@ -18,8 +20,13 @@ final class Client implements Builder
private ?Node\Expr $httpRequestFactory = null;
private ?Node\Expr $httpStreamFactory = null;
private ?Node\Expr $fileSystem = null;
private string $apiType = '';

public function __construct(private readonly Node\Expr $baseUrl, private readonly Node\Expr $clientId, private readonly Node\Expr $secret, private readonly null|Node\Expr $apiType)
public const API_ADMIN_KEY = 'admin';
public const API_SHOP_KEY = 'shop';
public const API_LEGACY_KEY = 'legacy';

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

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

public function withApiType(string $apiType): self
{
$this->apiType = $apiType;

return $this;
}

public function withPassword(Node\Expr $username, Node\Expr $password): self
{
$this->username = $username;
Expand Down Expand Up @@ -121,10 +135,9 @@ public function getNode(): Node\Expr\MethodCall
private function getClientBuilderNode(): Node\Expr\MethodCall
{
$className = match ($this->apiType) {

Check failure on line 137 in src/Builder/Client.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Match expression does not handle remaining value: string

Check failure on line 137 in src/Builder/Client.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Match expression does not handle remaining value: string

Check failure on line 137 in src/Builder/Client.php

View workflow job for this annotation

GitHub Actions / phpstan

Match expression does not handle remaining value: string
'admin' => \Diglin\Sylius\ApiClient\SyliusAdminClientBuilder::class,
'store' => \Diglin\Sylius\ApiClient\SyliusShopClientBuilder::class,
'legacy' => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder',
default => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder',
self::API_ADMIN_KEY => SyliusAdminClientBuilder::class,
self::API_SHOP_KEY => SyliusShopClientBuilder::class,
self::API_LEGACY_KEY => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder'
};

return new Node\Expr\MethodCall(
Expand Down
43 changes: 31 additions & 12 deletions src/Builder/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class Extractor implements StepBuilderInterface
{
private ?Node\Expr $logger = null;
private ?Node\Expr $client = null;
private string $apiType;

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

public function withApiType(string $apiType): self
{
$this->apiType = $apiType;

return $this;
}

public function withLogger(Node\Expr $logger): self
{
$this->logger = $logger;
Expand Down Expand Up @@ -55,18 +63,7 @@ class: new Node\Stmt\Class_(
name: new Node\Identifier(name: '__construct'),
subNodes: [
'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC,
'params' => [
new Node\Param(
var: new Node\Expr\Variable('client'),
type: new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusLegacyClientInterface::class),
flags: Node\Stmt\Class_::MODIFIER_PUBLIC,
),
new Node\Param(
var: new Node\Expr\Variable('logger'),
type: new Node\Name\FullyQualified(name: \Psr\Log\LoggerInterface::class),
flags: Node\Stmt\Class_::MODIFIER_PUBLIC,
),
],
'params' => $this->getParamsNode(),
],
),
new Node\Stmt\ClassMethod(
Expand Down Expand Up @@ -133,4 +130,26 @@ class: new Node\Stmt\Class_(
],
);
}

public function getParamsNode(): array

Check failure on line 134 in src/Builder/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Method Kiboko\Plugin\Sylius\Builder\Extractor::getParamsNode() return type has no value type specified in iterable type array.

Check failure on line 134 in src/Builder/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Method Kiboko\Plugin\Sylius\Builder\Extractor::getParamsNode() return type has no value type specified in iterable type array.
{

$className = match ($this->apiType) {

Check failure on line 137 in src/Builder/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Match expression does not handle remaining value: string

Check failure on line 137 in src/Builder/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Match expression does not handle remaining value: string

Check failure on line 137 in src/Builder/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan

Match expression does not handle remaining value: string
Client::API_ADMIN_KEY => \Diglin\Sylius\ApiClient\SyliusAdminClientInterface::class,
Client::API_LEGACY_KEY => \Diglin\Sylius\ApiClient\SyliusLegacyClientInterface::class,
Client::API_SHOP_KEY => \Diglin\Sylius\ApiClient\SyliusShopClientInterface::class,
};
return [
new Node\Param(
var: new Node\Expr\Variable('client'),
type: new Node\Name\FullyQualified(name: $className),
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
),
new Node\Param(
var: new Node\Expr\Variable('logger'),
type: new Node\Name\FullyQualified(name: \Psr\Log\LoggerInterface::class),
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
),
];
}
}
43 changes: 31 additions & 12 deletions src/Builder/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class Loader implements StepBuilderInterface
{
private ?Node\Expr $logger = null;
private ?Node\Expr $client = null;
private string $apiType;

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

public function withApiType(string $apiType): self
{
$this->apiType = $apiType;

return $this;
}

public function withLogger(Node\Expr $logger): self
{
$this->logger = $logger;
Expand Down Expand Up @@ -55,18 +63,7 @@ class: new Node\Stmt\Class_(
name: new Node\Identifier(name: '__construct'),
subNodes: [
'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC,
'params' => [
new Node\Param(
var: new Node\Expr\Variable('client'),
type: new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusLegacyClientInterface::class),
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
),
new Node\Param(
var: new Node\Expr\Variable('logger'),
type: new Node\Name\FullyQualified(name: \Psr\Log\LoggerInterface::class),
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
),
],
'params' => $this->getParamsNode(),
],
),
new Node\Stmt\ClassMethod(
Expand Down Expand Up @@ -139,4 +136,26 @@ class: new Node\Stmt\Class_(
],
);
}

public function getParamsNode(): array

Check failure on line 140 in src/Builder/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Method Kiboko\Plugin\Sylius\Builder\Loader::getParamsNode() return type has no value type specified in iterable type array.
{

$className = match ($this->apiType) {

Check failure on line 143 in src/Builder/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Match expression does not handle remaining value: string

Check failure on line 143 in src/Builder/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

Match expression does not handle remaining value: string
Client::API_ADMIN_KEY => \Diglin\Sylius\ApiClient\SyliusAdminClientInterface::class,
Client::API_LEGACY_KEY => \Diglin\Sylius\ApiClient\SyliusLegacyClientInterface::class,
Client::API_SHOP_KEY => \Diglin\Sylius\ApiClient\SyliusShopClientInterface::class,
};
return [
new Node\Param(
var: new Node\Expr\Variable('client'),
type: new Node\Name\FullyQualified(name: $className),
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
),
new Node\Param(
var: new Node\Expr\Variable('logger'),
type: new Node\Name\FullyQualified(name: \Psr\Log\LoggerInterface::class),
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
),
];
}
}
10 changes: 10 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Kiboko\Contract\Configurator\PluginConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use function Kiboko\Component\SatelliteToolbox\Configuration\asExpression;
use function Kiboko\Component\SatelliteToolbox\Configuration\isExpression;

final class Configuration implements PluginConfigurationInterface
{
Expand All @@ -27,6 +29,14 @@ public function getConfigTreeBuilder(): TreeBuilder
->arrayNode('expression_language')
->scalarPrototype()->end()
->end()
->scalarNode('api_type')
->isRequired()
->cannotBeEmpty()
->validate()
->ifTrue(isExpression())
->then(asExpression())
->end()
->end()
->append(node: $extractor->getConfigTreeBuilder()->getRootNode())
->append(node: $loader->getConfigTreeBuilder()->getRootNode())
->append(node: $client->getConfigTreeBuilder()->getRootNode())
Expand Down
8 changes: 0 additions & 8 deletions src/Configuration/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,6 @@ 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
4 changes: 4 additions & 0 deletions src/Configuration/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui
})
->end()
->children()
->scalarNode('api_type')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('type')
->isRequired()
->validate()
Expand Down
4 changes: 4 additions & 0 deletions src/Configuration/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui
})
->end()
->children()
->scalarNode('api_type')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('type')
->isRequired()
->validate()
Expand Down
5 changes: 4 additions & 1 deletion src/Factory/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ 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 All @@ -92,6 +91,10 @@ public function compile(array $config): Repository\Client
}
}

if (isset($config['api_type'])) {
$clientBuilder->withApiType($config['api_type']);
}

if (isset($config['password'])) {
$clientBuilder->withPassword(
compileValueWhenExpression($this->interpreter, $config['username']),
Expand Down
4 changes: 4 additions & 0 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep
$extractor = $extractorFactory->compile($config['extractor']);
$extractorBuilder = $extractor->getBuilder();

$clientFactory->withApiType($config['extractor']['api_type']);

Check failure on line 92 in src/Service.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Kiboko\Plugin\Sylius\Factory\Client::withApiType().
$client = $clientFactory->compile($config['client']);

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

$extractor->merge($client);

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

$clientFactory->withApiType($config['loader']['api_type']);

Check failure on line 108 in src/Service.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Kiboko\Plugin\Sylius\Factory\Client::withApiType().
$client = $clientFactory->compile($config['client']);

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

$loader->merge($client);

Expand Down

0 comments on commit a9bfc43

Please sign in to comment.