From 6b0e402fd24e64752fbf67d4131076db98c7e3ec Mon Sep 17 00:00:00 2001 From: Jonathan Date: Fri, 20 Oct 2023 16:03:25 +0200 Subject: [PATCH 01/27] add api_type flag to build the right api client version --- src/Builder/Client.php | 32 ++++++++++++++++++++++---------- src/Configuration/Client.php | 8 ++++++++ src/Factory/Client.php | 1 + 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/Builder/Client.php b/src/Builder/Client.php index 3af1458..3b69d2c 100644 --- a/src/Builder/Client.php +++ b/src/Builder/Client.php @@ -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) { } @@ -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( @@ -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) { diff --git a/src/Configuration/Client.php b/src/Configuration/Client.php index bd4840f..9182b63 100644 --- a/src/Configuration/Client.php +++ b/src/Configuration/Client.php @@ -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() diff --git a/src/Factory/Client.php b/src/Factory/Client.php index 5c61386..d5dfd5c 100644 --- a/src/Factory/Client.php +++ b/src/Factory/Client.php @@ -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'])) { From 642db8a60629e2ff77b921381b082762eb728271 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 20 Oct 2023 14:04:42 +0000 Subject: [PATCH 02/27] [rector] Rector fixes --- src/Builder/Client.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Builder/Client.php b/src/Builder/Client.php index 3b69d2c..2aad1a1 100644 --- a/src/Builder/Client.php +++ b/src/Builder/Client.php @@ -121,8 +121,8 @@ 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', + 'admin' => \Diglin\Sylius\ApiClient\SyliusAdminClientBuilder::class, + 'store' => \Diglin\Sylius\ApiClient\SyliusShopClientBuilder::class, 'legacy' => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder', default => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder', }; From a9bfc437bd32118d0cd3379ef6e53c0d02d70c8c Mon Sep 17 00:00:00 2001 From: Jonathan Date: Wed, 25 Oct 2023 10:35:38 +0200 Subject: [PATCH 03/27] move api_type in extractor/loader config, choose the right builder in extractor/loader builders, edit service to set all these new apiType variables --- src/Builder/Client.php | 23 ++++++++++++++---- src/Builder/Extractor.php | 43 ++++++++++++++++++++++++--------- src/Builder/Loader.php | 43 ++++++++++++++++++++++++--------- src/Configuration.php | 10 ++++++++ src/Configuration/Client.php | 8 ------ src/Configuration/Extractor.php | 4 +++ src/Configuration/Loader.php | 4 +++ src/Factory/Client.php | 5 +++- src/Service.php | 4 +++ 9 files changed, 106 insertions(+), 38 deletions(-) diff --git a/src/Builder/Client.php b/src/Builder/Client.php index 2aad1a1..f49566d 100644 --- a/src/Builder/Client.php +++ b/src/Builder/Client.php @@ -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; @@ -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) { } @@ -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; @@ -121,10 +135,9 @@ public function getNode(): Node\Expr\MethodCall private function getClientBuilderNode(): Node\Expr\MethodCall { $className = match ($this->apiType) { - '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( diff --git a/src/Builder/Extractor.php b/src/Builder/Extractor.php index cfe674f..d82fa0a 100644 --- a/src/Builder/Extractor.php +++ b/src/Builder/Extractor.php @@ -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) { @@ -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; @@ -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( @@ -133,4 +130,26 @@ class: new Node\Stmt\Class_( ], ); } + + public function getParamsNode(): array + { + + $className = match ($this->apiType) { + 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, + ), + ]; + } } diff --git a/src/Builder/Loader.php b/src/Builder/Loader.php index d5c4f57..e8127e5 100644 --- a/src/Builder/Loader.php +++ b/src/Builder/Loader.php @@ -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) { @@ -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; @@ -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( @@ -139,4 +136,26 @@ class: new Node\Stmt\Class_( ], ); } + + public function getParamsNode(): array + { + + $className = match ($this->apiType) { + 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, + ), + ]; + } } diff --git a/src/Configuration.php b/src/Configuration.php index a0b0315..1fff9bb 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -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 { @@ -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()) diff --git a/src/Configuration/Client.php b/src/Configuration/Client.php index 9182b63..bd4840f 100644 --- a/src/Configuration/Client.php +++ b/src/Configuration/Client.php @@ -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() diff --git a/src/Configuration/Extractor.php b/src/Configuration/Extractor.php index ccf5a4e..dce1044 100644 --- a/src/Configuration/Extractor.php +++ b/src/Configuration/Extractor.php @@ -165,6 +165,10 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui }) ->end() ->children() + ->scalarNode('api_type') + ->isRequired() + ->cannotBeEmpty() + ->end() ->scalarNode('type') ->isRequired() ->validate() diff --git a/src/Configuration/Loader.php b/src/Configuration/Loader.php index a62d237..6f1b7dc 100644 --- a/src/Configuration/Loader.php +++ b/src/Configuration/Loader.php @@ -118,6 +118,10 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui }) ->end() ->children() + ->scalarNode('api_type') + ->isRequired() + ->cannotBeEmpty() + ->end() ->scalarNode('type') ->isRequired() ->validate() diff --git a/src/Factory/Client.php b/src/Factory/Client.php index d5dfd5c..a258147 100644 --- a/src/Factory/Client.php +++ b/src/Factory/Client.php @@ -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'])) { @@ -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']), diff --git a/src/Service.php b/src/Service.php index 8827b51..22fd665 100644 --- a/src/Service.php +++ b/src/Service.php @@ -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']); $client = $clientFactory->compile($config['client']); $extractorBuilder->withClient($client->getBuilder()->getNode()); + $extractorBuilder->withApiType($config['extractor']['api_type']); $extractor->merge($client); @@ -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']); $client = $clientFactory->compile($config['client']); $loaderBuilder->withClient($client->getBuilder()->getNode()); + $loaderBuilder->withApiType($config['loader']['api_type']); $loader->merge($client); From 8e8ac77f22ee6aa64b2a2a8a904f179dcf0f8a5e Mon Sep 17 00:00:00 2001 From: Jonathan Date: Wed, 25 Oct 2023 17:04:57 +0200 Subject: [PATCH 04/27] remove api_type from Configuration, pass api_type to the client factory with the config array --- src/Configuration.php | 8 -------- src/Service.php | 8 ++++++-- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Configuration.php b/src/Configuration.php index 1fff9bb..0cd33f9 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -29,14 +29,6 @@ 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()) diff --git a/src/Service.php b/src/Service.php index 22fd665..3c10e5c 100644 --- a/src/Service.php +++ b/src/Service.php @@ -89,7 +89,9 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep $extractor = $extractorFactory->compile($config['extractor']); $extractorBuilder = $extractor->getBuilder(); - $clientFactory->withApiType($config['extractor']['api_type']); + if (isset($config['extractor']['api_type'])) { + $config['client']['api_type'] = $config['extractor']['api_type']; + } $client = $clientFactory->compile($config['client']); $extractorBuilder->withClient($client->getBuilder()->getNode()); @@ -105,7 +107,9 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep $loader = $loaderFactory->compile($config['loader']); $loaderBuilder = $loader->getBuilder(); - $clientFactory->withApiType($config['loader']['api_type']); + if (isset($config['loader']['api_type'])) { + $config['client']['api_type'] = $config['loader']['api_type']; + } $client = $clientFactory->compile($config['client']); $loaderBuilder->withClient($client->getBuilder()->getNode()); From 28283e1cc435cd90bc446ae411646ae5e343cd7d Mon Sep 17 00:00:00 2001 From: Jonathan Date: Wed, 25 Oct 2023 17:26:52 +0200 Subject: [PATCH 05/27] extract endpoints/methods from AdminApi on extractor/loader class --- src/Configuration/Extractor.php | 242 +++++++++++++++++++++++++++++++- src/Configuration/Loader.php | 148 +++++++++++++++++-- 2 files changed, 374 insertions(+), 16 deletions(-) diff --git a/src/Configuration/Extractor.php b/src/Configuration/Extractor.php index dce1044..014d9dc 100644 --- a/src/Configuration/Extractor.php +++ b/src/Configuration/Extractor.php @@ -4,6 +4,7 @@ namespace Kiboko\Plugin\Sylius\Configuration; +use phpDocumentor\Reflection\Types\Self_; use Symfony\Component\Config; use function Kiboko\Component\SatelliteToolbox\Configuration\asExpression; @@ -11,7 +12,7 @@ final class Extractor implements Config\Definition\ConfigurationInterface { - private static array $endpoints = [ + private static array $endpointsLegacy = [ // Core Endpoints 'channels' => [ 'listPerPage', @@ -125,13 +126,221 @@ final class Extractor implements Config\Definition\ConfigurationInterface ], ]; - private static array $doubleEndpoints = [ + private static array $endpointsAdmin = [ + 'address' => [ + 'get', + ], + 'adjustment' => [ + 'listPerPage', + 'all', + 'get', + ], + 'administrator' => [ + 'listPerPage', + 'all', + 'get', + ], + 'avatarImage' => [ + 'get', + ], + 'catalogPromotion' => [ + 'listPerPage', + 'all', + 'get', + ], + 'catalogPromotionTranslation' => [ + 'get', + ], + 'channel' => [ + 'listPerPage', + 'all', + 'get', + ], + 'country' => [ + 'listPerPage', + 'all', + 'get', + ], + 'currency' => [ + 'listPerPage', + 'all', + 'get', + ], + 'customer' => [ + 'get', + ], + 'customerGroup' => [ + 'listPerPage', + 'all', + 'get', + ], + 'exchangeRate' => [ + 'listPerPage', + 'all', + 'get', + ], + 'locale' => [ + 'listPerPage', + 'all', + 'get', + ], + 'order' => [ + 'listPerPage', + 'all', + 'get', + 'listPaymentsPerPage', + 'allPayments', + 'listShipmentPerPage', + 'allShipments' + ], + 'orderItem' => [ + 'get', + ], + 'orderItemUnit' => [ + 'get', + ], + 'payment' => [ + 'listPerPage', + 'all', + 'get', + ], + 'paymentMethod' => [ + 'get', + ], + 'product' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productAssociationType' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productAssociationTypeTranslation' => [ + 'get', + ], + 'productImage' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productOption' => [ + 'listPerPage', + 'all', + 'get', + 'listValuesPerPage', + 'allValues', + ], + 'productOptionTranslation' => [ + 'get', + ], + 'productOptionValue' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productReview' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productTaxon' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productTranslation' => [ + 'get', + ], + 'productVariant' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productVariantTranslation' => [ + 'get', + ], + 'promotion' => [ + 'listPerPage', + 'all', + 'get', + ], + 'province' => [ + 'listPerPage', + 'all', + 'get', + ], + 'shipment' => [ + 'listPerPage', + 'all', + 'get', + ], + 'shippingCategory' => [ + 'listPerPage', + 'all', + 'get', + ], + 'shippingMethod' => [ + 'listPerPage', + 'all', + 'get', + ], + 'ShopBillingData' => [ + 'listPerPage', + 'all', + 'get', + ], + 'taxCategory' => [ + 'listPerPage', + 'all', + 'get', + ], + 'taxon' => [ + 'listPerPage', + 'all', + 'get', + ], + 'taxonTranslation' => [ + 'listPerPage', + 'all', + 'get', + ], + 'zone' => [ + 'listPerPage', + 'all', + 'get', + ], + 'zoneMember' => [ + 'listPerPage', + 'all', + 'get', + ], + ]; + + private static array $endpointsShop = [ + // Core Endpoints + ]; + + private static array $doubleEndpointsLegacy = [ // Double resources Endpoints 'productReviews', 'productVariants', 'promotionCoupons', ]; + private static array $doubleEndpointsAdmin = [ + // Double resources Endpoints + 'adjustment', + 'province', + 'shopBillingData', + 'zoneMember', + ]; + + private static array $doubleEndpointsShop = [ + // Double resources Endpoints + ]; + public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder { $filters = new Search(); @@ -143,12 +352,33 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui ->validate() ->ifArray() ->then(function (array $item) { + switch($item['api_type']) { + case 'admin': + $endpoints = self::$endpointsAdmin; + $doubleEndpoints = self::$doubleEndpointsAdmin; + break; + case 'shop': + $endpoints = self::$endpointsShop; + $doubleEndpoints = self::$doubleEndpointsShop; + break; + case 'legacy': + $endpoints = self::$endpointsLegacy; + $doubleEndpoints = self::$doubleEndpointsLegacy; + break; + default: + $endpoints = []; + $doubleEndpoints = []; + break; + } + if (!\in_array(array_keys(array_merge($endpoints, $doubleEndpoints)), $item['type'])) { + throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_keys($endpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR))); + } if ( - \array_key_exists($item['type'], self::$endpoints) - && !\in_array($item['method'], self::$endpoints[$item['type']]) - && !\in_array($item['type'], self::$doubleEndpoints) + \array_key_exists($item['type'], $endpoints) + && !\in_array($item['method'], $endpoints[$item['type']]) + && !\in_array($item['type'], $doubleEndpoints) ) { - throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', self::$endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR))); + throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR))); } return $item; diff --git a/src/Configuration/Loader.php b/src/Configuration/Loader.php index 6f1b7dc..2a4c710 100644 --- a/src/Configuration/Loader.php +++ b/src/Configuration/Loader.php @@ -4,11 +4,12 @@ namespace Kiboko\Plugin\Sylius\Configuration; +use phpDocumentor\Reflection\Types\Self_; use Symfony\Component\Config; final class Loader implements Config\Definition\ConfigurationInterface { - private static array $endpoints = [ + private static array $endpointsLegacy = [ // Core Endpoints 'channels' => [ 'create', @@ -101,6 +102,127 @@ final class Loader implements Config\Definition\ConfigurationInterface ], ]; + private static array $endpointsAdmin = [ + // Core Endpoints + 'administrator' => [ + 'create', + 'delete', + 'upsert', + ], + 'avatarImage' => [ + 'create', + 'delete', + ], + 'catalogPromotion' => [ + 'create', + 'upsert', + ], + 'channel' => [ + 'create', + 'delete', + ], + 'country' => [ + 'create', + 'delete', + ], + 'currency' => [ + 'create', + 'delete', + ], + 'customerGroup' => [ + 'create', + 'delete', + 'upsert', + ], + 'exchangeRate' => [ + 'create', + 'delete', + 'upsert', + ], + 'locale' => [ + 'create', + ], + 'order' => [ + 'cancel', + ], + 'payment' => [ + 'complete', + ], + 'product' => [ + 'create', + 'delete', + 'upsert', + ], + 'productAssociationType' => [ + 'create', + 'delete', + 'upsert', + ], + 'productOption' => [ + 'create', + 'delete', + ], + 'productReview' => [ + 'upsert', + 'delete', + 'accept', + 'reject', + ], + 'productVariant' => [ + 'create', + 'upsert', + ], + 'promotion' => [ + 'create', + 'delete', + ], + 'province' => [ + 'upsert', + ], + 'resetPasswordRequest' => [ + 'create', + 'acknowledge', + ], + 'shipment' => [ + 'ship', + ], + 'shippingCategory' => [ + 'create', + 'delete', + 'upsert', + ], + 'shippingMethod' => [ + 'create', + 'delete', + 'upsert', + 'archive', + 'restore', + ], + 'taxCategory' => [ + 'create', + 'delete', + 'upsert', + ], + 'taxon' => [ + 'create', + 'upsert', + ], + 'verifyCustomerAccount' => [ + 'create', + 'acknowledge', + ], + 'zone' => [ + 'create', + 'delete', + 'upsert', + ], + ]; + + private static array $endpointsShop = [ + // Core Endpoints + + ]; + public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder { $builder = new Config\Definition\Builder\TreeBuilder('loader'); @@ -110,8 +232,16 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui ->validate() ->ifArray() ->then(function (array $item) { - if (!\in_array($item['method'], self::$endpoints[$item['type']])) { - throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', self::$endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR))); + $endpoints = match ($item['api_type']) { + 'admin' => self::$endpointsAdmin, + 'shop' => self::$endpointsShop, + 'legacy' => self::$endpointsLegacy + }; + if (!\in_array(array_keys($endpoints), $item['type'])) { + throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_keys($endpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR))); + } + if (!\in_array($item['method'], $endpoints[$item['type']])) { + throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR))); } return $item; @@ -124,14 +254,12 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui ->end() ->scalarNode('type') ->isRequired() - ->validate() - ->ifNotInArray(array_keys(self::$endpoints)) - ->thenInvalid( - sprintf('the value should be one of [%s]', implode(', ', array_keys(self::$endpoints))) - ) - ->end() + ->cannotBeEmpty() + ->end() + ->scalarNode('method') + ->isRequired() + ->cannotBeEmpty() ->end() - ->scalarNode('method')->end() ->end() ; From ca7e08f98a50f0035258cc603c6171283644062a Mon Sep 17 00:00:00 2001 From: Jonathan Date: Thu, 26 Oct 2023 10:14:12 +0200 Subject: [PATCH 06/27] extract endpoints/methods from ShopApi on extractor/loader class --- src/Configuration/Extractor.php | 152 +++++++++++++++++++++++++++----- src/Configuration/Loader.php | 36 +++++++- src/Service.php | 8 +- 3 files changed, 166 insertions(+), 30 deletions(-) diff --git a/src/Configuration/Extractor.php b/src/Configuration/Extractor.php index 014d9dc..6a1719b 100644 --- a/src/Configuration/Extractor.php +++ b/src/Configuration/Extractor.php @@ -190,7 +190,7 @@ final class Extractor implements Config\Definition\ConfigurationInterface 'get', 'listPaymentsPerPage', 'allPayments', - 'listShipmentPerPage', + 'listShipmentsPerPage', 'allShipments' ], 'orderItem' => [ @@ -320,6 +320,128 @@ final class Extractor implements Config\Definition\ConfigurationInterface private static array $endpointsShop = [ // Core Endpoints + 'address' => [ + 'listPerPage', + 'all', + 'get', + ], + 'adjustment' => [ + 'listPerPage', + 'all', + 'get', + ], + 'catalogPromotion' => [ + 'get', + ], + 'channel' => [ + 'get', + ], + 'country' => [ + 'listPerPage', + 'all', + 'get', + ], + 'currency' => [ + 'listPerPage', + 'all', + 'get', + ], + 'customer' => [ + 'get', + ], + 'locale' => [ + 'listPerPage', + 'all', + 'get', + ], + 'order' => [ + 'listPerPage', + 'all', + 'get', + 'listPaymentMethodsPerPage', + 'allPaymentMethods', + 'listShipmentMethodsPerPage', + 'allShipmentMethods', + 'listAdjustmentsPerPage', + 'allAdjustments', + 'listItemsPerPage', + 'allItems', + ], + 'orderItem' => [ + 'listPerPage', + 'all', + 'get', + 'listAdjustmentsPerPage', + 'allAdjustments', + ], + 'orderItemUnit' => [ + 'get', + ], + 'payment' => [ + 'listPerPage', + 'all', + 'get', + ], + 'paymentMethod' => [ + 'listPerPage', + 'all', + 'get', + ], + 'product' => [ + 'listPerPage', + 'all', + 'get', + 'getBySlug', + ], + 'productImage' => [ + 'get', + ], + 'productOption' => [ + 'get', + ], + 'productOptionValue' => [ + 'get', + ], + 'productReview' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productTaxon' => [ + 'get', + ], + 'productTranslation' => [ + 'get', + ], + 'productVariant' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productVariantTranslation' => [ + 'get', + ], + 'shipment' => [ + 'listPerPage', + 'all', + 'get', + ], + 'shippingMethod' => [ + 'listPerPage', + 'all', + 'get', + ], + 'shippingMethodTranslation' => [ + 'get', + ], + 'taxon' => [ + 'listPerPage', + 'all', + 'get', + ], + 'taxonTranslation' => [ + 'get', + ], ]; private static array $doubleEndpointsLegacy = [ @@ -339,6 +461,8 @@ final class Extractor implements Config\Definition\ConfigurationInterface private static array $doubleEndpointsShop = [ // Double resources Endpoints + 'adjustment', + 'order', ]; public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder @@ -370,8 +494,8 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui $doubleEndpoints = []; break; } - if (!\in_array(array_keys(array_merge($endpoints, $doubleEndpoints)), $item['type'])) { - throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_keys($endpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR))); + if (!\in_array($item['type'], array_merge(array_keys($endpoints), $doubleEndpoints))) { + throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_merge(array_keys($endpoints), $doubleEndpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR))); } if ( \array_key_exists($item['type'], $endpoints) @@ -380,20 +504,13 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui ) { throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR))); } + if (\in_array($item['type'], $doubleEndpoints) && !\array_key_exists('code', $item)) { + throw new \InvalidArgumentException(sprintf('The %s type should have a "code" field set.', $item['type'])); + } return $item; }) ->end() - ->validate() - ->ifArray() - ->then(function (array $item) { - if (\in_array($item['type'], self::$doubleEndpoints) && !\array_key_exists('code', $item)) { - throw new \InvalidArgumentException(sprintf('The %s type should have a "code" field set.', $item['type'])); - } - - return $item; - }) - ->end() ->children() ->scalarNode('api_type') ->isRequired() @@ -401,15 +518,6 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui ->end() ->scalarNode('type') ->isRequired() - ->validate() - ->ifNotInArray(array_merge(array_keys(self::$endpoints), self::$doubleEndpoints)) - ->thenInvalid( - sprintf( - 'the value should be one of [%s], got %%s', - implode(', ', array_merge(array_keys(self::$endpoints), self::$doubleEndpoints)) - ) - ) - ->end() ->end() ->scalarNode('method')->end() ->scalarNode('code') diff --git a/src/Configuration/Loader.php b/src/Configuration/Loader.php index 2a4c710..3781cda 100644 --- a/src/Configuration/Loader.php +++ b/src/Configuration/Loader.php @@ -220,7 +220,39 @@ final class Loader implements Config\Definition\ConfigurationInterface private static array $endpointsShop = [ // Core Endpoints - + 'address' => [ + 'create', + 'delete', + 'upsert', + ], + 'customer' => [ + 'create', + 'upsert', + 'changePassword', + ], + 'order' => [ + 'create', + 'upsert', + 'choosePayment', + 'chooseShipment', + 'complete', + ], + 'orderItem' => [ + 'create', + 'delete', + 'changeQuantity', + ], + 'productReview' => [ + 'create', + ], + 'resetPasswordRequest' => [ + 'create', + 'verify', + ], + 'verifyCustomerAccount' => [ + 'create', + 'verify', + ], ]; public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder @@ -237,7 +269,7 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui 'shop' => self::$endpointsShop, 'legacy' => self::$endpointsLegacy }; - if (!\in_array(array_keys($endpoints), $item['type'])) { + if (!\in_array($item['type'], array_keys($endpoints))) { throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_keys($endpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR))); } if (!\in_array($item['method'], $endpoints[$item['type']])) { diff --git a/src/Service.php b/src/Service.php index 3c10e5c..7124f19 100644 --- a/src/Service.php +++ b/src/Service.php @@ -89,9 +89,7 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep $extractor = $extractorFactory->compile($config['extractor']); $extractorBuilder = $extractor->getBuilder(); - if (isset($config['extractor']['api_type'])) { - $config['client']['api_type'] = $config['extractor']['api_type']; - } + $config['client']['api_type'] = $config['extractor']['api_type']; $client = $clientFactory->compile($config['client']); $extractorBuilder->withClient($client->getBuilder()->getNode()); @@ -107,9 +105,7 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep $loader = $loaderFactory->compile($config['loader']); $loaderBuilder = $loader->getBuilder(); - if (isset($config['loader']['api_type'])) { - $config['client']['api_type'] = $config['loader']['api_type']; - } + $config['client']['api_type'] = $config['loader']['api_type']; $client = $clientFactory->compile($config['client']); $loaderBuilder->withClient($client->getBuilder()->getNode()); From 3c3000c21593bd131aaa75e335119ce6cf027b5c Mon Sep 17 00:00:00 2001 From: Jonathan Date: Thu, 26 Oct 2023 11:10:35 +0200 Subject: [PATCH 07/27] extract endpoints for capacity All, fix a return type for filters on All capacity file --- src/Builder/Search.php | 16 ++++---- src/Capacity/All.php | 93 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 97 insertions(+), 12 deletions(-) diff --git a/src/Builder/Search.php b/src/Builder/Search.php index fe16ec9..c7baff2 100644 --- a/src/Builder/Search.php +++ b/src/Builder/Search.php @@ -9,16 +9,16 @@ final class Search implements Builder { - public function __construct(private array $filters = []) - { - } + public function __construct( + private array $filters = [] + ) {} public function addFilter( Node\Expr $field, Node\Expr $operator, Node\Expr $value = null, - ?Node\Expr $scope = null, - ?Node\Expr $locale = null + Node\Expr $scope = null, + Node\Expr $locale = null ): self { $arguments = [ new Node\Arg( @@ -28,7 +28,7 @@ public function addFilter( value: $operator, ), new Node\Arg( - value: $value, + value: $value ?? new Node\Expr\ConstFetch(new Node\Name('null')), ), ]; @@ -41,7 +41,7 @@ public function addFilter( } if (null !== $locale) { $options[] = new Node\Expr\ArrayItem( - value: $scope, + value: $locale, key: new Node\Scalar\String_('locale'), ); } @@ -60,7 +60,7 @@ public function addFilter( return $this; } - public function getNode(): Node + public function getNode(): Node\Expr { $instance = new Node\Expr\New_( class: new Node\Name\FullyQualified(\Diglin\Sylius\ApiClient\Search\SearchBuilder::class) diff --git a/src/Capacity/All.php b/src/Capacity/All.php index 61593ca..2d9cc34 100644 --- a/src/Capacity/All.php +++ b/src/Capacity/All.php @@ -13,7 +13,7 @@ final class All implements CapacityInterface { - private static array $endpoints = [ + private static array $endpointsLegacy = [ // Simple resources Endpoints 'channels', 'countries', @@ -41,26 +41,111 @@ final class All implements CapacityInterface 'zones', ]; - private static array $doubleEndpoints = [ + private static array $endpointsAdmin = [ + // Simple Ressource Endpoints + 'adjustment', + 'administrator', + 'catalogPromotion', + 'channel', + 'country', + 'currency', + 'customerGroup', + 'exchangeRate', + 'locale', + 'order', + 'payment', + 'product', + 'productAssociationType', + 'productImage', + 'productOption', + 'productOptionValue', + 'productReview', + 'productTaxon', + 'productVariant', + 'promotion', + 'province', + 'shipment', + 'shippingCategory', + 'shippingMethod', + 'ShopBillingData', + 'taxCategory', + 'taxon', + 'taxonTranslation', + 'zone', + 'zoneMember', + ]; + + private static array $endpointsShop = [ + // Simple Ressource Endpoints + 'address', + 'adjustment', + 'country', + 'currency', + 'locale', + 'order', + 'orderItem', + 'payment', + 'paymentMethod', + 'product', + 'productReview', + 'productVariant', + 'shipment', + 'shippingMethod', + 'taxon', + ]; + + private static array $doubleEndpointsLegacy = [ // Double resources Endpoints 'productReviews', 'productVariants', 'promotionCoupons', ]; + private static array $doubleEndpointsAdmin = [ + // Double resources Endpoints + 'adjustment', + 'province', + 'shopBillingData', + 'zoneMember', + ]; + + private static array $doubleEndpointsShop = [ + // Double resources Endpoints + 'adjustment', + 'order', + ]; + public function __construct(private readonly ExpressionLanguage $interpreter) { } public function applies(array $config): bool { + switch($config['api_type']) { + case 'admin': + $endpoints = self::$endpointsAdmin; + $doubleEndpoints = self::$doubleEndpointsAdmin; + break; + case 'shop': + $endpoints = self::$endpointsShop; + $doubleEndpoints = self::$doubleEndpointsShop; + break; + case 'legacy': + $endpoints = self::$endpointsLegacy; + $doubleEndpoints = self::$doubleEndpointsLegacy; + break; + default: + $endpoints = []; + $doubleEndpoints = []; + break; + } return isset($config['type']) - && (\in_array($config['type'], self::$endpoints) || \in_array($config['type'], self::$doubleEndpoints)) + && (\in_array($config['type'], $endpoints) || \in_array($config['type'], $doubleEndpoints)) && isset($config['method']) && 'all' === $config['method']; } - private function compileFilters(array ...$filters): Node + private function compileFilters(array ...$filters): Node\Expr { $builder = new Sylius\Builder\Search(); foreach ($filters as $filter) { From bc7fcbf881807e858466fb4e4776a879bab6ad5c Mon Sep 17 00:00:00 2001 From: Jonathan Date: Thu, 26 Oct 2023 12:11:09 +0200 Subject: [PATCH 08/27] extract endpoints for capacity Create/Upsert/ListPerPage --- src/Capacity/Create.php | 45 +++++++++++++++++- src/Capacity/ListPerPage.php | 92 ++++++++++++++++++++++++++++++++++-- src/Capacity/Upsert.php | 35 +++++++++++++- 3 files changed, 164 insertions(+), 8 deletions(-) diff --git a/src/Capacity/Create.php b/src/Capacity/Create.php index 0b02a08..cc4d452 100644 --- a/src/Capacity/Create.php +++ b/src/Capacity/Create.php @@ -10,7 +10,7 @@ final class Create implements CapacityInterface { - private static array $endpoints = [ + private static array $endpointsLegacy = [ // Core Endpoints 'channels', 'countries', @@ -40,10 +40,51 @@ final class Create implements CapacityInterface 'zones', ]; + private static array $endpointsAdmin = [ + // Core Endpoints + 'administrator', + 'avatarImage', + 'catalogPromotion', + 'channel', + 'country', + 'currency', + 'customerGroup', + 'exchangeRate', + 'locale', + 'product', + 'productAssociationType', + 'productOption', + 'productVariant', + 'promotion', + 'resetPasswordRequest', + 'shippingCategory', + 'shippingMethod', + 'taxCategory', + 'taxon', + 'verifyCustomerAccount', + 'zone', + ]; + + private static array $endpointsShop = [ + // Core Endpoints + 'address', + 'customer', + 'order', + 'orderItem', + 'productReview', + 'resetPasswordRequest', + 'verifyCustomerAccount', + ]; + public function applies(array $config): bool { + $endpoints = match($config['api_type']) { + 'admin' => self::$endpointsAdmin, + 'shop' =>self::$endpointsShop, + 'legacy' => self::$endpointsLegacy, + }; return isset($config['type']) - && \in_array($config['type'], self::$endpoints) + && \in_array($config['type'], $endpoints) && isset($config['method']) && 'create' === $config['method']; } diff --git a/src/Capacity/ListPerPage.php b/src/Capacity/ListPerPage.php index fef12ef..b78d9b9 100644 --- a/src/Capacity/ListPerPage.php +++ b/src/Capacity/ListPerPage.php @@ -13,7 +13,7 @@ final class ListPerPage implements CapacityInterface { - private static array $endpoints = [ + private static array $endpointsLegacy = [ // Simple resources Endpoints 'channels', 'countries', @@ -41,12 +41,78 @@ final class ListPerPage implements CapacityInterface 'zones', ]; - private static array $doubleEndpoints = [ + private static array $endpointsAdmin = [ + // Simple Ressource Endpoints + 'adjustment', + 'administrator', + 'catalogPromotion', + 'channel', + 'country', + 'currency', + 'customerGroup', + 'exchangeRate', + 'locale', + 'order', + 'payment', + 'product', + 'productAssociationType', + 'productImage', + 'productOption', + 'productOptionValue', + 'productReview', + 'productTaxon', + 'productVariant', + 'promotion', + 'province', + 'shipment', + 'shippingCategory', + 'shippingMethod', + 'ShopBillingData', + 'taxCategory', + 'taxon', + 'taxonTranslation', + 'zone', + 'zoneMember', + ]; + + private static array $endpointsShop = [ + // Simple Ressource Endpoints + 'address', + 'adjustment', + 'country', + 'currency', + 'locale', + 'order', + 'orderItem', + 'payment', + 'paymentMethod', + 'product', + 'productReview', + 'productVariant', + 'shipment', + 'shippingMethod', + 'taxon', + ]; + + private static array $doubleEndpointsLegacy = [ // Double resources Endpoints 'productReviews', 'productVariants', 'promotionCoupons', ]; + private static array $doubleEndpointsAdmin = [ + // Double resources Endpoints + 'adjustment', + 'province', + 'shopBillingData', + 'zoneMember', + ]; + + private static array $doubleEndpointsShop = [ + // Double resources Endpoints + 'adjustment', + 'order', + ]; public function __construct(private readonly ExpressionLanguage $interpreter) { @@ -54,13 +120,31 @@ public function __construct(private readonly ExpressionLanguage $interpreter) public function applies(array $config): bool { + switch($config['api_type']) { + case 'admin': + $endpoints = self::$endpointsAdmin; + $doubleEndpoints = self::$doubleEndpointsAdmin; + break; + case 'shop': + $endpoints = self::$endpointsShop; + $doubleEndpoints = self::$doubleEndpointsShop; + break; + case 'legacy': + $endpoints = self::$endpointsLegacy; + $doubleEndpoints = self::$doubleEndpointsLegacy; + break; + default: + $endpoints = []; + $doubleEndpoints = []; + break; + } return isset($config['type']) - && (\in_array($config['type'], self::$endpoints) || \in_array($config['type'], self::$doubleEndpoints)) + && (\in_array($config['type'], $endpoints) || \in_array($config['type'], $doubleEndpoints)) && isset($config['method']) && 'listPerPage' === $config['method']; } - private function compileFilters(array ...$filters): Node + private function compileFilters(array ...$filters): Node\Expr { $builder = new Sylius\Builder\Search(); foreach ($filters as $filter) { diff --git a/src/Capacity/Upsert.php b/src/Capacity/Upsert.php index 83b7079..92eb190 100644 --- a/src/Capacity/Upsert.php +++ b/src/Capacity/Upsert.php @@ -10,7 +10,7 @@ final class Upsert implements CapacityInterface { - private static array $endpoints = [ + private static array $endpointsLegacy = [ // Core Endpoints 'carts', 'channels', @@ -42,10 +42,41 @@ final class Upsert implements CapacityInterface 'zones', ]; + private static array $endpointsAdmin = [ + // Core Endpoints + 'administrator', + 'catalogPromotion', + 'customerGroup', + 'exchangeRate', + 'product', + 'productAssociationType', + 'productOption', + 'productReview', + 'productVariant', + 'province', + 'shippingCategory', + 'shippingMethod', + 'taxCategory', + 'taxon', + 'zone', + ]; + + private static array $endpointsShop = [ + // Core Endpoints + 'address', + 'customer', + 'order', + ]; + public function applies(array $config): bool { + $endpoints = match($config['api_type']) { + 'admin' => self::$endpointsAdmin, + 'shop' =>self::$endpointsShop, + 'legacy' => self::$endpointsLegacy, + }; return isset($config['type']) - && \in_array($config['type'], self::$endpoints) + && \in_array($config['type'], $endpoints) && isset($config['method']) && 'upsert' === $config['method']; } From 87b5b69d39a74ffd2e70dce79fbdae4cdf89f935 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Fri, 27 Oct 2023 13:57:02 +0200 Subject: [PATCH 09/27] create new validator classes for Loader and Extractor config --- src/Configuration/Extractor.php | 487 +---------------- src/Configuration/Loader.php | 261 +-------- .../ConfigurationValidatorInterface.php | 8 + .../ExtractorConfigurationValidator.php | 495 ++++++++++++++++++ .../LoaderConfigurationValidator.php | 267 ++++++++++ 5 files changed, 774 insertions(+), 744 deletions(-) create mode 100644 src/Validator/ConfigurationValidatorInterface.php create mode 100644 src/Validator/ExtractorConfigurationValidator.php create mode 100644 src/Validator/LoaderConfigurationValidator.php diff --git a/src/Configuration/Extractor.php b/src/Configuration/Extractor.php index 6a1719b..ca29fad 100644 --- a/src/Configuration/Extractor.php +++ b/src/Configuration/Extractor.php @@ -4,7 +4,7 @@ namespace Kiboko\Plugin\Sylius\Configuration; -use phpDocumentor\Reflection\Types\Self_; +use Kiboko\Plugin\Sylius\Validator\ExtractorConfigurationValidator; use Symfony\Component\Config; use function Kiboko\Component\SatelliteToolbox\Configuration\asExpression; @@ -12,458 +12,7 @@ final class Extractor implements Config\Definition\ConfigurationInterface { - private static array $endpointsLegacy = [ - // Core Endpoints - 'channels' => [ - 'listPerPage', - 'all', - 'get', - ], - 'countries' => [ - 'listPerPage', - 'all', - 'get', - ], - 'carts' => [ - 'listPerPage', - 'all', - 'get', - ], - 'currencies' => [ - 'listPerPage', - 'all', - 'get', - ], - 'customers' => [ - 'listPerPage', - 'all', - 'get', - ], - 'exchangeRates' => [ - 'listPerPage', - 'all', - 'get', - ], - 'locales' => [ - 'listPerPage', - 'all', - 'get', - ], - 'orders' => [ - 'listPerPage', - 'all', - 'get', - ], - 'payments' => [ - 'listPerPage', - 'all', - 'get', - ], - 'paymentMethods' => [ - 'listPerPage', - 'all', - 'get', - ], - 'products' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productAttributes' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productAssociationTypes' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productOptions' => [ - 'listPerPage', - 'all', - 'get', - ], - 'promotions' => [ - 'listPerPage', - 'all', - 'get', - ], - 'shipments' => [ - 'listPerPage', - 'all', - 'get', - ], - 'shippingCategories' => [ - 'listPerPage', - 'all', - 'get', - ], - 'taxCategories' => [ - 'listPerPage', - 'all', - 'get', - ], - 'taxRates' => [ - 'listPerPage', - 'all', - 'get', - ], - 'taxons' => [ - 'listPerPage', - 'all', - 'get', - ], - 'users' => [ - 'listPerPage', - 'all', - 'get', - ], - 'zones' => [ - 'listPerPage', - 'all', - 'get', - ], - ]; - private static array $endpointsAdmin = [ - 'address' => [ - 'get', - ], - 'adjustment' => [ - 'listPerPage', - 'all', - 'get', - ], - 'administrator' => [ - 'listPerPage', - 'all', - 'get', - ], - 'avatarImage' => [ - 'get', - ], - 'catalogPromotion' => [ - 'listPerPage', - 'all', - 'get', - ], - 'catalogPromotionTranslation' => [ - 'get', - ], - 'channel' => [ - 'listPerPage', - 'all', - 'get', - ], - 'country' => [ - 'listPerPage', - 'all', - 'get', - ], - 'currency' => [ - 'listPerPage', - 'all', - 'get', - ], - 'customer' => [ - 'get', - ], - 'customerGroup' => [ - 'listPerPage', - 'all', - 'get', - ], - 'exchangeRate' => [ - 'listPerPage', - 'all', - 'get', - ], - 'locale' => [ - 'listPerPage', - 'all', - 'get', - ], - 'order' => [ - 'listPerPage', - 'all', - 'get', - 'listPaymentsPerPage', - 'allPayments', - 'listShipmentsPerPage', - 'allShipments' - ], - 'orderItem' => [ - 'get', - ], - 'orderItemUnit' => [ - 'get', - ], - 'payment' => [ - 'listPerPage', - 'all', - 'get', - ], - 'paymentMethod' => [ - 'get', - ], - 'product' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productAssociationType' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productAssociationTypeTranslation' => [ - 'get', - ], - 'productImage' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productOption' => [ - 'listPerPage', - 'all', - 'get', - 'listValuesPerPage', - 'allValues', - ], - 'productOptionTranslation' => [ - 'get', - ], - 'productOptionValue' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productReview' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productTaxon' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productTranslation' => [ - 'get', - ], - 'productVariant' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productVariantTranslation' => [ - 'get', - ], - 'promotion' => [ - 'listPerPage', - 'all', - 'get', - ], - 'province' => [ - 'listPerPage', - 'all', - 'get', - ], - 'shipment' => [ - 'listPerPage', - 'all', - 'get', - ], - 'shippingCategory' => [ - 'listPerPage', - 'all', - 'get', - ], - 'shippingMethod' => [ - 'listPerPage', - 'all', - 'get', - ], - 'ShopBillingData' => [ - 'listPerPage', - 'all', - 'get', - ], - 'taxCategory' => [ - 'listPerPage', - 'all', - 'get', - ], - 'taxon' => [ - 'listPerPage', - 'all', - 'get', - ], - 'taxonTranslation' => [ - 'listPerPage', - 'all', - 'get', - ], - 'zone' => [ - 'listPerPage', - 'all', - 'get', - ], - 'zoneMember' => [ - 'listPerPage', - 'all', - 'get', - ], - ]; - - private static array $endpointsShop = [ - // Core Endpoints - 'address' => [ - 'listPerPage', - 'all', - 'get', - ], - 'adjustment' => [ - 'listPerPage', - 'all', - 'get', - ], - 'catalogPromotion' => [ - 'get', - ], - 'channel' => [ - 'get', - ], - 'country' => [ - 'listPerPage', - 'all', - 'get', - ], - 'currency' => [ - 'listPerPage', - 'all', - 'get', - ], - 'customer' => [ - 'get', - ], - 'locale' => [ - 'listPerPage', - 'all', - 'get', - ], - 'order' => [ - 'listPerPage', - 'all', - 'get', - 'listPaymentMethodsPerPage', - 'allPaymentMethods', - 'listShipmentMethodsPerPage', - 'allShipmentMethods', - 'listAdjustmentsPerPage', - 'allAdjustments', - 'listItemsPerPage', - 'allItems', - ], - 'orderItem' => [ - 'listPerPage', - 'all', - 'get', - 'listAdjustmentsPerPage', - 'allAdjustments', - ], - 'orderItemUnit' => [ - 'get', - ], - 'payment' => [ - 'listPerPage', - 'all', - 'get', - ], - 'paymentMethod' => [ - 'listPerPage', - 'all', - 'get', - ], - 'product' => [ - 'listPerPage', - 'all', - 'get', - 'getBySlug', - ], - 'productImage' => [ - 'get', - ], - 'productOption' => [ - 'get', - ], - 'productOptionValue' => [ - 'get', - ], - 'productReview' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productTaxon' => [ - 'get', - ], - 'productTranslation' => [ - 'get', - ], - 'productVariant' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productVariantTranslation' => [ - 'get', - ], - 'shipment' => [ - 'listPerPage', - 'all', - 'get', - ], - 'shippingMethod' => [ - 'listPerPage', - 'all', - 'get', - ], - 'shippingMethodTranslation' => [ - 'get', - ], - 'taxon' => [ - 'listPerPage', - 'all', - 'get', - ], - 'taxonTranslation' => [ - 'get', - ], - ]; - - private static array $doubleEndpointsLegacy = [ - // Double resources Endpoints - 'productReviews', - 'productVariants', - 'promotionCoupons', - ]; - - private static array $doubleEndpointsAdmin = [ - // Double resources Endpoints - 'adjustment', - 'province', - 'shopBillingData', - 'zoneMember', - ]; - - private static array $doubleEndpointsShop = [ - // Double resources Endpoints - 'adjustment', - 'order', - ]; public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder { @@ -476,39 +25,7 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui ->validate() ->ifArray() ->then(function (array $item) { - switch($item['api_type']) { - case 'admin': - $endpoints = self::$endpointsAdmin; - $doubleEndpoints = self::$doubleEndpointsAdmin; - break; - case 'shop': - $endpoints = self::$endpointsShop; - $doubleEndpoints = self::$doubleEndpointsShop; - break; - case 'legacy': - $endpoints = self::$endpointsLegacy; - $doubleEndpoints = self::$doubleEndpointsLegacy; - break; - default: - $endpoints = []; - $doubleEndpoints = []; - break; - } - if (!\in_array($item['type'], array_merge(array_keys($endpoints), $doubleEndpoints))) { - throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_merge(array_keys($endpoints), $doubleEndpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR))); - } - if ( - \array_key_exists($item['type'], $endpoints) - && !\in_array($item['method'], $endpoints[$item['type']]) - && !\in_array($item['type'], $doubleEndpoints) - ) { - throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR))); - } - if (\in_array($item['type'], $doubleEndpoints) && !\array_key_exists('code', $item)) { - throw new \InvalidArgumentException(sprintf('The %s type should have a "code" field set.', $item['type'])); - } - - return $item; + return ExtractorConfigurationValidator::validate($item); }) ->end() ->children() diff --git a/src/Configuration/Loader.php b/src/Configuration/Loader.php index 3781cda..e613c73 100644 --- a/src/Configuration/Loader.php +++ b/src/Configuration/Loader.php @@ -4,256 +4,11 @@ namespace Kiboko\Plugin\Sylius\Configuration; -use phpDocumentor\Reflection\Types\Self_; +use Kiboko\Plugin\Sylius\Validator\LoaderConfigurationValidator; use Symfony\Component\Config; final class Loader implements Config\Definition\ConfigurationInterface { - private static array $endpointsLegacy = [ - // Core Endpoints - 'channels' => [ - 'create', - 'delete', - ], - 'countries' => [ - 'create', - 'delete', - ], - 'carts' => [ - 'create', - 'delete', - ], - 'currencies' => [ - 'create', - 'delete', - ], - 'customers' => [ - 'create', - 'delete', - ], - 'exchangeRates' => [ - 'create', - 'delete', - ], - 'locales' => [ - 'create', - 'delete', - ], - 'orders' => [ - 'create', - 'delete', - ], - 'payments' => [ - 'create', - 'delete', - ], - 'paymentMethods' => [ - 'create', - 'delete', - ], - 'products' => [ - 'create', - 'upsert', - 'delete', - ], - 'productAttributes' => [ - 'create', - 'delete', - ], - 'productAssociationTypes' => [ - 'create', - 'delete', - ], - 'productOptions' => [ - 'create', - 'delete', - ], - 'promotions' => [ - 'create', - 'delete', - ], - 'shipments' => [ - 'create', - 'delete', - ], - 'shippingCategories' => [ - 'create', - 'delete', - ], - 'taxCategories' => [ - 'create', - 'delete', - ], - 'taxRates' => [ - 'create', - 'delete', - ], - 'taxons' => [ - 'create', - 'delete', - ], - 'users' => [ - 'create', - 'delete', - ], - 'zones' => [ - 'create', - 'delete', - ], - ]; - - private static array $endpointsAdmin = [ - // Core Endpoints - 'administrator' => [ - 'create', - 'delete', - 'upsert', - ], - 'avatarImage' => [ - 'create', - 'delete', - ], - 'catalogPromotion' => [ - 'create', - 'upsert', - ], - 'channel' => [ - 'create', - 'delete', - ], - 'country' => [ - 'create', - 'delete', - ], - 'currency' => [ - 'create', - 'delete', - ], - 'customerGroup' => [ - 'create', - 'delete', - 'upsert', - ], - 'exchangeRate' => [ - 'create', - 'delete', - 'upsert', - ], - 'locale' => [ - 'create', - ], - 'order' => [ - 'cancel', - ], - 'payment' => [ - 'complete', - ], - 'product' => [ - 'create', - 'delete', - 'upsert', - ], - 'productAssociationType' => [ - 'create', - 'delete', - 'upsert', - ], - 'productOption' => [ - 'create', - 'delete', - ], - 'productReview' => [ - 'upsert', - 'delete', - 'accept', - 'reject', - ], - 'productVariant' => [ - 'create', - 'upsert', - ], - 'promotion' => [ - 'create', - 'delete', - ], - 'province' => [ - 'upsert', - ], - 'resetPasswordRequest' => [ - 'create', - 'acknowledge', - ], - 'shipment' => [ - 'ship', - ], - 'shippingCategory' => [ - 'create', - 'delete', - 'upsert', - ], - 'shippingMethod' => [ - 'create', - 'delete', - 'upsert', - 'archive', - 'restore', - ], - 'taxCategory' => [ - 'create', - 'delete', - 'upsert', - ], - 'taxon' => [ - 'create', - 'upsert', - ], - 'verifyCustomerAccount' => [ - 'create', - 'acknowledge', - ], - 'zone' => [ - 'create', - 'delete', - 'upsert', - ], - ]; - - private static array $endpointsShop = [ - // Core Endpoints - 'address' => [ - 'create', - 'delete', - 'upsert', - ], - 'customer' => [ - 'create', - 'upsert', - 'changePassword', - ], - 'order' => [ - 'create', - 'upsert', - 'choosePayment', - 'chooseShipment', - 'complete', - ], - 'orderItem' => [ - 'create', - 'delete', - 'changeQuantity', - ], - 'productReview' => [ - 'create', - ], - 'resetPasswordRequest' => [ - 'create', - 'verify', - ], - 'verifyCustomerAccount' => [ - 'create', - 'verify', - ], - ]; public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder { @@ -264,19 +19,7 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui ->validate() ->ifArray() ->then(function (array $item) { - $endpoints = match ($item['api_type']) { - 'admin' => self::$endpointsAdmin, - 'shop' => self::$endpointsShop, - 'legacy' => self::$endpointsLegacy - }; - if (!\in_array($item['type'], array_keys($endpoints))) { - throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_keys($endpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR))); - } - if (!\in_array($item['method'], $endpoints[$item['type']])) { - throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR))); - } - - return $item; + return LoaderConfigurationValidator::validate($item); }) ->end() ->children() diff --git a/src/Validator/ConfigurationValidatorInterface.php b/src/Validator/ConfigurationValidatorInterface.php new file mode 100644 index 0000000..03134bd --- /dev/null +++ b/src/Validator/ConfigurationValidatorInterface.php @@ -0,0 +1,8 @@ + [ + 'listPerPage', + 'all', + 'get', + ], + 'countries' => [ + 'listPerPage', + 'all', + 'get', + ], + 'carts' => [ + 'listPerPage', + 'all', + 'get', + ], + 'currencies' => [ + 'listPerPage', + 'all', + 'get', + ], + 'customers' => [ + 'listPerPage', + 'all', + 'get', + ], + 'exchangeRates' => [ + 'listPerPage', + 'all', + 'get', + ], + 'locales' => [ + 'listPerPage', + 'all', + 'get', + ], + 'orders' => [ + 'listPerPage', + 'all', + 'get', + ], + 'payments' => [ + 'listPerPage', + 'all', + 'get', + ], + 'paymentMethods' => [ + 'listPerPage', + 'all', + 'get', + ], + 'products' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productAttributes' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productAssociationTypes' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productOptions' => [ + 'listPerPage', + 'all', + 'get', + ], + 'promotions' => [ + 'listPerPage', + 'all', + 'get', + ], + 'shipments' => [ + 'listPerPage', + 'all', + 'get', + ], + 'shippingCategories' => [ + 'listPerPage', + 'all', + 'get', + ], + 'taxCategories' => [ + 'listPerPage', + 'all', + 'get', + ], + 'taxRates' => [ + 'listPerPage', + 'all', + 'get', + ], + 'taxons' => [ + 'listPerPage', + 'all', + 'get', + ], + 'users' => [ + 'listPerPage', + 'all', + 'get', + ], + 'zones' => [ + 'listPerPage', + 'all', + 'get', + ], + ]; + + private static array $endpointsAdmin = [ + 'address' => [ + 'get', + ], + 'adjustment' => [ + 'listPerPage', + 'all', + 'get', + ], + 'administrator' => [ + 'listPerPage', + 'all', + 'get', + ], + 'avatarImage' => [ + 'get', + ], + 'catalogPromotion' => [ + 'listPerPage', + 'all', + 'get', + ], + 'catalogPromotionTranslation' => [ + 'get', + ], + 'channel' => [ + 'listPerPage', + 'all', + 'get', + ], + 'country' => [ + 'listPerPage', + 'all', + 'get', + ], + 'currency' => [ + 'listPerPage', + 'all', + 'get', + ], + 'customer' => [ + 'get', + ], + 'customerGroup' => [ + 'listPerPage', + 'all', + 'get', + ], + 'exchangeRate' => [ + 'listPerPage', + 'all', + 'get', + ], + 'locale' => [ + 'listPerPage', + 'all', + 'get', + ], + 'order' => [ + 'listPerPage', + 'all', + 'get', + 'listPaymentsPerPage', + 'allPayments', + 'listShipmentsPerPage', + 'allShipments' + ], + 'orderItem' => [ + 'get', + ], + 'orderItemUnit' => [ + 'get', + ], + 'payment' => [ + 'listPerPage', + 'all', + 'get', + ], + 'paymentMethod' => [ + 'get', + ], + 'product' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productAssociationType' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productAssociationTypeTranslation' => [ + 'get', + ], + 'productImage' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productOption' => [ + 'listPerPage', + 'all', + 'get', + 'listValuesPerPage', + 'allValues', + ], + 'productOptionTranslation' => [ + 'get', + ], + 'productOptionValue' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productReview' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productTaxon' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productTranslation' => [ + 'get', + ], + 'productVariant' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productVariantTranslation' => [ + 'get', + ], + 'promotion' => [ + 'listPerPage', + 'all', + 'get', + ], + 'province' => [ + 'listPerPage', + 'all', + 'get', + ], + 'shipment' => [ + 'listPerPage', + 'all', + 'get', + ], + 'shippingCategory' => [ + 'listPerPage', + 'all', + 'get', + ], + 'shippingMethod' => [ + 'listPerPage', + 'all', + 'get', + ], + 'ShopBillingData' => [ + 'listPerPage', + 'all', + 'get', + ], + 'taxCategory' => [ + 'listPerPage', + 'all', + 'get', + ], + 'taxon' => [ + 'listPerPage', + 'all', + 'get', + ], + 'taxonTranslation' => [ + 'listPerPage', + 'all', + 'get', + ], + 'zone' => [ + 'listPerPage', + 'all', + 'get', + ], + 'zoneMember' => [ + 'listPerPage', + 'all', + 'get', + ], + ]; + + private static array $endpointsShop = [ + // Core Endpoints + 'address' => [ + 'listPerPage', + 'all', + 'get', + ], + 'adjustment' => [ + 'listPerPage', + 'all', + 'get', + ], + 'catalogPromotion' => [ + 'get', + ], + 'channel' => [ + 'get', + ], + 'country' => [ + 'listPerPage', + 'all', + 'get', + ], + 'currency' => [ + 'listPerPage', + 'all', + 'get', + ], + 'customer' => [ + 'get', + ], + 'locale' => [ + 'listPerPage', + 'all', + 'get', + ], + 'order' => [ + 'listPerPage', + 'all', + 'get', + 'listPaymentMethodsPerPage', + 'allPaymentMethods', + 'listShipmentMethodsPerPage', + 'allShipmentMethods', + 'listAdjustmentsPerPage', + 'allAdjustments', + 'listItemsPerPage', + 'allItems', + ], + 'orderItem' => [ + 'listPerPage', + 'all', + 'get', + 'listAdjustmentsPerPage', + 'allAdjustments', + ], + 'orderItemUnit' => [ + 'get', + ], + 'payment' => [ + 'listPerPage', + 'all', + 'get', + ], + 'paymentMethod' => [ + 'listPerPage', + 'all', + 'get', + ], + 'product' => [ + 'listPerPage', + 'all', + 'get', + 'getBySlug', + ], + 'productImage' => [ + 'get', + ], + 'productOption' => [ + 'get', + ], + 'productOptionValue' => [ + 'get', + ], + 'productReview' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productTaxon' => [ + 'get', + ], + 'productTranslation' => [ + 'get', + ], + 'productVariant' => [ + 'listPerPage', + 'all', + 'get', + ], + 'productVariantTranslation' => [ + 'get', + ], + 'shipment' => [ + 'listPerPage', + 'all', + 'get', + ], + 'shippingMethod' => [ + 'listPerPage', + 'all', + 'get', + ], + 'shippingMethodTranslation' => [ + 'get', + ], + 'taxon' => [ + 'listPerPage', + 'all', + 'get', + ], + 'taxonTranslation' => [ + 'get', + ], + ]; + + private static array $doubleEndpointsLegacy = [ + // Double resources Endpoints + 'productReviews', + 'productVariants', + 'promotionCoupons', + ]; + + private static array $doubleEndpointsAdmin = [ + // Double resources Endpoints + 'adjustment', + 'province', + 'shopBillingData', + 'zoneMember', + ]; + + private static array $doubleEndpointsShop = [ + // Double resources Endpoints + 'adjustment', + 'order', + ]; + public static function validate(array $item): array + { + switch($item['api_type']) { + case 'admin': + $endpoints = self::$endpointsAdmin; + $doubleEndpoints = self::$doubleEndpointsAdmin; + break; + case 'shop': + $endpoints = self::$endpointsShop; + $doubleEndpoints = self::$doubleEndpointsShop; + break; + case 'legacy': + $endpoints = self::$endpointsLegacy; + $doubleEndpoints = self::$doubleEndpointsLegacy; + break; + default: + $endpoints = []; + $doubleEndpoints = []; + break; + } + if (!\in_array($item['type'], array_merge(array_keys($endpoints), $doubleEndpoints))) { + throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_merge(array_keys($endpoints), $doubleEndpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR))); + } + if ( + \array_key_exists($item['type'], $endpoints) + && !\in_array($item['method'], $endpoints[$item['type']]) + && !\in_array($item['type'], $doubleEndpoints) + ) { + throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR))); + } + if (\in_array($item['type'], $doubleEndpoints) && !\array_key_exists('code', $item)) { + throw new \InvalidArgumentException(sprintf('The %s type should have a "code" field set.', $item['type'])); + } + + return $item; + } +} diff --git a/src/Validator/LoaderConfigurationValidator.php b/src/Validator/LoaderConfigurationValidator.php new file mode 100644 index 0000000..2cdecbf --- /dev/null +++ b/src/Validator/LoaderConfigurationValidator.php @@ -0,0 +1,267 @@ + [ + 'create', + 'delete', + ], + 'countries' => [ + 'create', + 'delete', + ], + 'carts' => [ + 'create', + 'delete', + ], + 'currencies' => [ + 'create', + 'delete', + ], + 'customers' => [ + 'create', + 'delete', + ], + 'exchangeRates' => [ + 'create', + 'delete', + ], + 'locales' => [ + 'create', + 'delete', + ], + 'orders' => [ + 'create', + 'delete', + ], + 'payments' => [ + 'create', + 'delete', + ], + 'paymentMethods' => [ + 'create', + 'delete', + ], + 'products' => [ + 'create', + 'upsert', + 'delete', + ], + 'productAttributes' => [ + 'create', + 'delete', + ], + 'productAssociationTypes' => [ + 'create', + 'delete', + ], + 'productOptions' => [ + 'create', + 'delete', + ], + 'promotions' => [ + 'create', + 'delete', + ], + 'shipments' => [ + 'create', + 'delete', + ], + 'shippingCategories' => [ + 'create', + 'delete', + ], + 'taxCategories' => [ + 'create', + 'delete', + ], + 'taxRates' => [ + 'create', + 'delete', + ], + 'taxons' => [ + 'create', + 'delete', + ], + 'users' => [ + 'create', + 'delete', + ], + 'zones' => [ + 'create', + 'delete', + ], + ]; + + private static array $endpointsAdmin = [ + // Core Endpoints + 'administrator' => [ + 'create', + 'delete', + 'upsert', + ], + 'avatarImage' => [ + 'create', + 'delete', + ], + 'catalogPromotion' => [ + 'create', + 'upsert', + ], + 'channel' => [ + 'create', + 'delete', + ], + 'country' => [ + 'create', + 'delete', + ], + 'currency' => [ + 'create', + 'delete', + ], + 'customerGroup' => [ + 'create', + 'delete', + 'upsert', + ], + 'exchangeRate' => [ + 'create', + 'delete', + 'upsert', + ], + 'locale' => [ + 'create', + ], + 'order' => [ + 'cancel', + ], + 'payment' => [ + 'complete', + ], + 'product' => [ + 'create', + 'delete', + 'upsert', + ], + 'productAssociationType' => [ + 'create', + 'delete', + 'upsert', + ], + 'productOption' => [ + 'create', + 'delete', + ], + 'productReview' => [ + 'upsert', + 'delete', + 'accept', + 'reject', + ], + 'productVariant' => [ + 'create', + 'upsert', + ], + 'promotion' => [ + 'create', + 'delete', + ], + 'province' => [ + 'upsert', + ], + 'resetPasswordRequest' => [ + 'create', + 'acknowledge', + ], + 'shipment' => [ + 'ship', + ], + 'shippingCategory' => [ + 'create', + 'delete', + 'upsert', + ], + 'shippingMethod' => [ + 'create', + 'delete', + 'upsert', + 'archive', + 'restore', + ], + 'taxCategory' => [ + 'create', + 'delete', + 'upsert', + ], + 'taxon' => [ + 'create', + 'upsert', + ], + 'verifyCustomerAccount' => [ + 'create', + 'acknowledge', + ], + 'zone' => [ + 'create', + 'delete', + 'upsert', + ], + ]; + + private static array $endpointsShop = [ + // Core Endpoints + 'address' => [ + 'create', + 'delete', + 'upsert', + ], + 'customer' => [ + 'create', + 'upsert', + 'changePassword', + ], + 'order' => [ + 'create', + 'upsert', + 'choosePayment', + 'chooseShipment', + 'complete', + ], + 'orderItem' => [ + 'create', + 'delete', + 'changeQuantity', + ], + 'productReview' => [ + 'create', + ], + 'resetPasswordRequest' => [ + 'create', + 'verify', + ], + 'verifyCustomerAccount' => [ + 'create', + 'verify', + ], + ]; + public static function validate(array $item): array + { + $endpoints = match ($item['api_type']) { + 'admin' => self::$endpointsAdmin, + 'shop' => self::$endpointsShop, + 'legacy' => self::$endpointsLegacy + }; + if (!\in_array($item['type'], array_keys($endpoints))) { + throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_keys($endpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR))); + } + if (!\in_array($item['method'], $endpoints[$item['type']])) { + throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR))); + } + return $item; + } +} From 189f06e9f35a95d5a0cd91992feb311c2e580a45 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 27 Oct 2023 11:57:44 +0000 Subject: [PATCH 10/27] [rector] Rector fixes --- src/Configuration/Extractor.php | 4 +--- src/Configuration/Loader.php | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Configuration/Extractor.php b/src/Configuration/Extractor.php index ca29fad..1366d28 100644 --- a/src/Configuration/Extractor.php +++ b/src/Configuration/Extractor.php @@ -24,9 +24,7 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui $builder->getRootNode() ->validate() ->ifArray() - ->then(function (array $item) { - return ExtractorConfigurationValidator::validate($item); - }) + ->then(fn(array $item) => ExtractorConfigurationValidator::validate($item)) ->end() ->children() ->scalarNode('api_type') diff --git a/src/Configuration/Loader.php b/src/Configuration/Loader.php index e613c73..112c6c2 100644 --- a/src/Configuration/Loader.php +++ b/src/Configuration/Loader.php @@ -18,9 +18,7 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui $builder->getRootNode() ->validate() ->ifArray() - ->then(function (array $item) { - return LoaderConfigurationValidator::validate($item); - }) + ->then(fn(array $item) => LoaderConfigurationValidator::validate($item)) ->end() ->children() ->scalarNode('api_type') From 3f412ac3c39fccffe5a9c5234ccef0ab72b8ca76 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Fri, 27 Oct 2023 16:46:21 +0200 Subject: [PATCH 11/27] add enum for api type values --- src/Builder/Client.php | 9 +++++---- src/Validator/ApiType.php | 12 ++++++++++++ src/Validator/ExtractorConfigurationValidator.php | 6 +++--- src/Validator/LoaderConfigurationValidator.php | 6 +++--- 4 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 src/Validator/ApiType.php diff --git a/src/Builder/Client.php b/src/Builder/Client.php index f49566d..288f35e 100644 --- a/src/Builder/Client.php +++ b/src/Builder/Client.php @@ -7,6 +7,7 @@ use Diglin\Sylius\ApiClient\SyliusAdminClientBuilder; use Diglin\Sylius\ApiClient\SyliusShopClientBuilder; use Kiboko\Plugin\Sylius\MissingAuthenticationMethodException; +use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; use PhpParser\Node; @@ -20,7 +21,7 @@ final class Client implements Builder private ?Node\Expr $httpRequestFactory = null; private ?Node\Expr $httpStreamFactory = null; private ?Node\Expr $fileSystem = null; - private string $apiType = ''; + private string $apiType; public const API_ADMIN_KEY = 'admin'; public const API_SHOP_KEY = 'shop'; @@ -135,9 +136,9 @@ public function getNode(): Node\Expr\MethodCall private function getClientBuilderNode(): Node\Expr\MethodCall { $className = match ($this->apiType) { - self::API_ADMIN_KEY => SyliusAdminClientBuilder::class, - self::API_SHOP_KEY => SyliusShopClientBuilder::class, - self::API_LEGACY_KEY => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder' + ApiType::ADMIN->value => SyliusAdminClientBuilder::class, + ApiType::SHOP->value => SyliusShopClientBuilder::class, + ApiType::LEGACY->value => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder', }; return new Node\Expr\MethodCall( diff --git a/src/Validator/ApiType.php b/src/Validator/ApiType.php new file mode 100644 index 0000000..d963847 --- /dev/null +++ b/src/Validator/ApiType.php @@ -0,0 +1,12 @@ +value: $endpoints = self::$endpointsAdmin; $doubleEndpoints = self::$doubleEndpointsAdmin; break; - case 'shop': + case ApiType::SHOP->value: $endpoints = self::$endpointsShop; $doubleEndpoints = self::$doubleEndpointsShop; break; - case 'legacy': + case ApiType::LEGACY->value: $endpoints = self::$endpointsLegacy; $doubleEndpoints = self::$doubleEndpointsLegacy; break; diff --git a/src/Validator/LoaderConfigurationValidator.php b/src/Validator/LoaderConfigurationValidator.php index 2cdecbf..a028276 100644 --- a/src/Validator/LoaderConfigurationValidator.php +++ b/src/Validator/LoaderConfigurationValidator.php @@ -252,9 +252,9 @@ class LoaderConfigurationValidator implements ConfigurationValidatorInterface public static function validate(array $item): array { $endpoints = match ($item['api_type']) { - 'admin' => self::$endpointsAdmin, - 'shop' => self::$endpointsShop, - 'legacy' => self::$endpointsLegacy + ApiType::ADMIN->value => self::$endpointsAdmin, + ApiType::SHOP->value => self::$endpointsShop, + ApiType::LEGACY->value => self::$endpointsLegacy }; if (!\in_array($item['type'], array_keys($endpoints))) { throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_keys($endpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR))); From 771836be2c85a40cfee9ea7ef2e7e177eb21bcd1 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 20 Nov 2023 13:52:46 +0100 Subject: [PATCH 12/27] refacto to use the InvalidConfigurationException on the right node and have a coherent error's message, validate Configuration tests --- src/Configuration/Extractor.php | 25 ++++- src/Configuration/Loader.php | 14 ++- src/Validator/ApiType.php | 10 ++ .../ExtractorConfigurationValidator.php | 94 +++++++++++++------ .../LoaderConfigurationValidator.php | 50 +++++++++- tests/functional/Builder/ClientTest.php | 2 + .../Builder/Extractor/ExtractorTest.php | 2 + .../functional/Builder/Loader/LoaderTest.php | 2 + .../Configuration/ExtractorTest.php | 40 +++++++- tests/functional/Configuration/LoaderTest.php | 44 ++++++++- tests/functional/Factory/ExtractorTest.php | 10 ++ tests/functional/Factory/LoaderTest.php | 5 + 12 files changed, 252 insertions(+), 46 deletions(-) diff --git a/src/Configuration/Extractor.php b/src/Configuration/Extractor.php index 1366d28..e095c4d 100644 --- a/src/Configuration/Extractor.php +++ b/src/Configuration/Extractor.php @@ -4,7 +4,9 @@ namespace Kiboko\Plugin\Sylius\Configuration; +use Kiboko\Plugin\Sylius\Validator\ApiType; use Kiboko\Plugin\Sylius\Validator\ExtractorConfigurationValidator; +use Kiboko\Plugin\Sylius\Validator\LoaderConfigurationValidator; use Symfony\Component\Config; use function Kiboko\Component\SatelliteToolbox\Configuration\asExpression; @@ -13,7 +15,6 @@ final class Extractor implements Config\Definition\ConfigurationInterface { - public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder { $filters = new Search(); @@ -23,19 +24,35 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui /* @phpstan-ignore-next-line */ $builder->getRootNode() ->validate() - ->ifArray() - ->then(fn(array $item) => ExtractorConfigurationValidator::validate($item)) + ->always(fn(array $item) => ExtractorConfigurationValidator::validate($item)) //store the item value ->end() ->children() ->scalarNode('api_type') ->isRequired() ->cannotBeEmpty() + ->validate() + ->always(fn(string $item) => ExtractorConfigurationValidator::validateApiType($item)) //check index of the item value + ->end() ->end() ->scalarNode('type') ->isRequired() + ->cannotBeEmpty() + ->validate() + ->always(fn(string $item) => ExtractorConfigurationValidator::validateType($item)) //check index of the item value + ->end() + ->end() + ->scalarNode('method') + ->isRequired() + ->cannotBeEmpty() + ->validate() + ->always(fn(string $item) => ExtractorConfigurationValidator::validateMethod($item)) //check index of the item value + ->end() ->end() - ->scalarNode('method')->end() ->scalarNode('code') + ->cannotBeEmpty() + ->validate() + ->always(fn(string $item) => ExtractorConfigurationValidator::validateCode($item)) //check index of the item value + ->end() ->validate() ->ifTrue(isExpression()) ->then(asExpression()) diff --git a/src/Configuration/Loader.php b/src/Configuration/Loader.php index 112c6c2..9911868 100644 --- a/src/Configuration/Loader.php +++ b/src/Configuration/Loader.php @@ -4,6 +4,7 @@ namespace Kiboko\Plugin\Sylius\Configuration; +use Kiboko\Plugin\Sylius\Validator\ApiType; use Kiboko\Plugin\Sylius\Validator\LoaderConfigurationValidator; use Symfony\Component\Config; @@ -16,22 +17,27 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui /* @phpstan-ignore-next-line */ $builder->getRootNode() - ->validate() - ->ifArray() - ->then(fn(array $item) => LoaderConfigurationValidator::validate($item)) - ->end() ->children() ->scalarNode('api_type') ->isRequired() ->cannotBeEmpty() + ->validate() + ->always(fn(string $item) => LoaderConfigurationValidator::validateApiType($item)) + ->end() ->end() ->scalarNode('type') ->isRequired() ->cannotBeEmpty() + ->validate() + ->always(fn(string $item) => LoaderConfigurationValidator::validateType($item)) + ->end() ->end() ->scalarNode('method') ->isRequired() ->cannotBeEmpty() + ->validate() + ->always(fn(string $item) => LoaderConfigurationValidator::validateMethod($item)) + ->end() ->end() ->end() ; diff --git a/src/Validator/ApiType.php b/src/Validator/ApiType.php index d963847..dfefb7e 100644 --- a/src/Validator/ApiType.php +++ b/src/Validator/ApiType.php @@ -9,4 +9,14 @@ enum ApiType: string case ADMIN = 'admin'; case SHOP = 'shop'; case LEGACY = 'legacy'; + + public static function casesValue(): array + { + $apiTypeCases = []; + foreach (ApiType::cases() as $cases) + { + $apiTypeCases[] = $cases->value; + } + return $apiTypeCases; + } } diff --git a/src/Validator/ExtractorConfigurationValidator.php b/src/Validator/ExtractorConfigurationValidator.php index 5a391e3..fa8a63a 100644 --- a/src/Validator/ExtractorConfigurationValidator.php +++ b/src/Validator/ExtractorConfigurationValidator.php @@ -2,6 +2,9 @@ namespace Kiboko\Plugin\Sylius\Validator; +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; +use function PHPUnit\Framework\isEmpty; + class ExtractorConfigurationValidator implements ConfigurationValidatorInterface { private static array $endpointsLegacy = [ @@ -456,40 +459,77 @@ class ExtractorConfigurationValidator implements ConfigurationValidatorInterface 'adjustment', 'order', ]; + + private static array $item = []; + + public static function getEndpointsApiType(): array + { + return match (self::$currentApiType) { + ApiType::ADMIN->value => self::$endpointsAdmin, + ApiType::SHOP->value => self::$endpointsShop, + ApiType::LEGACY->value => self::$endpointsLegacy + }; + } + + public static function getDoubleEndpointsApiType(): array + { + return match (self::$currentApiType) { + ApiType::ADMIN->value => self::$doubleEndpointsAdmin, + ApiType::SHOP->value => self::$doubleEndpointsShop, + ApiType::LEGACY->value => self::$doubleEndpointsLegacy + }; + } + public static string $currentApiType; + public static string $currentType; + public static function validate(array $item): array { - switch($item['api_type']) { - case ApiType::ADMIN->value: - $endpoints = self::$endpointsAdmin; - $doubleEndpoints = self::$doubleEndpointsAdmin; - break; - case ApiType::SHOP->value: - $endpoints = self::$endpointsShop; - $doubleEndpoints = self::$doubleEndpointsShop; - break; - case ApiType::LEGACY->value: - $endpoints = self::$endpointsLegacy; - $doubleEndpoints = self::$doubleEndpointsLegacy; - break; - default: - $endpoints = []; - $doubleEndpoints = []; - break; + if(\in_array($item['type'], self::getDoubleEndpointsApiType()) && !\array_key_exists('code', $item)) { + throw new InvalidConfigurationException('The code parameters is required and cannot be empty because you choose a type: ' . $item['type']); } - if (!\in_array($item['type'], array_merge(array_keys($endpoints), $doubleEndpoints))) { - throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_merge(array_keys($endpoints), $doubleEndpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR))); + return $item; + } + + public static function validateApiType(string $apiType) + { + self::$currentApiType = $apiType; + if(!\in_array($apiType, ApiType::casesValue())){ + throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($apiType, \JSON_THROW_ON_ERROR))); } + return $apiType; + } + + public static function validateType(string $type) + { + self::$currentType = $type; + $endpoints = self::getEndpointsApiType(); + $doubleEndpoints = self::getDoubleEndpointsApiType(); + if (!\in_array($type, array_merge(array_keys($endpoints), $doubleEndpoints))) { + throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', array_merge(array_keys($endpoints), $doubleEndpoints)), json_encode($type, \JSON_THROW_ON_ERROR))); + } + return $type; + } + + public static function validateMethod(string $method) + { + $endpoints = self::getEndpointsApiType(); + $doubleEndpoints = self::getDoubleEndpointsApiType(); if ( - \array_key_exists($item['type'], $endpoints) - && !\in_array($item['method'], $endpoints[$item['type']]) - && !\in_array($item['type'], $doubleEndpoints) + \array_key_exists(self::$currentType, $endpoints) + && !\in_array($method, $endpoints[self::$currentType]) + && !\in_array(self::$currentType, $doubleEndpoints) ) { - throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR))); - } - if (\in_array($item['type'], $doubleEndpoints) && !\array_key_exists('code', $item)) { - throw new \InvalidArgumentException(sprintf('The %s type should have a "code" field set.', $item['type'])); + throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', $endpoints[self::$currentType]), json_encode($method, \JSON_THROW_ON_ERROR))); } + return $method; + } - return $item; + public static function validateCode(string $code) + { + $doubleEndpoints = self::getDoubleEndpointsApiType(); + if (\in_array(self::$currentType, $doubleEndpoints)) { + throw new \InvalidArgumentException(sprintf('The %s type should have a "code" field set.', self::$currentType), json_encode($code, \JSON_THROW_ON_ERROR)); + } + return $code; } } diff --git a/src/Validator/LoaderConfigurationValidator.php b/src/Validator/LoaderConfigurationValidator.php index a028276..23f5fc2 100644 --- a/src/Validator/LoaderConfigurationValidator.php +++ b/src/Validator/LoaderConfigurationValidator.php @@ -2,6 +2,8 @@ namespace Kiboko\Plugin\Sylius\Validator; +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; + class LoaderConfigurationValidator implements ConfigurationValidatorInterface { private static array $endpointsLegacy = [ @@ -249,19 +251,59 @@ class LoaderConfigurationValidator implements ConfigurationValidatorInterface 'verify', ], ]; - public static function validate(array $item): array + + + public static string $currentApiType; + public static string $currentType; + + public static function getEndpointsApiType(): array { - $endpoints = match ($item['api_type']) { + return match (self::$currentApiType) { ApiType::ADMIN->value => self::$endpointsAdmin, ApiType::SHOP->value => self::$endpointsShop, ApiType::LEGACY->value => self::$endpointsLegacy }; + } + public static function validate(array $item): array + { + $endpoints = self::getEndpointsApiType(); if (!\in_array($item['type'], array_keys($endpoints))) { - throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_keys($endpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR))); + throw new \InvalidArgumentException(sprintf('the value "type" should be one of [%s], got %s', implode(', ', array_keys($endpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR))); } if (!\in_array($item['method'], $endpoints[$item['type']])) { - throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR))); + throw new \InvalidArgumentException(sprintf('the value "method" should be one of [%s], got %s', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR))); } return $item; } + + public static function validateApiType(string $apiType) + { + self::$currentApiType = $apiType; + if(!\in_array($apiType, ApiType::casesValue())){ + throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($apiType, \JSON_THROW_ON_ERROR))); + } + return $apiType; + } + + public static function validateType(string $type) + { + self::$currentType = $type; + $endpoints = self::getEndpointsApiType(); + if (!\in_array($type, array_keys($endpoints))) { + throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', array_keys($endpoints)), json_encode($type, \JSON_THROW_ON_ERROR))); + } + return $type; + } + + public static function validateMethod(string $method) + { + $endpoints = self::getEndpointsApiType(); + if ( + \array_key_exists(self::$currentType, $endpoints) + && !\in_array($method, $endpoints[self::$currentType]) + ) { + throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', $endpoints[self::$currentType]), json_encode($method, \JSON_THROW_ON_ERROR))); + } + return $method; + } } diff --git a/tests/functional/Builder/ClientTest.php b/tests/functional/Builder/ClientTest.php index 8e693a1..38f9074 100644 --- a/tests/functional/Builder/ClientTest.php +++ b/tests/functional/Builder/ClientTest.php @@ -41,6 +41,8 @@ public function testExpectingTokenOrPassword(): void $this->expectException(MissingAuthenticationMethodException::class); $this->expectExceptionMessage('Please check your client builder, you should either call withToken() or withPassword() methods.'); + $client->withApiType('legacy'); + $client->getNode(); } } diff --git a/tests/functional/Builder/Extractor/ExtractorTest.php b/tests/functional/Builder/Extractor/ExtractorTest.php index d6cdd46..8f60e49 100644 --- a/tests/functional/Builder/Extractor/ExtractorTest.php +++ b/tests/functional/Builder/Extractor/ExtractorTest.php @@ -46,6 +46,7 @@ public function testAllProducts(): void $builder = new Extractor($capacity); $builder->withClient($client->getNode()); + $builder->withApiType('legacy'); $this->assertBuildsExtractorExtractsExactly( [ @@ -189,6 +190,7 @@ public function testAllProductsWithSearch(): void $builder = new Extractor($capacity); $builder->withClient($client->getNode()); + $builder->withApiType('legacy'); $this->assertBuildsExtractorExtractsExactly( [ diff --git a/tests/functional/Builder/Loader/LoaderTest.php b/tests/functional/Builder/Loader/LoaderTest.php index b7d9ac7..799ba08 100644 --- a/tests/functional/Builder/Loader/LoaderTest.php +++ b/tests/functional/Builder/Loader/LoaderTest.php @@ -47,6 +47,7 @@ public function testUpsertProduct(): void $builder = new Loader($capacity); $builder->withClient($client->getNode()); + $builder->withApiType('legacy'); $this->assertBuildsLoaderLoadsExactly( [ @@ -95,6 +96,7 @@ public function testCreateProduct(): void $builder = new Loader($capacity); $builder->withClient($client->getNode()); + $builder->withApiType('legacy'); $this->assertBuildsLoaderLoadsExactly( [ diff --git a/tests/functional/Configuration/ExtractorTest.php b/tests/functional/Configuration/ExtractorTest.php index 3427e3c..f682f33 100644 --- a/tests/functional/Configuration/ExtractorTest.php +++ b/tests/functional/Configuration/ExtractorTest.php @@ -23,11 +23,13 @@ public static function validDataProvider(): iterable 'config' => [ 'type' => 'products', 'method' => 'all', + 'api_type' => 'legacy', 'search' => [], ], 'expected' => [ 'type' => 'products', 'method' => 'all', + 'api_type' => 'legacy', 'search' => [], ], ]; @@ -35,11 +37,13 @@ public static function validDataProvider(): iterable 'config' => [ 'type' => 'products', 'method' => 'listPerPage', + 'api_type' => 'legacy', 'search' => [], ], 'expected' => [ 'type' => 'products', 'method' => 'listPerPage', + 'api_type' => 'legacy', 'search' => [], ], ]; @@ -47,11 +51,13 @@ public static function validDataProvider(): iterable 'config' => [ 'type' => 'products', 'method' => 'get', + 'api_type' => 'legacy', 'search' => [], ], 'expected' => [ 'type' => 'products', 'method' => 'get', + 'api_type' => 'legacy', 'search' => [], ], ]; @@ -65,7 +71,7 @@ public function testValidConfig(array $config, array $expected): void $this->assertSame($expected, $this->processor->processConfiguration($client, [$config])); } - public function testWrongMethod(): void + public function testWrongApiType(): void { $client = new Configuration\Extractor(); @@ -73,13 +79,14 @@ public function testWrongMethod(): void Config\Definition\Exception\InvalidConfigurationException::class, ); $this->expectExceptionMessage( - 'Invalid configuration for path "extractor": The value should be one of [listPerPage, all, get], got "invalidValue".', + 'Invalid configuration for path "extractor.api_type": the value should be one of [admin, shop, legacy], got "invalidValue".', ); $this->processor->processConfiguration($client, [ [ + 'api_type' => 'invalidValue', 'type' => 'products', - 'method' => 'invalidValue', + 'method' => 'all' ], ]); } @@ -92,12 +99,34 @@ public function testWrongType(): void Config\Definition\Exception\InvalidConfigurationException::class, ); $this->expectExceptionMessage( - 'Invalid configuration for path "extractor.type": the value should be one of [channels, countries, carts, currencies, customers, exchangeRates, locales, orders, payments, paymentMethods, products, productAttributes, productAssociationTypes, productOptions, promotions, shipments, shippingCategories, taxCategories, taxRates, taxons, users, zones, productReviews, productVariants, promotionCoupons], got "wrong"', + 'Invalid configuration for path "extractor.type": the value should be one of [channels, countries, carts, currencies, customers, exchangeRates, locales, orders, payments, paymentMethods, products, productAttributes, productAssociationTypes, productOptions, promotions, shipments, shippingCategories, taxCategories, taxRates, taxons, users, zones, productReviews, productVariants, promotionCoupons], got "wrong".', ); $this->processor->processConfiguration($client, [ [ 'type' => 'wrong', + 'api_type' => 'legacy', + 'method' => 'all' + ], + ]); + } + + public function testWrongMethod(): void + { + $client = new Configuration\Extractor(); + + $this->expectException( + Config\Definition\Exception\InvalidConfigurationException::class, + ); + $this->expectExceptionMessage( + 'Invalid configuration for path "extractor.method": The value should be one of [listPerPage, all, get], got "invalidValue".', + ); + + $this->processor->processConfiguration($client, [ + [ + 'type' => 'products', + 'method' => 'invalidValue', + 'api_type' => 'legacy', ], ]); } @@ -110,13 +139,14 @@ public function testMissingCode(): void Config\Definition\Exception\InvalidConfigurationException::class, ); $this->expectExceptionMessage( - 'The productReviews type should have a "code" field set.', + 'The code parameters is required and cannot be empty because you choose a type: productReviews', ); $this->processor->processConfiguration($client, [ [ 'type' => 'productReviews', 'method' => 'get', + 'api_type' => 'legacy', ], ]); } diff --git a/tests/functional/Configuration/LoaderTest.php b/tests/functional/Configuration/LoaderTest.php index 7ee2598..b89c5c7 100644 --- a/tests/functional/Configuration/LoaderTest.php +++ b/tests/functional/Configuration/LoaderTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace functional\Configuration; +namespace functional\Kiboko\Plugin\Sylius\Configuration; use Kiboko\Plugin\Sylius\Configuration; use PHPUnit\Framework\TestCase; @@ -16,6 +16,45 @@ protected function setUp(): void { $this->processor = new Config\Definition\Processor(); } + public function testWrongApiType(): void + { + $client = new Configuration\Extractor(); + + $this->expectException( + Config\Definition\Exception\InvalidConfigurationException::class, + ); + $this->expectExceptionMessage( + 'Invalid configuration for path "extractor.api_type": the value should be one of [admin, shop, legacy], got "invalidValue".', + ); + + $this->processor->processConfiguration($client, [ + [ + 'api_type' => 'invalidValue', + 'type' => 'products', + 'method' => 'all' + ], + ]); + } + + public function testWrongType(): void + { + $client = new Configuration\Extractor(); + + $this->expectException( + Config\Definition\Exception\InvalidConfigurationException::class, + ); + $this->expectExceptionMessage( + 'Invalid configuration for path "extractor.type": the value should be one of [channels, countries, carts, currencies, customers, exchangeRates, locales, orders, payments, paymentMethods, products, productAttributes, productAssociationTypes, productOptions, promotions, shipments, shippingCategories, taxCategories, taxRates, taxons, users, zones, productReviews, productVariants, promotionCoupons], got "wrong".', + ); + + $this->processor->processConfiguration($client, [ + [ + 'type' => 'wrong', + 'api_type' => 'legacy', + 'method' => 'all' + ], + ]); + } public function testWrongMethod(): void { @@ -25,13 +64,14 @@ public function testWrongMethod(): void Config\Definition\Exception\InvalidConfigurationException::class, ); $this->expectExceptionMessage( - 'Invalid configuration for path "loader": the value should be one of [create, upsert, delete], got "invalidValue"', + 'Invalid configuration for path "loader.method": The value should be one of [create, upsert, delete], got "invalidValue".', ); $this->processor->processConfiguration($client, [ [ 'type' => 'products', 'method' => 'invalidValue', + 'api_type' => 'legacy', ], ]); } diff --git a/tests/functional/Factory/ExtractorTest.php b/tests/functional/Factory/ExtractorTest.php index c3c0de4..34bbca1 100644 --- a/tests/functional/Factory/ExtractorTest.php +++ b/tests/functional/Factory/ExtractorTest.php @@ -17,6 +17,7 @@ public static function validDataProvider(): \Generator [ 'type' => 'products', 'method' => 'all', + 'api_type' => 'legacy', ], ]; @@ -24,6 +25,7 @@ public static function validDataProvider(): \Generator [ 'type' => 'products', 'method' => 'listPerPage', + 'api_type' => 'legacy', ], ]; } @@ -43,12 +45,20 @@ public static function wrongConfigs(): \Generator 'config' => [ 'type' => 'wrong', 'method' => 'all', + 'api_type' => 'legacy', ], ]; yield [ 'config' => [ 'type' => 'products', 'method' => 'wrong', + 'api_type' => 'legacy', + ], + ]; + yield [ + 'config' => [ + 'type' => 'products', + 'api_type' => 'legacy', ], ]; yield [ diff --git a/tests/functional/Factory/LoaderTest.php b/tests/functional/Factory/LoaderTest.php index cf460aa..d95436f 100644 --- a/tests/functional/Factory/LoaderTest.php +++ b/tests/functional/Factory/LoaderTest.php @@ -16,6 +16,7 @@ public static function validDataProvider(): \Generator [ 'type' => 'products', 'method' => 'create', + 'api_type' => 'legacy', ], ]; @@ -23,6 +24,7 @@ public static function validDataProvider(): \Generator [ 'type' => 'products', 'method' => 'upsert', + 'api_type' => 'legacy', ], ]; } @@ -42,17 +44,20 @@ public static function wrongConfigs(): \Generator 'config' => [ 'type' => 'wrong', 'method' => 'all', + 'api_type' => 'legacy', ], ]; yield [ 'config' => [ 'type' => 'products', 'method' => 'wrong', + 'api_type' => 'legacy', ], ]; yield [ 'config' => [ 'type' => 'products', + 'api_type' => 'legacy', ], ]; } From 29e1b3faf0e1725e678a957874d71cbcc7012289 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 20 Nov 2023 14:12:34 +0100 Subject: [PATCH 13/27] fix tests, add a check on api_type parameter --- src/Capacity/All.php | 3 +++ src/Capacity/Create.php | 3 +++ src/Capacity/ListPerPage.php | 3 +++ src/Capacity/Upsert.php | 3 +++ tests/functional/ServiceTest.php | 5 +++++ 5 files changed, 17 insertions(+) diff --git a/src/Capacity/All.php b/src/Capacity/All.php index 2d9cc34..fec661c 100644 --- a/src/Capacity/All.php +++ b/src/Capacity/All.php @@ -121,6 +121,9 @@ public function __construct(private readonly ExpressionLanguage $interpreter) public function applies(array $config): bool { + if (!isset($config['api_type'])) { + return false; + } switch($config['api_type']) { case 'admin': $endpoints = self::$endpointsAdmin; diff --git a/src/Capacity/Create.php b/src/Capacity/Create.php index cc4d452..d65f5a6 100644 --- a/src/Capacity/Create.php +++ b/src/Capacity/Create.php @@ -78,6 +78,9 @@ final class Create implements CapacityInterface public function applies(array $config): bool { + if (!isset($config['api_type'])) { + return false; + } $endpoints = match($config['api_type']) { 'admin' => self::$endpointsAdmin, 'shop' =>self::$endpointsShop, diff --git a/src/Capacity/ListPerPage.php b/src/Capacity/ListPerPage.php index b78d9b9..fd108d5 100644 --- a/src/Capacity/ListPerPage.php +++ b/src/Capacity/ListPerPage.php @@ -120,6 +120,9 @@ public function __construct(private readonly ExpressionLanguage $interpreter) public function applies(array $config): bool { + if (!isset($config['api_type'])) { + return false; + } switch($config['api_type']) { case 'admin': $endpoints = self::$endpointsAdmin; diff --git a/src/Capacity/Upsert.php b/src/Capacity/Upsert.php index 92eb190..06d1a3e 100644 --- a/src/Capacity/Upsert.php +++ b/src/Capacity/Upsert.php @@ -70,6 +70,9 @@ final class Upsert implements CapacityInterface public function applies(array $config): bool { + if (!isset($config['api_type'])) { + return false; + } $endpoints = match($config['api_type']) { 'admin' => self::$endpointsAdmin, 'shop' =>self::$endpointsShop, diff --git a/tests/functional/ServiceTest.php b/tests/functional/ServiceTest.php index 4f09f9f..1b24106 100644 --- a/tests/functional/ServiceTest.php +++ b/tests/functional/ServiceTest.php @@ -20,6 +20,7 @@ public static function validDataProvider(): \Generator 'extractor' => [ 'type' => 'products', 'method' => 'all', + 'api_type' => 'legacy', 'search' => [], ], 'client' => [ @@ -34,6 +35,7 @@ public static function validDataProvider(): \Generator 'extractor' => [ 'type' => 'products', 'method' => 'all', + 'api_type' => 'legacy', ], 'client' => [ 'api_url' => '1234', @@ -52,6 +54,7 @@ public static function validDataProvider(): \Generator 'loader' => [ 'type' => 'products', 'method' => 'upsert', + 'api_type' => 'legacy', ], 'client' => [ 'api_url' => '1234', @@ -66,6 +69,7 @@ public static function validDataProvider(): \Generator 'loader' => [ 'type' => 'products', 'method' => 'upsert', + 'api_type' => 'legacy', ], 'client' => [ 'api_url' => '1234', @@ -113,6 +117,7 @@ public function testMissingAuthentication(): void 'loader' => [ 'type' => 'products', 'method' => 'upsert', + 'api_type' => 'legacy', 'code' => 'azerty123', ], 'client' => [ From b39d26272353df43841bd6dd430a18092b21dde3 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 20 Nov 2023 14:14:58 +0100 Subject: [PATCH 14/27] cs-fixer --- src/Builder/Capacity/All.php | 4 +--- src/Builder/Capacity/Create.php | 4 +--- src/Builder/Capacity/ListPerPage.php | 4 +--- src/Builder/Capacity/Upsert.php | 4 +--- src/Builder/Client.php | 4 +--- src/Builder/Extractor.php | 6 ++---- src/Builder/Loader.php | 6 ++---- src/Capacity/All.php | 7 +++---- src/Capacity/Create.php | 5 +++-- src/Capacity/ListPerPage.php | 7 +++---- src/Capacity/Upsert.php | 5 +++-- src/Configuration.php | 2 -- src/Configuration/Extractor.php | 13 +++++-------- src/Configuration/Loader.php | 8 +++----- src/Factory/Client.php | 4 ++-- src/Factory/Extractor.php | 2 +- src/Factory/Loader.php | 2 +- src/Factory/NoApplicableCapacityException.php | 4 +--- src/Factory/Repository/RepositoryTrait.php | 2 +- src/Factory/Search.php | 4 ++-- src/MissingAuthenticationMethodException.php | 4 +--- src/MissingEndpointException.php | 4 +--- src/MissingParameterException.php | 4 +--- src/Service.php | 6 +++--- src/Validator/ApiType.php | 4 ++-- .../ConfigurationValidatorInterface.php | 2 ++ .../ExtractorConfigurationValidator.php | 16 +++++++++++----- src/Validator/LoaderConfigurationValidator.php | 12 ++++++++---- 28 files changed, 66 insertions(+), 83 deletions(-) diff --git a/src/Builder/Capacity/All.php b/src/Builder/Capacity/All.php index 471066a..8750483 100644 --- a/src/Builder/Capacity/All.php +++ b/src/Builder/Capacity/All.php @@ -14,9 +14,7 @@ final class All implements Builder private null|Node\Expr $search = null; private null|Node\Expr $code = null; - public function __construct() - { - } + public function __construct() {} public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self { diff --git a/src/Builder/Capacity/Create.php b/src/Builder/Capacity/Create.php index 61eaefc..283bf52 100644 --- a/src/Builder/Capacity/Create.php +++ b/src/Builder/Capacity/Create.php @@ -15,9 +15,7 @@ final class Create implements Builder private null|Node\Expr $code = null; private null|Node\Expr $data = null; - public function __construct() - { - } + public function __construct() {} public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self { diff --git a/src/Builder/Capacity/ListPerPage.php b/src/Builder/Capacity/ListPerPage.php index 68484e6..1e1c450 100644 --- a/src/Builder/Capacity/ListPerPage.php +++ b/src/Builder/Capacity/ListPerPage.php @@ -14,9 +14,7 @@ final class ListPerPage implements Builder private null|Node\Expr $search = null; private null|Node\Expr $code = null; - public function __construct() - { - } + public function __construct() {} public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self { diff --git a/src/Builder/Capacity/Upsert.php b/src/Builder/Capacity/Upsert.php index cfecc63..c443862 100644 --- a/src/Builder/Capacity/Upsert.php +++ b/src/Builder/Capacity/Upsert.php @@ -15,9 +15,7 @@ final class Upsert implements Builder private null|Node\Expr $code = null; private null|Node\Expr $data = null; - public function __construct() - { - } + public function __construct() {} public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self { diff --git a/src/Builder/Client.php b/src/Builder/Client.php index 288f35e..bf2c2cc 100644 --- a/src/Builder/Client.php +++ b/src/Builder/Client.php @@ -27,9 +27,7 @@ final class Client implements Builder 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) - { - } + public function __construct(private readonly Node\Expr $baseUrl, private readonly Node\Expr $clientId, private readonly Node\Expr $secret) {} public function withToken(Node\Expr $token, Node\Expr $refreshToken): self { diff --git a/src/Builder/Extractor.php b/src/Builder/Extractor.php index d82fa0a..4440a06 100644 --- a/src/Builder/Extractor.php +++ b/src/Builder/Extractor.php @@ -14,9 +14,7 @@ final class Extractor implements StepBuilderInterface private ?Node\Expr $client = null; private string $apiType; - public function __construct(private readonly Builder $capacity) - { - } + public function __construct(private readonly Builder $capacity) {} public function withClient(Node\Expr $client): self { @@ -133,12 +131,12 @@ class: new Node\Stmt\Class_( public function getParamsNode(): array { - $className = match ($this->apiType) { 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'), diff --git a/src/Builder/Loader.php b/src/Builder/Loader.php index e8127e5..bd90150 100644 --- a/src/Builder/Loader.php +++ b/src/Builder/Loader.php @@ -14,9 +14,7 @@ final class Loader implements StepBuilderInterface private ?Node\Expr $client = null; private string $apiType; - public function __construct(private readonly Builder $capacity) - { - } + public function __construct(private readonly Builder $capacity) {} public function withClient(Node\Expr $client): self { @@ -139,12 +137,12 @@ class: new Node\Stmt\Class_( public function getParamsNode(): array { - $className = match ($this->apiType) { 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'), diff --git a/src/Capacity/All.php b/src/Capacity/All.php index fec661c..b8d7d19 100644 --- a/src/Capacity/All.php +++ b/src/Capacity/All.php @@ -115,16 +115,14 @@ final class All implements CapacityInterface 'order', ]; - public function __construct(private readonly ExpressionLanguage $interpreter) - { - } + public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { if (!isset($config['api_type'])) { return false; } - switch($config['api_type']) { + switch ($config['api_type']) { case 'admin': $endpoints = self::$endpointsAdmin; $doubleEndpoints = self::$doubleEndpointsAdmin; @@ -142,6 +140,7 @@ public function applies(array $config): bool $doubleEndpoints = []; break; } + return isset($config['type']) && (\in_array($config['type'], $endpoints) || \in_array($config['type'], $doubleEndpoints)) && isset($config['method']) diff --git a/src/Capacity/Create.php b/src/Capacity/Create.php index d65f5a6..17e0711 100644 --- a/src/Capacity/Create.php +++ b/src/Capacity/Create.php @@ -81,11 +81,12 @@ public function applies(array $config): bool if (!isset($config['api_type'])) { return false; } - $endpoints = match($config['api_type']) { + $endpoints = match ($config['api_type']) { 'admin' => self::$endpointsAdmin, - 'shop' =>self::$endpointsShop, + 'shop' => self::$endpointsShop, 'legacy' => self::$endpointsLegacy, }; + return isset($config['type']) && \in_array($config['type'], $endpoints) && isset($config['method']) diff --git a/src/Capacity/ListPerPage.php b/src/Capacity/ListPerPage.php index fd108d5..a91614d 100644 --- a/src/Capacity/ListPerPage.php +++ b/src/Capacity/ListPerPage.php @@ -114,16 +114,14 @@ final class ListPerPage implements CapacityInterface 'order', ]; - public function __construct(private readonly ExpressionLanguage $interpreter) - { - } + public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { if (!isset($config['api_type'])) { return false; } - switch($config['api_type']) { + switch ($config['api_type']) { case 'admin': $endpoints = self::$endpointsAdmin; $doubleEndpoints = self::$doubleEndpointsAdmin; @@ -141,6 +139,7 @@ public function applies(array $config): bool $doubleEndpoints = []; break; } + return isset($config['type']) && (\in_array($config['type'], $endpoints) || \in_array($config['type'], $doubleEndpoints)) && isset($config['method']) diff --git a/src/Capacity/Upsert.php b/src/Capacity/Upsert.php index 06d1a3e..53b5d58 100644 --- a/src/Capacity/Upsert.php +++ b/src/Capacity/Upsert.php @@ -73,11 +73,12 @@ public function applies(array $config): bool if (!isset($config['api_type'])) { return false; } - $endpoints = match($config['api_type']) { + $endpoints = match ($config['api_type']) { 'admin' => self::$endpointsAdmin, - 'shop' =>self::$endpointsShop, + 'shop' => self::$endpointsShop, 'legacy' => self::$endpointsLegacy, }; + return isset($config['type']) && \in_array($config['type'], $endpoints) && isset($config['method']) diff --git a/src/Configuration.php b/src/Configuration.php index 0cd33f9..a0b0315 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -6,8 +6,6 @@ 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 { diff --git a/src/Configuration/Extractor.php b/src/Configuration/Extractor.php index e095c4d..05b9b77 100644 --- a/src/Configuration/Extractor.php +++ b/src/Configuration/Extractor.php @@ -4,9 +4,7 @@ namespace Kiboko\Plugin\Sylius\Configuration; -use Kiboko\Plugin\Sylius\Validator\ApiType; use Kiboko\Plugin\Sylius\Validator\ExtractorConfigurationValidator; -use Kiboko\Plugin\Sylius\Validator\LoaderConfigurationValidator; use Symfony\Component\Config; use function Kiboko\Component\SatelliteToolbox\Configuration\asExpression; @@ -14,7 +12,6 @@ final class Extractor implements Config\Definition\ConfigurationInterface { - public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder { $filters = new Search(); @@ -24,34 +21,34 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui /* @phpstan-ignore-next-line */ $builder->getRootNode() ->validate() - ->always(fn(array $item) => ExtractorConfigurationValidator::validate($item)) //store the item value + ->always(fn (array $item) => ExtractorConfigurationValidator::validate($item)) // store the item value ->end() ->children() ->scalarNode('api_type') ->isRequired() ->cannotBeEmpty() ->validate() - ->always(fn(string $item) => ExtractorConfigurationValidator::validateApiType($item)) //check index of the item value + ->always(fn (string $item) => ExtractorConfigurationValidator::validateApiType($item)) // check index of the item value ->end() ->end() ->scalarNode('type') ->isRequired() ->cannotBeEmpty() ->validate() - ->always(fn(string $item) => ExtractorConfigurationValidator::validateType($item)) //check index of the item value + ->always(fn (string $item) => ExtractorConfigurationValidator::validateType($item)) // check index of the item value ->end() ->end() ->scalarNode('method') ->isRequired() ->cannotBeEmpty() ->validate() - ->always(fn(string $item) => ExtractorConfigurationValidator::validateMethod($item)) //check index of the item value + ->always(fn (string $item) => ExtractorConfigurationValidator::validateMethod($item)) // check index of the item value ->end() ->end() ->scalarNode('code') ->cannotBeEmpty() ->validate() - ->always(fn(string $item) => ExtractorConfigurationValidator::validateCode($item)) //check index of the item value + ->always(fn (string $item) => ExtractorConfigurationValidator::validateCode($item)) // check index of the item value ->end() ->validate() ->ifTrue(isExpression()) diff --git a/src/Configuration/Loader.php b/src/Configuration/Loader.php index 9911868..4d22faa 100644 --- a/src/Configuration/Loader.php +++ b/src/Configuration/Loader.php @@ -4,13 +4,11 @@ namespace Kiboko\Plugin\Sylius\Configuration; -use Kiboko\Plugin\Sylius\Validator\ApiType; use Kiboko\Plugin\Sylius\Validator\LoaderConfigurationValidator; use Symfony\Component\Config; final class Loader implements Config\Definition\ConfigurationInterface { - public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder { $builder = new Config\Definition\Builder\TreeBuilder('loader'); @@ -22,21 +20,21 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui ->isRequired() ->cannotBeEmpty() ->validate() - ->always(fn(string $item) => LoaderConfigurationValidator::validateApiType($item)) + ->always(fn (string $item) => LoaderConfigurationValidator::validateApiType($item)) ->end() ->end() ->scalarNode('type') ->isRequired() ->cannotBeEmpty() ->validate() - ->always(fn(string $item) => LoaderConfigurationValidator::validateType($item)) + ->always(fn (string $item) => LoaderConfigurationValidator::validateType($item)) ->end() ->end() ->scalarNode('method') ->isRequired() ->cannotBeEmpty() ->validate() - ->always(fn(string $item) => LoaderConfigurationValidator::validateMethod($item)) + ->always(fn (string $item) => LoaderConfigurationValidator::validateMethod($item)) ->end() ->end() ->end() diff --git a/src/Factory/Client.php b/src/Factory/Client.php index a258147..eccf9d8 100644 --- a/src/Factory/Client.php +++ b/src/Factory/Client.php @@ -37,7 +37,7 @@ public function normalize(array $config): array { try { return $this->processor->processConfiguration($this->configuration, $config); - } catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException $exception) { + } catch (Symfony\InvalidConfigurationException|Symfony\InvalidTypeException $exception) { throw new Configurator\InvalidConfigurationException($exception->getMessage(), 0, $exception); } } @@ -110,7 +110,7 @@ public function compile(array $config): Repository\Client return new Repository\Client($clientBuilder); } catch (Sylius\MissingAuthenticationMethodException $exception) { throw new Configurator\InvalidConfigurationException(message: 'Your Sylius API configuration is missing an authentication method, you should either define "username" or "token" options.', previous: $exception); - } catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException $exception) { + } catch (Symfony\InvalidConfigurationException|Symfony\InvalidTypeException $exception) { throw new Configurator\InvalidConfigurationException(message: $exception->getMessage(), previous: $exception); } } diff --git a/src/Factory/Extractor.php b/src/Factory/Extractor.php index cbd3980..5551259 100644 --- a/src/Factory/Extractor.php +++ b/src/Factory/Extractor.php @@ -40,7 +40,7 @@ public function normalize(array $config): array { try { return $this->processor->processConfiguration($this->configuration, $config); - } catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException $exception) { + } catch (Symfony\InvalidConfigurationException|Symfony\InvalidTypeException $exception) { throw new Configurator\InvalidConfigurationException($exception->getMessage(), 0, $exception); } } diff --git a/src/Factory/Loader.php b/src/Factory/Loader.php index e75a917..e69e6e5 100644 --- a/src/Factory/Loader.php +++ b/src/Factory/Loader.php @@ -39,7 +39,7 @@ public function normalize(array $config): array { try { return $this->processor->processConfiguration($this->configuration, $config); - } catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException $exception) { + } catch (Symfony\InvalidConfigurationException|Symfony\InvalidTypeException $exception) { throw new Configurator\InvalidConfigurationException($exception->getMessage(), 0, $exception); } } diff --git a/src/Factory/NoApplicableCapacityException.php b/src/Factory/NoApplicableCapacityException.php index 1ab18bc..2922447 100644 --- a/src/Factory/NoApplicableCapacityException.php +++ b/src/Factory/NoApplicableCapacityException.php @@ -4,6 +4,4 @@ namespace Kiboko\Plugin\Sylius\Factory; -final class NoApplicableCapacityException extends \OutOfRangeException -{ -} +final class NoApplicableCapacityException extends \OutOfRangeException {} diff --git a/src/Factory/Repository/RepositoryTrait.php b/src/Factory/Repository/RepositoryTrait.php index 5544a6d..e292132 100644 --- a/src/Factory/Repository/RepositoryTrait.php +++ b/src/Factory/Repository/RepositoryTrait.php @@ -15,7 +15,7 @@ trait RepositoryTrait /** @var string[] */ private array $packages; - public function addFiles(FileInterface|DirectoryInterface ...$files): Configurator\RepositoryInterface + public function addFiles(DirectoryInterface|FileInterface ...$files): Configurator\RepositoryInterface { array_push($this->files, ...$files); diff --git a/src/Factory/Search.php b/src/Factory/Search.php index 331ce62..3657333 100644 --- a/src/Factory/Search.php +++ b/src/Factory/Search.php @@ -33,7 +33,7 @@ public function normalize(array $config): array { try { return $this->processor->processConfiguration($this->configuration, $config); - } catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException $exception) { + } catch (Symfony\InvalidConfigurationException|Symfony\InvalidTypeException $exception) { throw new Configurator\InvalidConfigurationException($exception->getMessage(), 0, $exception); } } @@ -59,7 +59,7 @@ public function compile(array $config): Repository\Search } return new Repository\Search($builder); - } catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException $exception) { + } catch (Symfony\InvalidConfigurationException|Symfony\InvalidTypeException $exception) { throw new Configurator\InvalidConfigurationException(message: $exception->getMessage(), previous: $exception); } } diff --git a/src/MissingAuthenticationMethodException.php b/src/MissingAuthenticationMethodException.php index 577827b..8389a55 100644 --- a/src/MissingAuthenticationMethodException.php +++ b/src/MissingAuthenticationMethodException.php @@ -4,6 +4,4 @@ namespace Kiboko\Plugin\Sylius; -final class MissingAuthenticationMethodException extends \RuntimeException -{ -} +final class MissingAuthenticationMethodException extends \RuntimeException {} diff --git a/src/MissingEndpointException.php b/src/MissingEndpointException.php index ea86064..847c93b 100644 --- a/src/MissingEndpointException.php +++ b/src/MissingEndpointException.php @@ -4,6 +4,4 @@ namespace Kiboko\Plugin\Sylius; -final class MissingEndpointException extends \RuntimeException -{ -} +final class MissingEndpointException extends \RuntimeException {} diff --git a/src/MissingParameterException.php b/src/MissingParameterException.php index 2cb38d5..5119d4b 100644 --- a/src/MissingParameterException.php +++ b/src/MissingParameterException.php @@ -4,6 +4,4 @@ namespace Kiboko\Plugin\Sylius; -final class MissingParameterException extends \UnexpectedValueException -{ -} +final class MissingParameterException extends \UnexpectedValueException {} diff --git a/src/Service.php b/src/Service.php index 7124f19..d8d5aa8 100644 --- a/src/Service.php +++ b/src/Service.php @@ -50,7 +50,7 @@ public function normalize(array $config): array { try { return $this->processor->processConfiguration($this->configuration, $config); - } catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException $exception) { + } catch (Symfony\InvalidConfigurationException|Symfony\InvalidTypeException $exception) { throw new InvalidConfigurationException($exception->getMessage(), 0, $exception); } } @@ -61,7 +61,7 @@ public function validate(array $config): bool $this->processor->processConfiguration($this->configuration, $config); return true; - } catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException) { + } catch (Symfony\InvalidConfigurationException|Symfony\InvalidTypeException) { return false; } } @@ -118,7 +118,7 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep throw new InvalidConfigurationException('Could not determine if the factory should build an extractor or a loader.'); } catch (MissingAuthenticationMethodException $exception) { throw new InvalidConfigurationException('Your Sylius API configuration is missing an authentication method, you should either define "username" or "token" options.', 0, $exception); - } catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException $exception) { + } catch (Symfony\InvalidConfigurationException|Symfony\InvalidTypeException $exception) { throw new InvalidConfigurationException($exception->getMessage(), 0, $exception); } } diff --git a/src/Validator/ApiType.php b/src/Validator/ApiType.php index dfefb7e..c0304eb 100644 --- a/src/Validator/ApiType.php +++ b/src/Validator/ApiType.php @@ -13,10 +13,10 @@ enum ApiType: string public static function casesValue(): array { $apiTypeCases = []; - foreach (ApiType::cases() as $cases) - { + foreach (ApiType::cases() as $cases) { $apiTypeCases[] = $cases->value; } + return $apiTypeCases; } } diff --git a/src/Validator/ConfigurationValidatorInterface.php b/src/Validator/ConfigurationValidatorInterface.php index 03134bd..13aa474 100644 --- a/src/Validator/ConfigurationValidatorInterface.php +++ b/src/Validator/ConfigurationValidatorInterface.php @@ -1,5 +1,7 @@ [ 'get', @@ -484,18 +485,20 @@ public static function getDoubleEndpointsApiType(): array public static function validate(array $item): array { - if(\in_array($item['type'], self::getDoubleEndpointsApiType()) && !\array_key_exists('code', $item)) { - throw new InvalidConfigurationException('The code parameters is required and cannot be empty because you choose a type: ' . $item['type']); + if (\in_array($item['type'], self::getDoubleEndpointsApiType()) && !\array_key_exists('code', $item)) { + throw new InvalidConfigurationException('The code parameters is required and cannot be empty because you choose a type: '.$item['type']); } + return $item; } public static function validateApiType(string $apiType) { self::$currentApiType = $apiType; - if(!\in_array($apiType, ApiType::casesValue())){ + if (!\in_array($apiType, ApiType::casesValue())) { throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($apiType, \JSON_THROW_ON_ERROR))); } + return $apiType; } @@ -507,6 +510,7 @@ public static function validateType(string $type) if (!\in_array($type, array_merge(array_keys($endpoints), $doubleEndpoints))) { throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', array_merge(array_keys($endpoints), $doubleEndpoints)), json_encode($type, \JSON_THROW_ON_ERROR))); } + return $type; } @@ -521,6 +525,7 @@ public static function validateMethod(string $method) ) { throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', $endpoints[self::$currentType]), json_encode($method, \JSON_THROW_ON_ERROR))); } + return $method; } @@ -530,6 +535,7 @@ public static function validateCode(string $code) if (\in_array(self::$currentType, $doubleEndpoints)) { throw new \InvalidArgumentException(sprintf('The %s type should have a "code" field set.', self::$currentType), json_encode($code, \JSON_THROW_ON_ERROR)); } + return $code; } } diff --git a/src/Validator/LoaderConfigurationValidator.php b/src/Validator/LoaderConfigurationValidator.php index 23f5fc2..8b986df 100644 --- a/src/Validator/LoaderConfigurationValidator.php +++ b/src/Validator/LoaderConfigurationValidator.php @@ -1,8 +1,8 @@ value => self::$endpointsLegacy }; } + public static function validate(array $item): array { $endpoints = self::getEndpointsApiType(); @@ -273,15 +273,17 @@ public static function validate(array $item): array if (!\in_array($item['method'], $endpoints[$item['type']])) { throw new \InvalidArgumentException(sprintf('the value "method" should be one of [%s], got %s', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR))); } + return $item; } public static function validateApiType(string $apiType) { self::$currentApiType = $apiType; - if(!\in_array($apiType, ApiType::casesValue())){ + if (!\in_array($apiType, ApiType::casesValue())) { throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($apiType, \JSON_THROW_ON_ERROR))); } + return $apiType; } @@ -292,6 +294,7 @@ public static function validateType(string $type) if (!\in_array($type, array_keys($endpoints))) { throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', array_keys($endpoints)), json_encode($type, \JSON_THROW_ON_ERROR))); } + return $type; } @@ -304,6 +307,7 @@ public static function validateMethod(string $method) ) { throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', $endpoints[self::$currentType]), json_encode($method, \JSON_THROW_ON_ERROR))); } + return $method; } } From 0492d94b15c95a6499a02e774f706c784c14899c Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 20 Nov 2023 14:39:28 +0100 Subject: [PATCH 15/27] phpstan fixes --- src/Builder/Client.php | 2 ++ src/Builder/Extractor.php | 2 ++ src/Builder/Loader.php | 2 ++ src/Capacity/Create.php | 2 ++ src/Capacity/Upsert.php | 2 ++ src/Validator/ExtractorConfigurationValidator.php | 9 +++++---- src/Validator/LoaderConfigurationValidator.php | 5 ++++- 7 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Builder/Client.php b/src/Builder/Client.php index bf2c2cc..44bef8c 100644 --- a/src/Builder/Client.php +++ b/src/Builder/Client.php @@ -10,6 +10,7 @@ use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; use PhpParser\Node; +use UnhandledMatchError; final class Client implements Builder { @@ -137,6 +138,7 @@ private function getClientBuilderNode(): Node\Expr\MethodCall ApiType::ADMIN->value => SyliusAdminClientBuilder::class, ApiType::SHOP->value => SyliusShopClientBuilder::class, ApiType::LEGACY->value => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder', + default => throw new UnhandledMatchError($this->apiType) }; return new Node\Expr\MethodCall( diff --git a/src/Builder/Extractor.php b/src/Builder/Extractor.php index 4440a06..63f68fe 100644 --- a/src/Builder/Extractor.php +++ b/src/Builder/Extractor.php @@ -7,6 +7,7 @@ use Kiboko\Contract\Configurator\StepBuilderInterface; use PhpParser\Builder; use PhpParser\Node; +use UnhandledMatchError; final class Extractor implements StepBuilderInterface { @@ -135,6 +136,7 @@ public function getParamsNode(): array 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, + default => throw new UnhandledMatchError($this->apiType) }; return [ diff --git a/src/Builder/Loader.php b/src/Builder/Loader.php index bd90150..b41feca 100644 --- a/src/Builder/Loader.php +++ b/src/Builder/Loader.php @@ -7,6 +7,7 @@ use Kiboko\Contract\Configurator\StepBuilderInterface; use PhpParser\Builder; use PhpParser\Node; +use UnhandledMatchError; final class Loader implements StepBuilderInterface { @@ -141,6 +142,7 @@ public function getParamsNode(): array 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, + default => throw new UnhandledMatchError($this->apiType) }; return [ diff --git a/src/Capacity/Create.php b/src/Capacity/Create.php index 17e0711..242df7e 100644 --- a/src/Capacity/Create.php +++ b/src/Capacity/Create.php @@ -7,6 +7,7 @@ use Kiboko\Plugin\Sylius; use PhpParser\Builder; use PhpParser\Node; +use UnhandledMatchError; final class Create implements CapacityInterface { @@ -85,6 +86,7 @@ public function applies(array $config): bool 'admin' => self::$endpointsAdmin, 'shop' => self::$endpointsShop, 'legacy' => self::$endpointsLegacy, + default => throw new UnhandledMatchError($config['api_type']) }; return isset($config['type']) diff --git a/src/Capacity/Upsert.php b/src/Capacity/Upsert.php index 53b5d58..16ec158 100644 --- a/src/Capacity/Upsert.php +++ b/src/Capacity/Upsert.php @@ -7,6 +7,7 @@ use Kiboko\Plugin\Sylius; use PhpParser\Builder; use PhpParser\Node; +use UnhandledMatchError; final class Upsert implements CapacityInterface { @@ -77,6 +78,7 @@ public function applies(array $config): bool 'admin' => self::$endpointsAdmin, 'shop' => self::$endpointsShop, 'legacy' => self::$endpointsLegacy, + default => throw new UnhandledMatchError($config['api_type']) }; return isset($config['type']) diff --git a/src/Validator/ExtractorConfigurationValidator.php b/src/Validator/ExtractorConfigurationValidator.php index 8d03357..72d8983 100644 --- a/src/Validator/ExtractorConfigurationValidator.php +++ b/src/Validator/ExtractorConfigurationValidator.php @@ -5,6 +5,7 @@ namespace Kiboko\Plugin\Sylius\Validator; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; +use UnhandledMatchError; class ExtractorConfigurationValidator implements ConfigurationValidatorInterface { @@ -461,14 +462,13 @@ class ExtractorConfigurationValidator implements ConfigurationValidatorInterface 'order', ]; - private static array $item = []; - public static function getEndpointsApiType(): array { return match (self::$currentApiType) { ApiType::ADMIN->value => self::$endpointsAdmin, ApiType::SHOP->value => self::$endpointsShop, - ApiType::LEGACY->value => self::$endpointsLegacy + ApiType::LEGACY->value => self::$endpointsLegacy, + default => throw new UnhandledMatchError(self::$currentApiType) }; } @@ -477,7 +477,8 @@ public static function getDoubleEndpointsApiType(): array return match (self::$currentApiType) { ApiType::ADMIN->value => self::$doubleEndpointsAdmin, ApiType::SHOP->value => self::$doubleEndpointsShop, - ApiType::LEGACY->value => self::$doubleEndpointsLegacy + ApiType::LEGACY->value => self::$doubleEndpointsLegacy, + default => throw new UnhandledMatchError(self::$currentApiType) }; } public static string $currentApiType; diff --git a/src/Validator/LoaderConfigurationValidator.php b/src/Validator/LoaderConfigurationValidator.php index 8b986df..874863f 100644 --- a/src/Validator/LoaderConfigurationValidator.php +++ b/src/Validator/LoaderConfigurationValidator.php @@ -4,6 +4,8 @@ namespace Kiboko\Plugin\Sylius\Validator; +use UnhandledMatchError; + class LoaderConfigurationValidator implements ConfigurationValidatorInterface { private static array $endpointsLegacy = [ @@ -260,7 +262,8 @@ public static function getEndpointsApiType(): array return match (self::$currentApiType) { ApiType::ADMIN->value => self::$endpointsAdmin, ApiType::SHOP->value => self::$endpointsShop, - ApiType::LEGACY->value => self::$endpointsLegacy + ApiType::LEGACY->value => self::$endpointsLegacy, + default => throw new UnhandledMatchError(self::$currentApiType), }; } From bb64f43b186c746bc6e46a6d5a491bb960f1fb81 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 20 Nov 2023 14:42:32 +0100 Subject: [PATCH 16/27] cs-fixer --- src/Builder/Client.php | 3 +-- src/Builder/Extractor.php | 3 +-- src/Builder/Loader.php | 3 +-- src/Capacity/Create.php | 3 +-- src/Capacity/Upsert.php | 3 +-- src/Validator/ExtractorConfigurationValidator.php | 5 ++--- src/Validator/LoaderConfigurationValidator.php | 4 +--- 7 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/Builder/Client.php b/src/Builder/Client.php index 44bef8c..078c2ed 100644 --- a/src/Builder/Client.php +++ b/src/Builder/Client.php @@ -10,7 +10,6 @@ use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; use PhpParser\Node; -use UnhandledMatchError; final class Client implements Builder { @@ -138,7 +137,7 @@ private function getClientBuilderNode(): Node\Expr\MethodCall ApiType::ADMIN->value => SyliusAdminClientBuilder::class, ApiType::SHOP->value => SyliusShopClientBuilder::class, ApiType::LEGACY->value => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder', - default => throw new UnhandledMatchError($this->apiType) + default => throw new \UnhandledMatchError($this->apiType) }; return new Node\Expr\MethodCall( diff --git a/src/Builder/Extractor.php b/src/Builder/Extractor.php index 63f68fe..b198293 100644 --- a/src/Builder/Extractor.php +++ b/src/Builder/Extractor.php @@ -7,7 +7,6 @@ use Kiboko\Contract\Configurator\StepBuilderInterface; use PhpParser\Builder; use PhpParser\Node; -use UnhandledMatchError; final class Extractor implements StepBuilderInterface { @@ -136,7 +135,7 @@ public function getParamsNode(): array 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, - default => throw new UnhandledMatchError($this->apiType) + default => throw new \UnhandledMatchError($this->apiType) }; return [ diff --git a/src/Builder/Loader.php b/src/Builder/Loader.php index b41feca..b97af37 100644 --- a/src/Builder/Loader.php +++ b/src/Builder/Loader.php @@ -7,7 +7,6 @@ use Kiboko\Contract\Configurator\StepBuilderInterface; use PhpParser\Builder; use PhpParser\Node; -use UnhandledMatchError; final class Loader implements StepBuilderInterface { @@ -142,7 +141,7 @@ public function getParamsNode(): array 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, - default => throw new UnhandledMatchError($this->apiType) + default => throw new \UnhandledMatchError($this->apiType) }; return [ diff --git a/src/Capacity/Create.php b/src/Capacity/Create.php index 242df7e..780cd22 100644 --- a/src/Capacity/Create.php +++ b/src/Capacity/Create.php @@ -7,7 +7,6 @@ use Kiboko\Plugin\Sylius; use PhpParser\Builder; use PhpParser\Node; -use UnhandledMatchError; final class Create implements CapacityInterface { @@ -86,7 +85,7 @@ public function applies(array $config): bool 'admin' => self::$endpointsAdmin, 'shop' => self::$endpointsShop, 'legacy' => self::$endpointsLegacy, - default => throw new UnhandledMatchError($config['api_type']) + default => throw new \UnhandledMatchError($config['api_type']) }; return isset($config['type']) diff --git a/src/Capacity/Upsert.php b/src/Capacity/Upsert.php index 16ec158..59d2632 100644 --- a/src/Capacity/Upsert.php +++ b/src/Capacity/Upsert.php @@ -7,7 +7,6 @@ use Kiboko\Plugin\Sylius; use PhpParser\Builder; use PhpParser\Node; -use UnhandledMatchError; final class Upsert implements CapacityInterface { @@ -78,7 +77,7 @@ public function applies(array $config): bool 'admin' => self::$endpointsAdmin, 'shop' => self::$endpointsShop, 'legacy' => self::$endpointsLegacy, - default => throw new UnhandledMatchError($config['api_type']) + default => throw new \UnhandledMatchError($config['api_type']) }; return isset($config['type']) diff --git a/src/Validator/ExtractorConfigurationValidator.php b/src/Validator/ExtractorConfigurationValidator.php index 72d8983..e36f407 100644 --- a/src/Validator/ExtractorConfigurationValidator.php +++ b/src/Validator/ExtractorConfigurationValidator.php @@ -5,7 +5,6 @@ namespace Kiboko\Plugin\Sylius\Validator; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; -use UnhandledMatchError; class ExtractorConfigurationValidator implements ConfigurationValidatorInterface { @@ -468,7 +467,7 @@ public static function getEndpointsApiType(): array ApiType::ADMIN->value => self::$endpointsAdmin, ApiType::SHOP->value => self::$endpointsShop, ApiType::LEGACY->value => self::$endpointsLegacy, - default => throw new UnhandledMatchError(self::$currentApiType) + default => throw new \UnhandledMatchError(self::$currentApiType) }; } @@ -478,7 +477,7 @@ public static function getDoubleEndpointsApiType(): array ApiType::ADMIN->value => self::$doubleEndpointsAdmin, ApiType::SHOP->value => self::$doubleEndpointsShop, ApiType::LEGACY->value => self::$doubleEndpointsLegacy, - default => throw new UnhandledMatchError(self::$currentApiType) + default => throw new \UnhandledMatchError(self::$currentApiType) }; } public static string $currentApiType; diff --git a/src/Validator/LoaderConfigurationValidator.php b/src/Validator/LoaderConfigurationValidator.php index 874863f..04ef141 100644 --- a/src/Validator/LoaderConfigurationValidator.php +++ b/src/Validator/LoaderConfigurationValidator.php @@ -4,8 +4,6 @@ namespace Kiboko\Plugin\Sylius\Validator; -use UnhandledMatchError; - class LoaderConfigurationValidator implements ConfigurationValidatorInterface { private static array $endpointsLegacy = [ @@ -263,7 +261,7 @@ public static function getEndpointsApiType(): array ApiType::ADMIN->value => self::$endpointsAdmin, ApiType::SHOP->value => self::$endpointsShop, ApiType::LEGACY->value => self::$endpointsLegacy, - default => throw new UnhandledMatchError(self::$currentApiType), + default => throw new \UnhandledMatchError(self::$currentApiType), }; } From 53fa3b1fad273d4079189c7741763659c667b990 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 20 Nov 2023 16:18:41 +0100 Subject: [PATCH 17/27] improve tests data for factory, add 2 check of wrong config --- src/Capacity/All.php | 8 +-- src/Capacity/Create.php | 6 +- src/Capacity/ListPerPage.php | 8 +-- src/Capacity/Upsert.php | 6 +- tests/functional/Factory/ExtractorTest.php | 77 ++++++++++++++++++++-- tests/functional/Factory/LoaderTest.php | 77 ++++++++++++++++++++-- 6 files changed, 151 insertions(+), 31 deletions(-) diff --git a/src/Capacity/All.php b/src/Capacity/All.php index b8d7d19..3fe81cc 100644 --- a/src/Capacity/All.php +++ b/src/Capacity/All.php @@ -5,6 +5,7 @@ namespace Kiboko\Plugin\Sylius\Capacity; use Kiboko\Plugin\Sylius; +use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; use PhpParser\Node; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; @@ -119,9 +120,6 @@ public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { - if (!isset($config['api_type'])) { - return false; - } switch ($config['api_type']) { case 'admin': $endpoints = self::$endpointsAdmin; @@ -136,9 +134,7 @@ public function applies(array $config): bool $doubleEndpoints = self::$doubleEndpointsLegacy; break; default: - $endpoints = []; - $doubleEndpoints = []; - break; + throw new \InvalidArgumentException(sprintf('The value of api_type should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($config['api_type'], \JSON_THROW_ON_ERROR))); } return isset($config['type']) diff --git a/src/Capacity/Create.php b/src/Capacity/Create.php index 780cd22..75efd8f 100644 --- a/src/Capacity/Create.php +++ b/src/Capacity/Create.php @@ -5,6 +5,7 @@ namespace Kiboko\Plugin\Sylius\Capacity; use Kiboko\Plugin\Sylius; +use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; use PhpParser\Node; @@ -78,14 +79,11 @@ final class Create implements CapacityInterface public function applies(array $config): bool { - if (!isset($config['api_type'])) { - return false; - } $endpoints = match ($config['api_type']) { 'admin' => self::$endpointsAdmin, 'shop' => self::$endpointsShop, 'legacy' => self::$endpointsLegacy, - default => throw new \UnhandledMatchError($config['api_type']) + default => throw new \InvalidArgumentException(sprintf('The value of api_type should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($config['api_type'], \JSON_THROW_ON_ERROR))) }; return isset($config['type']) diff --git a/src/Capacity/ListPerPage.php b/src/Capacity/ListPerPage.php index a91614d..0003b2c 100644 --- a/src/Capacity/ListPerPage.php +++ b/src/Capacity/ListPerPage.php @@ -5,6 +5,7 @@ namespace Kiboko\Plugin\Sylius\Capacity; use Kiboko\Plugin\Sylius; +use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; use PhpParser\Node; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; @@ -118,9 +119,6 @@ public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { - if (!isset($config['api_type'])) { - return false; - } switch ($config['api_type']) { case 'admin': $endpoints = self::$endpointsAdmin; @@ -135,9 +133,7 @@ public function applies(array $config): bool $doubleEndpoints = self::$doubleEndpointsLegacy; break; default: - $endpoints = []; - $doubleEndpoints = []; - break; + throw new \InvalidArgumentException(sprintf('The value of api_type should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($config['api_type'], \JSON_THROW_ON_ERROR))); } return isset($config['type']) diff --git a/src/Capacity/Upsert.php b/src/Capacity/Upsert.php index 59d2632..8130d38 100644 --- a/src/Capacity/Upsert.php +++ b/src/Capacity/Upsert.php @@ -5,6 +5,7 @@ namespace Kiboko\Plugin\Sylius\Capacity; use Kiboko\Plugin\Sylius; +use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; use PhpParser\Node; @@ -70,14 +71,11 @@ final class Upsert implements CapacityInterface public function applies(array $config): bool { - if (!isset($config['api_type'])) { - return false; - } $endpoints = match ($config['api_type']) { 'admin' => self::$endpointsAdmin, 'shop' => self::$endpointsShop, 'legacy' => self::$endpointsLegacy, - default => throw new \UnhandledMatchError($config['api_type']) + default => throw new \InvalidArgumentException(sprintf('The value of api_type should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($config['api_type'], \JSON_THROW_ON_ERROR))) }; return isset($config['type']) diff --git a/tests/functional/Factory/ExtractorTest.php b/tests/functional/Factory/ExtractorTest.php index 34bbca1..e39795b 100644 --- a/tests/functional/Factory/ExtractorTest.php +++ b/tests/functional/Factory/ExtractorTest.php @@ -6,6 +6,7 @@ use Kiboko\Contract\Configurator\InvalidConfigurationException; use Kiboko\Plugin\Sylius\Factory\Extractor; +use Kiboko\Plugin\Sylius\Factory\Loader; use PHPUnit\Framework\TestCase; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; @@ -30,7 +31,18 @@ public static function validDataProvider(): \Generator ]; } - public static function wrongConfigs(): \Generator + public static function wrongApiType(): \Generator + { + yield [ + 'config' => [ + 'type' => 'products', + 'method' => 'get', + 'api_type' => 'wrong', + ], + ]; + } + + public static function missingApiType(): \Generator { yield [ 'config' => [ @@ -43,15 +55,26 @@ public static function wrongConfigs(): \Generator ]; yield [ 'config' => [ - 'type' => 'wrong', - 'method' => 'all', - 'api_type' => 'legacy', + 'type' => 'products', + ], + ]; + yield [ + 'config' => [ + 'method' => 'get', ], ]; yield [ 'config' => [ 'type' => 'products', - 'method' => 'wrong', + 'method' => 'get', + ], + ]; + } + + public static function missingCapacityConfigs(): \Generator + { + yield [ + 'config' => [ 'api_type' => 'legacy', ], ]; @@ -61,9 +84,24 @@ public static function wrongConfigs(): \Generator 'api_type' => 'legacy', ], ]; + yield [ + 'config' => [ + 'api_type' => 'legacy', + 'method' => 'get', + ], + ]; + yield [ + 'config' => [ + 'type' => 'wrong', + 'method' => 'all', + 'api_type' => 'legacy', + ], + ]; yield [ 'config' => [ 'type' => 'products', + 'method' => 'wrong', + 'api_type' => 'legacy', ], ]; } @@ -76,7 +114,34 @@ public function testValidateConfiguration(array $config): void $client->compile($config); } - #[\PHPUnit\Framework\Attributes\DataProvider('wrongConfigs')] + + #[\PHPUnit\Framework\Attributes\DataProvider('wrongApiType')] + public function testWrongApiType(array $config) + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionCode(0); + $this->expectExceptionMessage('The value of api_type should be one of [admin, shop, legacy], got "wrong".'); + + $client = new Loader(); + $this->assertFalse($client->validate($config)); + $client->compile($config); + } + + + #[\PHPUnit\Framework\Attributes\DataProvider('missingApiType')] + public function testMissingApiType(array $config) + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionCode(0); + $this->expectExceptionMessage('The value of api_type should be one of [admin, shop, legacy], got null.'); + + $client = new Loader(); + $this->assertFalse($client->validate($config)); + $client->compile($config); + } + + + #[\PHPUnit\Framework\Attributes\DataProvider('missingCapacityConfigs')] public function testMissingCapacity(array $config): void { $this->expectException(InvalidConfigurationException::class); diff --git a/tests/functional/Factory/LoaderTest.php b/tests/functional/Factory/LoaderTest.php index d95436f..2e2d108 100644 --- a/tests/functional/Factory/LoaderTest.php +++ b/tests/functional/Factory/LoaderTest.php @@ -29,7 +29,18 @@ public static function validDataProvider(): \Generator ]; } - public static function wrongConfigs(): \Generator + public static function wrongApiType(): \Generator + { + yield [ + 'config' => [ + 'type' => 'products', + 'method' => 'upsert', + 'api_type' => 'wrong', + ], + ]; + } + + public static function missingApiType(): \Generator { yield [ 'config' => [ @@ -42,21 +53,52 @@ public static function wrongConfigs(): \Generator ]; yield [ 'config' => [ - 'type' => 'wrong', - 'method' => 'all', + 'type' => 'products', + ], + ]; + yield [ + 'config' => [ + 'method' => 'upsert', + ], + ]; + yield [ + 'config' => [ + 'type' => 'products', + 'method' => 'upsert', + ], + ]; + } + + public static function missingCapacityConfigs(): \Generator + { + yield [ + 'config' => [ 'api_type' => 'legacy', ], ]; yield [ 'config' => [ + 'api_type' => 'legacy', 'type' => 'products', - 'method' => 'wrong', + ], + ]; + yield [ + 'config' => [ + 'method' => 'upsert', + 'api_type' => 'legacy', + ], + ]; + yield [ + 'config' => [ + 'type' => 'wrong', + 'method' => 'upsert', 'api_type' => 'legacy', ], ]; yield [ 'config' => [ 'type' => 'products', + 'method' => 'wrong', 'api_type' => 'legacy', ], ]; @@ -70,7 +112,32 @@ public function testValidateConfiguration(array $config): void $client->compile($config); } - #[\PHPUnit\Framework\Attributes\DataProvider('wrongConfigs')] + #[\PHPUnit\Framework\Attributes\DataProvider('wrongApiType')] + public function testWrongApiType(array $config) + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionCode(0); + $this->expectExceptionMessage('The value of api_type should be one of [admin, shop, legacy], got "wrong".'); + + $client = new Loader(); + $this->assertFalse($client->validate($config)); + $client->compile($config); + } + + + #[\PHPUnit\Framework\Attributes\DataProvider('missingApiType')] + public function testMissingApiType(array $config) + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionCode(0); + $this->expectExceptionMessage('The value of api_type should be one of [admin, shop, legacy], got null.'); + + $client = new Loader(); + $this->assertFalse($client->validate($config)); + $client->compile($config); + } + + #[\PHPUnit\Framework\Attributes\DataProvider('missingCapacityConfigs')] public function testMissingCapacity(array $config): void { $this->expectException(InvalidConfigurationException::class); From 67dacf96e34ff8f7600948784b3257dd61b2c386 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 20 Nov 2023 17:29:34 +0100 Subject: [PATCH 18/27] manage exception for tests and infection, change xml version for validate phpunit config, rename coverage by source --- phpunit.xml | 6 +++--- src/Capacity/All.php | 3 +++ src/Capacity/Create.php | 4 ++++ src/Capacity/ListPerPage.php | 3 +++ src/Capacity/Upsert.php | 3 +++ tests/functional/Factory/ExtractorTest.php | 16 ++++++++++++++-- tests/functional/Factory/LoaderTest.php | 4 ++-- 7 files changed, 32 insertions(+), 7 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index eadaa79..7fb2e0e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,7 @@ tests/functional/ - + src - + diff --git a/src/Capacity/All.php b/src/Capacity/All.php index 3fe81cc..e0aac54 100644 --- a/src/Capacity/All.php +++ b/src/Capacity/All.php @@ -120,6 +120,9 @@ public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { + if (!isset($config['api_type'])) { + return false; + } switch ($config['api_type']) { case 'admin': $endpoints = self::$endpointsAdmin; diff --git a/src/Capacity/Create.php b/src/Capacity/Create.php index 75efd8f..36157a3 100644 --- a/src/Capacity/Create.php +++ b/src/Capacity/Create.php @@ -4,6 +4,7 @@ namespace Kiboko\Plugin\Sylius\Capacity; +use Kiboko\Contract\Configurator\InvalidConfigurationException; use Kiboko\Plugin\Sylius; use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; @@ -79,6 +80,9 @@ final class Create implements CapacityInterface public function applies(array $config): bool { + if(!isset($config['api_type'])) { + throw new InvalidConfigurationException('Your Sylius API configuration is using some unsupported capacity, check your "api_type" properties to a suitable set.'); + } $endpoints = match ($config['api_type']) { 'admin' => self::$endpointsAdmin, 'shop' => self::$endpointsShop, diff --git a/src/Capacity/ListPerPage.php b/src/Capacity/ListPerPage.php index 0003b2c..2b4d06a 100644 --- a/src/Capacity/ListPerPage.php +++ b/src/Capacity/ListPerPage.php @@ -119,6 +119,9 @@ public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { + if (!isset($config['api_type'])) { + return false; + } switch ($config['api_type']) { case 'admin': $endpoints = self::$endpointsAdmin; diff --git a/src/Capacity/Upsert.php b/src/Capacity/Upsert.php index 8130d38..ac699f1 100644 --- a/src/Capacity/Upsert.php +++ b/src/Capacity/Upsert.php @@ -71,6 +71,9 @@ final class Upsert implements CapacityInterface public function applies(array $config): bool { + if (!isset($config['api_type'])) { + return false; + } $endpoints = match ($config['api_type']) { 'admin' => self::$endpointsAdmin, 'shop' => self::$endpointsShop, diff --git a/tests/functional/Factory/ExtractorTest.php b/tests/functional/Factory/ExtractorTest.php index e39795b..8dd3f3a 100644 --- a/tests/functional/Factory/ExtractorTest.php +++ b/tests/functional/Factory/ExtractorTest.php @@ -40,6 +40,18 @@ public static function wrongApiType(): \Generator 'api_type' => 'wrong', ], ]; + yield [ + 'config' => [ + 'type' => 'products', + 'api_type' => 'wrong', + ], + ]; + yield [ + 'config' => [ + 'method' => 'get', + 'api_type' => 'wrong', + ], + ]; } public static function missingApiType(): \Generator @@ -131,9 +143,9 @@ public function testWrongApiType(array $config) #[\PHPUnit\Framework\Attributes\DataProvider('missingApiType')] public function testMissingApiType(array $config) { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidConfigurationException::class); $this->expectExceptionCode(0); - $this->expectExceptionMessage('The value of api_type should be one of [admin, shop, legacy], got null.'); + $this->expectExceptionMessage('Your Sylius API configuration is using some unsupported capacity, check your "api_type" properties to a suitable set.'); $client = new Loader(); $this->assertFalse($client->validate($config)); diff --git a/tests/functional/Factory/LoaderTest.php b/tests/functional/Factory/LoaderTest.php index 2e2d108..686d6f0 100644 --- a/tests/functional/Factory/LoaderTest.php +++ b/tests/functional/Factory/LoaderTest.php @@ -128,9 +128,9 @@ public function testWrongApiType(array $config) #[\PHPUnit\Framework\Attributes\DataProvider('missingApiType')] public function testMissingApiType(array $config) { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidConfigurationException::class); $this->expectExceptionCode(0); - $this->expectExceptionMessage('The value of api_type should be one of [admin, shop, legacy], got null.'); + $this->expectExceptionMessage('Your Sylius API configuration is using some unsupported capacity, check your "api_type" properties to a suitable set.'); $client = new Loader(); $this->assertFalse($client->validate($config)); From 5eca1e48efc590b1faf8eaea687d6765d7d19069 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Tue, 21 Nov 2023 10:46:27 +0100 Subject: [PATCH 19/27] improve tests and mutation percent, update phpunit config --- phpunit.xml | 5 - src/Capacity/All.php | 3 +- src/Capacity/ListPerPage.php | 3 +- src/Capacity/Upsert.php | 3 +- .../Configuration/ExtractorTest.php | 84 ++++++++++ tests/functional/Configuration/LoaderTest.php | 93 +++++++++++- tests/functional/Factory/ExtractorTest.php | 143 +++++++++++++++++- tests/functional/Factory/LoaderTest.php | 40 +++++ 8 files changed, 356 insertions(+), 18 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 7fb2e0e..bd96811 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -21,11 +21,6 @@ tests/functional/ - - - src - - diff --git a/src/Capacity/All.php b/src/Capacity/All.php index e0aac54..6a9f9d5 100644 --- a/src/Capacity/All.php +++ b/src/Capacity/All.php @@ -4,6 +4,7 @@ namespace Kiboko\Plugin\Sylius\Capacity; +use Kiboko\Contract\Configurator\InvalidConfigurationException; use Kiboko\Plugin\Sylius; use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; @@ -121,7 +122,7 @@ public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { if (!isset($config['api_type'])) { - return false; + throw new InvalidConfigurationException('Your Sylius API configuration is using some unsupported capacity, check your "api_type" properties to a suitable set.'); } switch ($config['api_type']) { case 'admin': diff --git a/src/Capacity/ListPerPage.php b/src/Capacity/ListPerPage.php index 2b4d06a..084070b 100644 --- a/src/Capacity/ListPerPage.php +++ b/src/Capacity/ListPerPage.php @@ -4,6 +4,7 @@ namespace Kiboko\Plugin\Sylius\Capacity; +use Kiboko\Contract\Configurator\InvalidConfigurationException; use Kiboko\Plugin\Sylius; use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; @@ -120,7 +121,7 @@ public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { if (!isset($config['api_type'])) { - return false; + throw new InvalidConfigurationException('Your Sylius API configuration is using some unsupported capacity, check your "api_type" properties to a suitable set.'); } switch ($config['api_type']) { case 'admin': diff --git a/src/Capacity/Upsert.php b/src/Capacity/Upsert.php index ac699f1..5ff97c9 100644 --- a/src/Capacity/Upsert.php +++ b/src/Capacity/Upsert.php @@ -4,6 +4,7 @@ namespace Kiboko\Plugin\Sylius\Capacity; +use Kiboko\Contract\Configurator\InvalidConfigurationException; use Kiboko\Plugin\Sylius; use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; @@ -72,7 +73,7 @@ final class Upsert implements CapacityInterface public function applies(array $config): bool { if (!isset($config['api_type'])) { - return false; + throw new InvalidConfigurationException('Your Sylius API configuration is using some unsupported capacity, check your "api_type" properties to a suitable set.'); } $endpoints = match ($config['api_type']) { 'admin' => self::$endpointsAdmin, diff --git a/tests/functional/Configuration/ExtractorTest.php b/tests/functional/Configuration/ExtractorTest.php index f682f33..7d2a52c 100644 --- a/tests/functional/Configuration/ExtractorTest.php +++ b/tests/functional/Configuration/ExtractorTest.php @@ -61,6 +61,90 @@ public static function validDataProvider(): iterable 'search' => [], ], ]; + yield [ + 'config' => [ + 'type' => 'product', + 'method' => 'get', + 'api_type' => 'admin', + 'search' => [], + ], + 'expected' => [ + 'type' => 'product', + 'method' => 'get', + 'api_type' => 'admin', + 'search' => [], + ], + ]; + yield [ + 'config' => [ + 'type' => 'product', + 'method' => 'all', + 'api_type' => 'admin', + 'search' => [], + ], + 'expected' => [ + 'type' => 'product', + 'method' => 'all', + 'api_type' => 'admin', + 'search' => [], + ], + ]; + yield [ + 'config' => [ + 'type' => 'product', + 'method' => 'listPerPage', + 'api_type' => 'admin', + 'search' => [], + ], + 'expected' => [ + 'type' => 'product', + 'method' => 'listPerPage', + 'api_type' => 'admin', + 'search' => [], + ], + ]; + yield [ + 'config' => [ + 'type' => 'product', + 'method' => 'get', + 'api_type' => 'shop', + 'search' => [], + ], + 'expected' => [ + 'type' => 'product', + 'method' => 'get', + 'api_type' => 'shop', + 'search' => [], + ], + ]; + yield [ + 'config' => [ + 'type' => 'product', + 'method' => 'all', + 'api_type' => 'shop', + 'search' => [], + ], + 'expected' => [ + 'type' => 'product', + 'method' => 'all', + 'api_type' => 'shop', + 'search' => [], + ], + ]; + yield [ + 'config' => [ + 'type' => 'product', + 'method' => 'listPerPage', + 'api_type' => 'shop', + 'search' => [], + ], + 'expected' => [ + 'type' => 'product', + 'method' => 'listPerPage', + 'api_type' => 'shop', + 'search' => [], + ], + ]; } #[\PHPUnit\Framework\Attributes\DataProvider('validDataProvider')] diff --git a/tests/functional/Configuration/LoaderTest.php b/tests/functional/Configuration/LoaderTest.php index b89c5c7..0de9720 100644 --- a/tests/functional/Configuration/LoaderTest.php +++ b/tests/functional/Configuration/LoaderTest.php @@ -16,15 +16,100 @@ protected function setUp(): void { $this->processor = new Config\Definition\Processor(); } + + public static function validDataProvider(): iterable + { + yield [ + 'config' => [ + 'type' => 'products', + 'method' => 'create', + 'api_type' => 'legacy', + ], + 'expected' => [ + 'type' => 'products', + 'method' => 'create', + 'api_type' => 'legacy', + ], + ]; + yield [ + 'config' => [ + 'type' => 'products', + 'method' => 'upsert', + 'api_type' => 'legacy', + ], + 'expected' => [ + 'type' => 'products', + 'method' => 'upsert', + 'api_type' => 'legacy', + ], + ]; + yield [ + 'config' => [ + 'type' => 'product', + 'method' => 'create', + 'api_type' => 'admin', + ], + 'expected' => [ + 'type' => 'product', + 'method' => 'create', + 'api_type' => 'admin', + ], + ]; + yield [ + 'config' => [ + 'type' => 'product', + 'method' => 'upsert', + 'api_type' => 'admin', + ], + 'expected' => [ + 'type' => 'product', + 'method' => 'upsert', + 'api_type' => 'admin', + ], + ]; + yield [ + 'config' => [ + 'type' => 'customer', + 'method' => 'create', + 'api_type' => 'shop', + ], + 'expected' => [ + 'type' => 'customer', + 'method' => 'create', + 'api_type' => 'shop', + ], + ]; + yield [ + 'config' => [ + 'type' => 'customer', + 'method' => 'upsert', + 'api_type' => 'shop', + ], + 'expected' => [ + 'type' => 'customer', + 'method' => 'upsert', + 'api_type' => 'shop', + ], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('validDataProvider')] + public function testValidConfig(array $config, array $expected): void + { + $client = new Configuration\Loader(); + + $this->assertSame($expected, $this->processor->processConfiguration($client, [$config])); + } + public function testWrongApiType(): void { - $client = new Configuration\Extractor(); + $client = new Configuration\Loader(); $this->expectException( Config\Definition\Exception\InvalidConfigurationException::class, ); $this->expectExceptionMessage( - 'Invalid configuration for path "extractor.api_type": the value should be one of [admin, shop, legacy], got "invalidValue".', + 'Invalid configuration for path "loader.api_type": the value should be one of [admin, shop, legacy], got "invalidValue".', ); $this->processor->processConfiguration($client, [ @@ -38,13 +123,13 @@ public function testWrongApiType(): void public function testWrongType(): void { - $client = new Configuration\Extractor(); + $client = new Configuration\Loader(); $this->expectException( Config\Definition\Exception\InvalidConfigurationException::class, ); $this->expectExceptionMessage( - 'Invalid configuration for path "extractor.type": the value should be one of [channels, countries, carts, currencies, customers, exchangeRates, locales, orders, payments, paymentMethods, products, productAttributes, productAssociationTypes, productOptions, promotions, shipments, shippingCategories, taxCategories, taxRates, taxons, users, zones, productReviews, productVariants, promotionCoupons], got "wrong".', + 'Invalid configuration for path "loader.type": the value should be one of [channels, countries, carts, currencies, customers, exchangeRates, locales, orders, payments, paymentMethods, products, productAttributes, productAssociationTypes, productOptions, promotions, shipments, shippingCategories, taxCategories, taxRates, taxons, users, zones], got "wrong".', ); $this->processor->processConfiguration($client, [ diff --git a/tests/functional/Factory/ExtractorTest.php b/tests/functional/Factory/ExtractorTest.php index 8dd3f3a..dc1970d 100644 --- a/tests/functional/Factory/ExtractorTest.php +++ b/tests/functional/Factory/ExtractorTest.php @@ -21,7 +21,6 @@ public static function validDataProvider(): \Generator 'api_type' => 'legacy', ], ]; - yield [ [ 'type' => 'products', @@ -29,6 +28,34 @@ public static function validDataProvider(): \Generator 'api_type' => 'legacy', ], ]; + yield [ + [ + 'type' => 'product', + 'method' => 'all', + 'api_type' => 'admin', + ], + ]; + yield [ + [ + 'type' => 'product', + 'method' => 'listPerPage', + 'api_type' => 'admin', + ], + ]; + yield [ + [ + 'type' => 'product', + 'method' => 'all', + 'api_type' => 'shop', + ], + ]; + yield [ + [ + 'type' => 'product', + 'method' => 'listPerPage', + 'api_type' => 'shop', + ], + ]; } public static function wrongApiType(): \Generator @@ -36,7 +63,7 @@ public static function wrongApiType(): \Generator yield [ 'config' => [ 'type' => 'products', - 'method' => 'get', + 'method' => 'all', 'api_type' => 'wrong', ], ]; @@ -48,7 +75,12 @@ public static function wrongApiType(): \Generator ]; yield [ 'config' => [ - 'method' => 'get', + 'method' => 'all', + 'api_type' => 'wrong', + ], + ]; + yield [ + 'config' => [ 'api_type' => 'wrong', ], ]; @@ -72,13 +104,13 @@ public static function missingApiType(): \Generator ]; yield [ 'config' => [ - 'method' => 'get', + 'method' => 'all', ], ]; yield [ 'config' => [ 'type' => 'products', - 'method' => 'get', + 'method' => 'all', ], ]; } @@ -90,6 +122,16 @@ public static function missingCapacityConfigs(): \Generator 'api_type' => 'legacy', ], ]; + yield [ + 'config' => [ + 'api_type' => 'admin', + ], + ]; + yield [ + 'config' => [ + 'api_type' => 'shop', + ], + ]; yield [ 'config' => [ 'type' => 'products', @@ -99,7 +141,31 @@ public static function missingCapacityConfigs(): \Generator yield [ 'config' => [ 'api_type' => 'legacy', - 'method' => 'get', + 'method' => 'all', + ], + ]; + yield [ + 'config' => [ + 'type' => 'product', + 'api_type' => 'admin', + ], + ]; + yield [ + 'config' => [ + 'api_type' => 'admin', + 'method' => 'all', + ], + ]; + yield [ + 'config' => [ + 'type' => 'product', + 'api_type' => 'shop', + ], + ]; + yield [ + 'config' => [ + 'api_type' => 'shop', + 'method' => 'all', ], ]; yield [ @@ -116,6 +182,59 @@ public static function missingCapacityConfigs(): \Generator 'api_type' => 'legacy', ], ]; + yield [ + 'config' => [ + 'type' => 'wrong', + 'method' => 'all', + 'api_type' => 'admin', + ], + ]; + yield [ + 'config' => [ + 'type' => 'products', + 'method' => 'wrong', + 'api_type' => 'admin', + ], + ]; + yield [ + 'config' => [ + 'type' => 'wrong', + 'method' => 'all', + 'api_type' => 'shop', + ], + ]; + yield [ + 'config' => [ + 'type' => 'products', + 'method' => 'wrong', + 'api_type' => 'shop', + ], + ]; + } + + public static function wrongConfigs(): \Generator + { + yield [ + 'config' => [ + 'azerty' => 'tata', + 'api_type' => 'legacy', + ], + ]; + yield [ + 'config' => [ + 'azerty' => 'tata', + 'meyuiop' => 'toto', + 'api_type' => 'admin', + ], + ]; + yield [ + 'config' => [ + 'azerty' => 'tata', + 'meyuiop' => 'toto', + 'qsdfgh' => 'tutu', + 'api_type' => 'shop', + ], + ]; } #[\PHPUnit\Framework\Attributes\DataProvider('validDataProvider')] @@ -164,4 +283,16 @@ public function testMissingCapacity(array $config): void $this->assertFalse($client->validate($config)); $client->compile($config); } + + #[\PHPUnit\Framework\Attributes\DataProvider('wrongConfigs')] + public function testWrongConfigs(array $config): void + { + $this->expectException(InvalidConfigurationException::class); + $this->expectExceptionCode(0); + $this->expectExceptionMessage('Your Sylius API configuration is using some unsupported capacity, check your "type" and "method" properties to a suitable set.'); + + $client = new Extractor(new ExpressionLanguage()); + $this->assertFalse($client->validate($config)); + $client->compile($config); + } } diff --git a/tests/functional/Factory/LoaderTest.php b/tests/functional/Factory/LoaderTest.php index 686d6f0..a04cf87 100644 --- a/tests/functional/Factory/LoaderTest.php +++ b/tests/functional/Factory/LoaderTest.php @@ -5,8 +5,10 @@ namespace functional\Kiboko\Plugin\Sylius\Factory; use Kiboko\Contract\Configurator\InvalidConfigurationException; +use Kiboko\Plugin\Sylius\Factory\Extractor; use Kiboko\Plugin\Sylius\Factory\Loader; use PHPUnit\Framework\TestCase; +use Symfony\Component\ExpressionLanguage\ExpressionLanguage; final class LoaderTest extends TestCase { @@ -104,6 +106,32 @@ public static function missingCapacityConfigs(): \Generator ]; } + public static function wrongConfigs(): \Generator + { + yield [ + 'config' => [ + 'azerty' => 'tata', + 'api_type' => 'legacy', + ], + ]; + yield [ + 'config' => [ + 'azerty' => 'tata', + 'meyuiop' => 'toto', + 'api_type' => 'admin', + ], + ]; + yield [ + 'config' => [ + 'azerty' => 'tata', + 'meyuiop' => 'toto', + 'qsdfgh' => 'tutu', + 'api_type' => 'shop', + ], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('validDataProvider')] public function testValidateConfiguration(array $config): void { @@ -148,4 +176,16 @@ public function testMissingCapacity(array $config): void $this->assertFalse($client->validate($config)); $client->compile($config); } + + #[\PHPUnit\Framework\Attributes\DataProvider('wrongConfigs')] + public function testWrongConfigs(array $config): void + { + $this->expectException(InvalidConfigurationException::class); + $this->expectExceptionCode(0); + $this->expectExceptionMessage('Your Sylius API configuration is using some unsupported capacity, check your "type" and "method" properties to a suitable set.'); + + $client = new Loader(); + $this->assertFalse($client->validate($config)); + $client->compile($config); + } } From f22f4a78fbd11faef120d7a7ffed04274d48103c Mon Sep 17 00:00:00 2001 From: Jonathan Date: Tue, 21 Nov 2023 11:21:24 +0100 Subject: [PATCH 20/27] fix infection version for accept phpunit 10.1.1 config (source tag replace coverage tag) --- .github/workflows/infection.yaml | 4 +-- phpunit.xml | 48 +++++++++++++++++++------------- src/Capacity/Create.php | 2 +- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/.github/workflows/infection.yaml b/.github/workflows/infection.yaml index c5adb1e..aacb82d 100644 --- a/.github/workflows/infection.yaml +++ b/.github/workflows/infection.yaml @@ -23,8 +23,8 @@ jobs: - name: Infection run: | - wget -q https://github.com/infection/infection/releases/download/0.26.18/infection.phar - wget -q https://github.com/infection/infection/releases/download/0.26.18/infection.phar.asc + wget -q https://github.com/infection/infection/releases/download/0.27.0/infection.phar + wget -q https://github.com/infection/infection/releases/download/0.27.0/infection.phar.asc chmod +x infection.phar ./infection.phar diff --git a/phpunit.xml b/phpunit.xml index bd96811..41806ce 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,26 +1,36 @@ - - + + - tests/functional/ + tests/functional + + + src + + diff --git a/src/Capacity/Create.php b/src/Capacity/Create.php index 36157a3..6205627 100644 --- a/src/Capacity/Create.php +++ b/src/Capacity/Create.php @@ -80,7 +80,7 @@ final class Create implements CapacityInterface public function applies(array $config): bool { - if(!isset($config['api_type'])) { + if (!isset($config['api_type'])) { throw new InvalidConfigurationException('Your Sylius API configuration is using some unsupported capacity, check your "api_type" properties to a suitable set.'); } $endpoints = match ($config['api_type']) { From 0e7e2f2d71a84b51f1f1fd284c7ee8cb19c078c4 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Wed, 22 Nov 2023 11:11:58 +0100 Subject: [PATCH 21/27] use api_type to switch parameters for authentications methods --- src/Builder/Client.php | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Builder/Client.php b/src/Builder/Client.php index 078c2ed..9f5069b 100644 --- a/src/Builder/Client.php +++ b/src/Builder/Client.php @@ -167,20 +167,33 @@ private function getFactoryMethod(): string private function getFactoryArguments(): array { if (null !== $this->password) { + if ($this->apiType === ApiType::LEGACY->value) { + return [ + $this->clientId, + $this->secret, + $this->username, + $this->password, + ]; + } + return [ - $this->clientId, - $this->secret, $this->username, $this->password, ]; } if (null !== $this->refreshToken) { + if ($this->apiType === ApiType::LEGACY->value) { + return [ + $this->clientId, + $this->secret, + $this->token, + $this->refreshToken, + ]; + } + return [ - $this->clientId, - $this->secret, $this->token, - $this->refreshToken, ]; } From 27a7769e8d36e4b676f2ad58c4b86db0d49a0855 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Thu, 23 Nov 2023 10:07:59 +0100 Subject: [PATCH 22/27] Add withSecret methods on the client to avoid clientId and secret parameters to be required --- src/Builder/Client.php | 13 ++++++++++++- src/Configuration/Client.php | 2 -- src/Factory/Client.php | 11 +++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Builder/Client.php b/src/Builder/Client.php index 9f5069b..9f91e2f 100644 --- a/src/Builder/Client.php +++ b/src/Builder/Client.php @@ -13,6 +13,9 @@ final class Client implements Builder { + + private ?Node\Expr $clientId = null; + private ?Node\Expr $secret = null; private ?Node\Expr $username = null; private ?Node\Expr $password = null; private ?Node\Expr $token = null; @@ -27,7 +30,15 @@ final class Client implements Builder 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) {} + public function __construct(private readonly Node\Expr $baseUrl) {} + + public function withSecret(Node\Expr $clientId, Node\Expr $secret): self + { + $this->clientId = $clientId; + $this->secret = $secret; + + return $this; + } public function withToken(Node\Expr $token, Node\Expr $refreshToken): self { diff --git a/src/Configuration/Client.php b/src/Configuration/Client.php index bd4840f..7f01cd6 100644 --- a/src/Configuration/Client.php +++ b/src/Configuration/Client.php @@ -76,7 +76,6 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui ->end() ->end() ->scalarNode('client_id') - ->isRequired() ->cannotBeEmpty() ->validate() ->ifTrue(isExpression()) @@ -84,7 +83,6 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui ->end() ->end() ->scalarNode('secret') - ->isRequired() ->cannotBeEmpty() ->validate() ->ifTrue(isExpression()) diff --git a/src/Factory/Client.php b/src/Factory/Client.php index eccf9d8..3bbcdb3 100644 --- a/src/Factory/Client.php +++ b/src/Factory/Client.php @@ -72,8 +72,6 @@ public function compile(array $config): Repository\Client try { $clientBuilder = new Sylius\Builder\Client( compileValueWhenExpression($this->interpreter, $config['api_url']), - compileValueWhenExpression($this->interpreter, $config['client_id']), - compileValueWhenExpression($this->interpreter, $config['secret']), ); if (isset($config['context'])) { @@ -95,6 +93,15 @@ public function compile(array $config): Repository\Client $clientBuilder->withApiType($config['api_type']); } + if (isset($config['client_id']) && isset($config['secret'])) { + if (isset($config['api_type']) && $config['api_type'] === Sylius\Validator\ApiType::LEGACY->value) { + $clientBuilder->withSecret( + compileValueWhenExpression($this->interpreter, $config['client_id']), + compileValueWhenExpression($this->interpreter, $config['secret']) + ); + } + } + if (isset($config['password'])) { $clientBuilder->withPassword( compileValueWhenExpression($this->interpreter, $config['username']), From 65caa5f2f229a759152e77835a91e3ff5322e086 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Thu, 23 May 2024 16:36:32 +0200 Subject: [PATCH 23/27] code format commit --- .github/workflows/infection.yaml | 4 ++-- composer.lock | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/infection.yaml b/.github/workflows/infection.yaml index aacb82d..3220a7b 100644 --- a/.github/workflows/infection.yaml +++ b/.github/workflows/infection.yaml @@ -23,8 +23,8 @@ jobs: - name: Infection run: | - wget -q https://github.com/infection/infection/releases/download/0.27.0/infection.phar - wget -q https://github.com/infection/infection/releases/download/0.27.0/infection.phar.asc + wget -q https://github.com/infection/infection/releases/download/0.26.20/infection.phar + wget -q https://github.com/infection/infection/releases/download/0.26.20/infection.phar.asc chmod +x infection.phar ./infection.phar diff --git a/composer.lock b/composer.lock index f96587f..2b9c0a8 100644 --- a/composer.lock +++ b/composer.lock @@ -4866,16 +4866,16 @@ }, { "name": "phpunit/phpunit", - "version": "10.1.1", + "version": "10.2.6", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "0d9401b7e8245d71079e249e3cb868e9d2337887" + "reference": "1c17815c129f133f3019cc18e8d0c8622e6d9bcd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/0d9401b7e8245d71079e249e3cb868e9d2337887", - "reference": "0d9401b7e8245d71079e249e3cb868e9d2337887", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/1c17815c129f133f3019cc18e8d0c8622e6d9bcd", + "reference": "1c17815c129f133f3019cc18e8d0c8622e6d9bcd", "shasum": "" }, "require": { @@ -4915,7 +4915,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "10.1-dev" + "dev-main": "10.2-dev" } }, "autoload": { @@ -4947,7 +4947,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/10.1.1" + "source": "https://github.com/sebastianbergmann/phpunit/tree/10.2.6" }, "funding": [ { @@ -4963,7 +4963,7 @@ "type": "tidelift" } ], - "time": "2023-04-17T12:17:05+00:00" + "time": "2023-07-17T12:08:28+00:00" }, { "name": "psr/event-dispatcher", From 09e8740562f860ddbfffb37cf154d518c250259d Mon Sep 17 00:00:00 2001 From: sebprt Date: Mon, 8 Jul 2024 17:17:07 +0200 Subject: [PATCH 24/27] Improved the Sylius plugin for 1;9 or more recent version --- src/ApiType.php | 9 + src/Builder/Client.php | 45 +- src/Builder/Extractor.php | 19 +- src/Builder/Loader.php | 15 +- src/Capacity/All.php | 141 +----- src/Capacity/Create.php | 82 +--- src/Capacity/ListPerPage.php | 128 +----- src/Capacity/Upsert.php | 75 +--- src/Configuration.php | 25 +- src/Configuration/Client.php | 84 +--- src/Configuration/Extractor.php | 19 - src/Configuration/Loader.php | 13 - src/Factory/Client.php | 66 +-- src/Service.php | 13 +- src/Validator/ApiType.php | 22 - .../ConfigurationValidatorInterface.php | 10 - .../ExtractorConfigurationValidator.php | 412 ++++-------------- .../LoaderConfigurationValidator.php | 272 ++++-------- 18 files changed, 306 insertions(+), 1144 deletions(-) create mode 100644 src/ApiType.php delete mode 100644 src/Validator/ApiType.php delete mode 100644 src/Validator/ConfigurationValidatorInterface.php diff --git a/src/ApiType.php b/src/ApiType.php new file mode 100644 index 0000000..df140cb --- /dev/null +++ b/src/ApiType.php @@ -0,0 +1,9 @@ +apiType = $apiType; - - return $this; - } - public function withPassword(Node\Expr $username, Node\Expr $password): self { $this->username = $username; @@ -91,9 +79,16 @@ public function withFileSystem(Node\Expr $fileSystem): self return $this; } + public function withClientBuilder(Node\Expr $client): self + { + $this->client = $client; + + return $this; + } + public function getNode(): Node\Expr\MethodCall { - $instance = $this->getClientBuilderNode(); + $instance = $this->client; if (null !== $this->httpClient) { $instance = new Node\Expr\MethodCall( @@ -144,17 +139,8 @@ public function getNode(): Node\Expr\MethodCall private function getClientBuilderNode(): Node\Expr\MethodCall { - $className = match ($this->apiType) { - ApiType::ADMIN->value => SyliusAdminClientBuilder::class, - ApiType::SHOP->value => SyliusShopClientBuilder::class, - ApiType::LEGACY->value => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder', - default => throw new \UnhandledMatchError($this->apiType) - }; - return new Node\Expr\MethodCall( - var: new Node\Expr\New_( - new Node\Name\FullyQualified($className), - ), + var: $this->client, name: new Node\Identifier('setBaseUri'), args: [ new Node\Arg($this->baseUrl), @@ -178,15 +164,6 @@ private function getFactoryMethod(): string private function getFactoryArguments(): array { if (null !== $this->password) { - if ($this->apiType === ApiType::LEGACY->value) { - return [ - $this->clientId, - $this->secret, - $this->username, - $this->password, - ]; - } - return [ $this->username, $this->password, diff --git a/src/Builder/Extractor.php b/src/Builder/Extractor.php index b198293..d90b3ef 100644 --- a/src/Builder/Extractor.php +++ b/src/Builder/Extractor.php @@ -12,9 +12,11 @@ final class Extractor implements StepBuilderInterface { private ?Node\Expr $logger = null; private ?Node\Expr $client = null; - private string $apiType; + private ?Node $type = null; - public function __construct(private readonly Builder $capacity) {} + public function __construct( + private readonly Builder $capacity, + ) {} public function withClient(Node\Expr $client): self { @@ -23,9 +25,9 @@ public function withClient(Node\Expr $client): self return $this; } - public function withApiType(string $apiType): self + public function withClientType(Node $type): self { - $this->apiType = $apiType; + $this->type = $type; return $this; } @@ -131,17 +133,10 @@ class: new Node\Stmt\Class_( public function getParamsNode(): array { - $className = match ($this->apiType) { - 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, - default => throw new \UnhandledMatchError($this->apiType) - }; - return [ new Node\Param( var: new Node\Expr\Variable('client'), - type: new Node\Name\FullyQualified(name: $className), + type: $this->type, flags: Node\Stmt\Class_::MODIFIER_PRIVATE, ), new Node\Param( diff --git a/src/Builder/Loader.php b/src/Builder/Loader.php index b97af37..0cf2abc 100644 --- a/src/Builder/Loader.php +++ b/src/Builder/Loader.php @@ -12,7 +12,7 @@ final class Loader implements StepBuilderInterface { private ?Node\Expr $logger = null; private ?Node\Expr $client = null; - private string $apiType; + private ?Node $type = null; public function __construct(private readonly Builder $capacity) {} @@ -23,9 +23,9 @@ public function withClient(Node\Expr $client): self return $this; } - public function withApiType(string $apiType): self + public function withClientType(Node $type): self { - $this->apiType = $apiType; + $this->type = $type; return $this; } @@ -137,17 +137,10 @@ class: new Node\Stmt\Class_( public function getParamsNode(): array { - $className = match ($this->apiType) { - 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, - default => throw new \UnhandledMatchError($this->apiType) - }; - return [ new Node\Param( var: new Node\Expr\Variable('client'), - type: new Node\Name\FullyQualified(name: $className), + type: $this->type, flags: Node\Stmt\Class_::MODIFIER_PRIVATE, ), new Node\Param( diff --git a/src/Capacity/All.php b/src/Capacity/All.php index 6a9f9d5..57881e1 100644 --- a/src/Capacity/All.php +++ b/src/Capacity/All.php @@ -4,145 +4,26 @@ namespace Kiboko\Plugin\Sylius\Capacity; -use Kiboko\Contract\Configurator\InvalidConfigurationException; use Kiboko\Plugin\Sylius; -use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; use PhpParser\Node; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; -use function Kiboko\Component\SatelliteToolbox\Configuration\compileValue; +use function Kiboko\Component\SatelliteToolbox\Configuration\compileValueWhenExpression; final class All implements CapacityInterface { - private static array $endpointsLegacy = [ - // Simple resources Endpoints - 'channels', - 'countries', - 'carts', - 'channels', - 'countries', - 'currencies', - 'customers', - 'exchangeRates', - 'locales', - 'orders', - 'payments', - 'paymentMethods', - 'products', - 'productAttributes', - 'productAssociationTypes', - 'productOptions', - 'promotions', - 'shipments', - 'shippingCategories', - 'taxCategories', - 'taxRates', - 'taxons', - 'users', - 'zones', - ]; - - private static array $endpointsAdmin = [ - // Simple Ressource Endpoints - 'adjustment', - 'administrator', - 'catalogPromotion', - 'channel', - 'country', - 'currency', - 'customerGroup', - 'exchangeRate', - 'locale', - 'order', - 'payment', - 'product', - 'productAssociationType', - 'productImage', - 'productOption', - 'productOptionValue', - 'productReview', - 'productTaxon', - 'productVariant', - 'promotion', - 'province', - 'shipment', - 'shippingCategory', - 'shippingMethod', - 'ShopBillingData', - 'taxCategory', - 'taxon', - 'taxonTranslation', - 'zone', - 'zoneMember', - ]; - - private static array $endpointsShop = [ - // Simple Ressource Endpoints - 'address', - 'adjustment', - 'country', - 'currency', - 'locale', - 'order', - 'orderItem', - 'payment', - 'paymentMethod', - 'product', - 'productReview', - 'productVariant', - 'shipment', - 'shippingMethod', - 'taxon', - ]; - - private static array $doubleEndpointsLegacy = [ - // Double resources Endpoints - 'productReviews', - 'productVariants', - 'promotionCoupons', - ]; - - private static array $doubleEndpointsAdmin = [ - // Double resources Endpoints - 'adjustment', - 'province', - 'shopBillingData', - 'zoneMember', - ]; - - private static array $doubleEndpointsShop = [ - // Double resources Endpoints - 'adjustment', - 'order', - ]; - public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { - if (!isset($config['api_type'])) { - throw new InvalidConfigurationException('Your Sylius API configuration is using some unsupported capacity, check your "api_type" properties to a suitable set.'); - } - switch ($config['api_type']) { - case 'admin': - $endpoints = self::$endpointsAdmin; - $doubleEndpoints = self::$doubleEndpointsAdmin; - break; - case 'shop': - $endpoints = self::$endpointsShop; - $doubleEndpoints = self::$doubleEndpointsShop; - break; - case 'legacy': - $endpoints = self::$endpointsLegacy; - $doubleEndpoints = self::$doubleEndpointsLegacy; - break; - default: - throw new \InvalidArgumentException(sprintf('The value of api_type should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($config['api_type'], \JSON_THROW_ON_ERROR))); - } + $endpoints = array_merge( + Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, + Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES, + ); return isset($config['type']) - && (\in_array($config['type'], $endpoints) || \in_array($config['type'], $doubleEndpoints)) + && \array_key_exists($config['type'], $endpoints) && isset($config['method']) && 'all' === $config['method']; } @@ -152,11 +33,11 @@ private function compileFilters(array ...$filters): Node\Expr $builder = new Sylius\Builder\Search(); foreach ($filters as $filter) { $builder->addFilter( - field: compileValue($this->interpreter, $filter['field']), - operator: compileValue($this->interpreter, $filter['operator']), - value: compileValue($this->interpreter, $filter['value']), - scope: \array_key_exists('scope', $filter) ? compileValue($this->interpreter, $filter['scope']) : null, - locale: \array_key_exists('locale', $filter) ? compileValue($this->interpreter, $filter['locale']) : null + field: compileValueWhenExpression($this->interpreter, $filter['field']), + operator: compileValueWhenExpression($this->interpreter, $filter['operator']), + value: compileValueWhenExpression($this->interpreter, $filter['value']), + scope: \array_key_exists('scope', $filter) ? compileValueWhenExpression($this->interpreter, $filter['scope']) : null, + locale: \array_key_exists('locale', $filter) ? compileValueWhenExpression($this->interpreter, $filter['locale']) : null ); } diff --git a/src/Capacity/Create.php b/src/Capacity/Create.php index 6205627..d3b314b 100644 --- a/src/Capacity/Create.php +++ b/src/Capacity/Create.php @@ -6,92 +6,20 @@ use Kiboko\Contract\Configurator\InvalidConfigurationException; use Kiboko\Plugin\Sylius; -use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; use PhpParser\Node; final class Create implements CapacityInterface { - private static array $endpointsLegacy = [ - // Core Endpoints - 'channels', - 'countries', - 'carts', - 'channels', - 'countries', - 'currencies', - 'customers', - 'exchangeRates', - 'locales', - 'orders', - 'payments', - 'paymentMethods', - 'products', - 'productAttributes', - 'productAssociationTypes', - 'productOptions', - 'productReviews', - 'productVariants', - 'promotions', - 'promotionCoupons', - 'shipments', - 'taxCategories', - 'taxRates', - 'taxons', - 'users', - 'zones', - ]; - - private static array $endpointsAdmin = [ - // Core Endpoints - 'administrator', - 'avatarImage', - 'catalogPromotion', - 'channel', - 'country', - 'currency', - 'customerGroup', - 'exchangeRate', - 'locale', - 'product', - 'productAssociationType', - 'productOption', - 'productVariant', - 'promotion', - 'resetPasswordRequest', - 'shippingCategory', - 'shippingMethod', - 'taxCategory', - 'taxon', - 'verifyCustomerAccount', - 'zone', - ]; - - private static array $endpointsShop = [ - // Core Endpoints - 'address', - 'customer', - 'order', - 'orderItem', - 'productReview', - 'resetPasswordRequest', - 'verifyCustomerAccount', - ]; - public function applies(array $config): bool { - if (!isset($config['api_type'])) { - throw new InvalidConfigurationException('Your Sylius API configuration is using some unsupported capacity, check your "api_type" properties to a suitable set.'); - } - $endpoints = match ($config['api_type']) { - 'admin' => self::$endpointsAdmin, - 'shop' => self::$endpointsShop, - 'legacy' => self::$endpointsLegacy, - default => throw new \InvalidArgumentException(sprintf('The value of api_type should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($config['api_type'], \JSON_THROW_ON_ERROR))) - }; + $endpoints = array_merge( + Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, + Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES, + ); return isset($config['type']) - && \in_array($config['type'], $endpoints) + && \array_key_exists($config['type'], $endpoints) && isset($config['method']) && 'create' === $config['method']; } diff --git a/src/Capacity/ListPerPage.php b/src/Capacity/ListPerPage.php index 084070b..3ab8dc4 100644 --- a/src/Capacity/ListPerPage.php +++ b/src/Capacity/ListPerPage.php @@ -4,9 +4,7 @@ namespace Kiboko\Plugin\Sylius\Capacity; -use Kiboko\Contract\Configurator\InvalidConfigurationException; use Kiboko\Plugin\Sylius; -use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; use PhpParser\Node; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; @@ -15,133 +13,17 @@ final class ListPerPage implements CapacityInterface { - private static array $endpointsLegacy = [ - // Simple resources Endpoints - 'channels', - 'countries', - 'carts', - 'channels', - 'countries', - 'currencies', - 'customers', - 'exchangeRates', - 'locales', - 'orders', - 'payments', - 'paymentMethods', - 'products', - 'productAttributes', - 'productAssociationTypes', - 'productOptions', - 'promotions', - 'shipments', - 'shippingCategories', - 'taxCategories', - 'taxRates', - 'taxons', - 'users', - 'zones', - ]; - - private static array $endpointsAdmin = [ - // Simple Ressource Endpoints - 'adjustment', - 'administrator', - 'catalogPromotion', - 'channel', - 'country', - 'currency', - 'customerGroup', - 'exchangeRate', - 'locale', - 'order', - 'payment', - 'product', - 'productAssociationType', - 'productImage', - 'productOption', - 'productOptionValue', - 'productReview', - 'productTaxon', - 'productVariant', - 'promotion', - 'province', - 'shipment', - 'shippingCategory', - 'shippingMethod', - 'ShopBillingData', - 'taxCategory', - 'taxon', - 'taxonTranslation', - 'zone', - 'zoneMember', - ]; - - private static array $endpointsShop = [ - // Simple Ressource Endpoints - 'address', - 'adjustment', - 'country', - 'currency', - 'locale', - 'order', - 'orderItem', - 'payment', - 'paymentMethod', - 'product', - 'productReview', - 'productVariant', - 'shipment', - 'shippingMethod', - 'taxon', - ]; - - private static array $doubleEndpointsLegacy = [ - // Double resources Endpoints - 'productReviews', - 'productVariants', - 'promotionCoupons', - ]; - private static array $doubleEndpointsAdmin = [ - // Double resources Endpoints - 'adjustment', - 'province', - 'shopBillingData', - 'zoneMember', - ]; - - private static array $doubleEndpointsShop = [ - // Double resources Endpoints - 'adjustment', - 'order', - ]; - public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { - if (!isset($config['api_type'])) { - throw new InvalidConfigurationException('Your Sylius API configuration is using some unsupported capacity, check your "api_type" properties to a suitable set.'); - } - switch ($config['api_type']) { - case 'admin': - $endpoints = self::$endpointsAdmin; - $doubleEndpoints = self::$doubleEndpointsAdmin; - break; - case 'shop': - $endpoints = self::$endpointsShop; - $doubleEndpoints = self::$doubleEndpointsShop; - break; - case 'legacy': - $endpoints = self::$endpointsLegacy; - $doubleEndpoints = self::$doubleEndpointsLegacy; - break; - default: - throw new \InvalidArgumentException(sprintf('The value of api_type should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($config['api_type'], \JSON_THROW_ON_ERROR))); - } + $endpoints = array_merge( + Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, + Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES, + ); return isset($config['type']) - && (\in_array($config['type'], $endpoints) || \in_array($config['type'], $doubleEndpoints)) + && \array_key_exists($config['type'], $endpoints) && isset($config['method']) && 'listPerPage' === $config['method']; } diff --git a/src/Capacity/Upsert.php b/src/Capacity/Upsert.php index 5ff97c9..17e7ed3 100644 --- a/src/Capacity/Upsert.php +++ b/src/Capacity/Upsert.php @@ -4,86 +4,21 @@ namespace Kiboko\Plugin\Sylius\Capacity; -use Kiboko\Contract\Configurator\InvalidConfigurationException; use Kiboko\Plugin\Sylius; -use Kiboko\Plugin\Sylius\Validator\ApiType; use PhpParser\Builder; use PhpParser\Node; final class Upsert implements CapacityInterface { - private static array $endpointsLegacy = [ - // Core Endpoints - 'carts', - 'channels', - 'countries', - 'carts', - 'channels', - 'countries', - 'currencies', - 'customers', - 'exchangeRates', - 'locales', - 'orders', - 'paymentMethods', - 'payments', - 'products', - 'productAttributes', - 'productAssociationTypes', - 'productOptions', - 'productReviews', - 'productVariants', - 'promotions', - 'promotionCoupons', - 'shipments', - 'shippingCategories', - 'taxCategories', - 'taxRates', - 'taxons', - 'users', - 'zones', - ]; - - private static array $endpointsAdmin = [ - // Core Endpoints - 'administrator', - 'catalogPromotion', - 'customerGroup', - 'exchangeRate', - 'product', - 'productAssociationType', - 'productOption', - 'productReview', - 'productVariant', - 'province', - 'shippingCategory', - 'shippingMethod', - 'taxCategory', - 'taxon', - 'zone', - ]; - - private static array $endpointsShop = [ - // Core Endpoints - 'address', - 'customer', - 'order', - ]; - public function applies(array $config): bool { - if (!isset($config['api_type'])) { - throw new InvalidConfigurationException('Your Sylius API configuration is using some unsupported capacity, check your "api_type" properties to a suitable set.'); - } - $endpoints = match ($config['api_type']) { - 'admin' => self::$endpointsAdmin, - 'shop' => self::$endpointsShop, - 'legacy' => self::$endpointsLegacy, - default => throw new \InvalidArgumentException(sprintf('The value of api_type should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($config['api_type'], \JSON_THROW_ON_ERROR))) - }; + $endpoints = array_merge( + Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, + Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES, + ); return isset($config['type']) - && \in_array($config['type'], $endpoints) + && \array_key_exists($config['type'], $endpoints) && isset($config['method']) && 'upsert' === $config['method']; } diff --git a/src/Configuration.php b/src/Configuration.php index a0b0315..6d61bc2 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -4,7 +4,10 @@ namespace Kiboko\Plugin\Sylius; +use Kiboko\Contract\Configurator\InvalidConfigurationException; use Kiboko\Contract\Configurator\PluginConfigurationInterface; +use Kiboko\Plugin\Sylius\Validator\ExtractorConfigurationValidator; +use Kiboko\Plugin\Sylius\Validator\LoaderConfigurationValidator; use Symfony\Component\Config\Definition\Builder\TreeBuilder; final class Configuration implements PluginConfigurationInterface @@ -20,13 +23,33 @@ public function getConfigTreeBuilder(): TreeBuilder /* @phpstan-ignore-next-line */ $builder->getRootNode() ->validate() - ->ifTrue(fn (array $value) => \array_key_exists('extractor', $value) && \array_key_exists('loader', $value)) + ->ifTrue(fn (array $value) => \array_key_exists('extractor', $value) && \array_key_exists('loader', $value)) ->thenInvalid('Your configuration should either contain the "extractor" or the "loader" key, not both.') ->end() + ->validate() + ->always(function (array $value) { + if (\array_key_exists('extractor', $value)) { + ExtractorConfigurationValidator::validate($value['extractor']['type'], $value['extractor']['method'], $value['version']); + } + + if (\array_key_exists('loader', $value)) { + LoaderConfigurationValidator::validate($value['loader']['type'], $value['loader']['method'], $value['version']); + } + + return $value; + }) + ->end() ->children() ->arrayNode('expression_language') ->scalarPrototype()->end() ->end() + ->scalarNode('version') + ->isRequired() + ->validate() + ->ifNotInArray(['admin', 'shop']) + ->thenInvalid('Invalid version %s') + ->end() + ->end() ->append(node: $extractor->getConfigTreeBuilder()->getRootNode()) ->append(node: $loader->getConfigTreeBuilder()->getRootNode()) ->append(node: $client->getConfigTreeBuilder()->getRootNode()) diff --git a/src/Configuration/Client.php b/src/Configuration/Client.php index 7f01cd6..7ddbfc4 100644 --- a/src/Configuration/Client.php +++ b/src/Configuration/Client.php @@ -18,55 +18,24 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui /* @phpstan-ignore-next-line */ $builder->getRootNode() ->validate() - ->ifArray() - ->then(function (array $value) { - if (isset($value['username']) && !isset($value['password'])) { - throw new Config\Definition\Exception\InvalidConfigurationException('The configuration option "password" should be defined if you use the username authentication method for Sylius API.'); - } - if (isset($value['token']) && !isset($value['refresh_token'])) { - throw new Config\Definition\Exception\InvalidConfigurationException('The configuration option "refreshToken" should be defined if you use the token authentication method for Sylius API.'); - } - if (isset($value['username'], $value['token']) || (!isset($value['username']) && !isset($value['token'])) - ) { - throw new Config\Definition\Exception\InvalidConfigurationException('You must choose between "username" and "token" as authentication method for Sylius API, both are mutually exclusive.'); - } - - return $value; - }) + ->ifTrue(function ($v) { + return !empty($v['token']) && (!empty($v['username']) || !empty($v['password'])); + }) + ->thenInvalid('You cannot specify both a token and a username/password combination.') + ->end() + ->validate() + ->ifTrue(function ($v) { + return (!empty($v['username']) && empty($v['password'])) || (empty($v['username']) && !empty($v['password'])); + }) + ->thenInvalid('Both username and password must be defined together.') + ->end() + ->validate() + ->ifTrue(function ($v) { + return empty($v['token']) && (empty($v['username']) || empty($v['password'])); + }) + ->thenInvalid('You must specify either a token or a username and password combination.') ->end() ->children() - ->arrayNode('context') - ->children() - ->scalarNode('http_client') - ->cannotBeEmpty() - ->validate() - ->ifTrue(isExpression()) - ->then(asExpression()) - ->end() - ->end() - ->scalarNode('http_request_factory') - ->cannotBeEmpty() - ->validate() - ->ifTrue(isExpression()) - ->then(asExpression()) - ->end() - ->end() - ->scalarNode('http_stream_factory') - ->cannotBeEmpty() - ->validate() - ->ifTrue(isExpression()) - ->then(asExpression()) - ->end() - ->end() - ->scalarNode('filesystem') - ->cannotBeEmpty() - ->validate() - ->ifTrue(isExpression()) - ->then(asExpression()) - ->end() - ->end() - ->end() - ->end() ->scalarNode('api_url') ->isRequired() ->cannotBeEmpty() @@ -75,20 +44,6 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui ->then(asExpression()) ->end() ->end() - ->scalarNode('client_id') - ->cannotBeEmpty() - ->validate() - ->ifTrue(isExpression()) - ->then(asExpression()) - ->end() - ->end() - ->scalarNode('secret') - ->cannotBeEmpty() - ->validate() - ->ifTrue(isExpression()) - ->then(asExpression()) - ->end() - ->end() ->scalarNode('username') ->cannotBeEmpty() ->validate() @@ -110,13 +65,6 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui ->then(asExpression()) ->end() ->end() - ->scalarNode('refresh_token') - ->cannotBeEmpty() - ->validate() - ->ifTrue(isExpression()) - ->then(asExpression()) - ->end() - ->end() ->end() ; diff --git a/src/Configuration/Extractor.php b/src/Configuration/Extractor.php index 05b9b77..81a5de9 100644 --- a/src/Configuration/Extractor.php +++ b/src/Configuration/Extractor.php @@ -20,36 +20,17 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui /* @phpstan-ignore-next-line */ $builder->getRootNode() - ->validate() - ->always(fn (array $item) => ExtractorConfigurationValidator::validate($item)) // store the item value - ->end() ->children() - ->scalarNode('api_type') - ->isRequired() - ->cannotBeEmpty() - ->validate() - ->always(fn (string $item) => ExtractorConfigurationValidator::validateApiType($item)) // check index of the item value - ->end() - ->end() ->scalarNode('type') ->isRequired() ->cannotBeEmpty() - ->validate() - ->always(fn (string $item) => ExtractorConfigurationValidator::validateType($item)) // check index of the item value - ->end() ->end() ->scalarNode('method') ->isRequired() ->cannotBeEmpty() - ->validate() - ->always(fn (string $item) => ExtractorConfigurationValidator::validateMethod($item)) // check index of the item value - ->end() ->end() ->scalarNode('code') ->cannotBeEmpty() - ->validate() - ->always(fn (string $item) => ExtractorConfigurationValidator::validateCode($item)) // check index of the item value - ->end() ->validate() ->ifTrue(isExpression()) ->then(asExpression()) diff --git a/src/Configuration/Loader.php b/src/Configuration/Loader.php index 4d22faa..1c45a0d 100644 --- a/src/Configuration/Loader.php +++ b/src/Configuration/Loader.php @@ -16,26 +16,13 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui /* @phpstan-ignore-next-line */ $builder->getRootNode() ->children() - ->scalarNode('api_type') - ->isRequired() - ->cannotBeEmpty() - ->validate() - ->always(fn (string $item) => LoaderConfigurationValidator::validateApiType($item)) - ->end() - ->end() ->scalarNode('type') ->isRequired() ->cannotBeEmpty() - ->validate() - ->always(fn (string $item) => LoaderConfigurationValidator::validateType($item)) - ->end() ->end() ->scalarNode('method') ->isRequired() ->cannotBeEmpty() - ->validate() - ->always(fn (string $item) => LoaderConfigurationValidator::validateMethod($item)) - ->end() ->end() ->end() ; diff --git a/src/Factory/Client.php b/src/Factory/Client.php index 3bbcdb3..87c1cf6 100644 --- a/src/Factory/Client.php +++ b/src/Factory/Client.php @@ -19,8 +19,10 @@ private Processor $processor; private ConfigurationInterface $configuration; - public function __construct(private ExpressionLanguage $interpreter) - { + public function __construct( + private ExpressionLanguage $interpreter, + private Sylius\ApiType $type = Sylius\ApiType::ADMIN, + ) { $this->processor = new Processor(); $this->configuration = new Sylius\Configuration\Client(); } @@ -74,40 +76,44 @@ public function compile(array $config): Repository\Client compileValueWhenExpression($this->interpreter, $config['api_url']), ); - if (isset($config['context'])) { - if (isset($config['context']['http_client'])) { - $clientBuilder->withHttpClient($this->buildFactoryNode($config['context']['http_client'])); - } - if (isset($config['context']['http_request_factory'])) { - $clientBuilder->withHttpRequestFactory($this->buildFactoryNode($config['context']['http_request_factory'])); - } - if (isset($config['context']['http_stream_factory'])) { - $clientBuilder->withHttpStreamFactory($this->buildFactoryNode($config['context']['http_stream_factory'])); - } - if (isset($config['context']['filesystem'])) { - $clientBuilder->withFileSystem($this->buildFactoryNode($config['context']['filesystem'])); - } - } - - if (isset($config['api_type'])) { - $clientBuilder->withApiType($config['api_type']); - } +// if (isset($config['context'])) { +// if (isset($config['context']['http_client'])) { +// $clientBuilder->withHttpClient($this->buildFactoryNode($config['context']['http_client'])); +// } +// if (isset($config['context']['http_request_factory'])) { +// $clientBuilder->withHttpRequestFactory($this->buildFactoryNode($config['context']['http_request_factory'])); +// } +// if (isset($config['context']['http_stream_factory'])) { +// $clientBuilder->withHttpStreamFactory($this->buildFactoryNode($config['context']['http_stream_factory'])); +// } +// if (isset($config['context']['filesystem'])) { +// $clientBuilder->withFileSystem($this->buildFactoryNode($config['context']['filesystem'])); +// } +// } + + $clientBuilder->withClientBuilder( + new Node\Expr\New_( + new Node\Name\FullyQualified( + $this->type == Sylius\ApiType::ADMIN ? 'Diglin\\Sylius\\ApiClient\\SyliusAdminClientBuilder' : 'Diglin\\Sylius\\ApiClient\\SyliusShopClientBuilder' + ), + ) + ); - if (isset($config['client_id']) && isset($config['secret'])) { - if (isset($config['api_type']) && $config['api_type'] === Sylius\Validator\ApiType::LEGACY->value) { - $clientBuilder->withSecret( - compileValueWhenExpression($this->interpreter, $config['client_id']), - compileValueWhenExpression($this->interpreter, $config['secret']) - ); - } - } +// if (isset($config['client_id']) && isset($config['secret'])) { +// if (isset($config['api_type']) && $config['api_type'] === Sylius\Validator\ApiType::LEGACY->value) { +// $clientBuilder->withSecret( +// compileValueWhenExpression($this->interpreter, $config['client_id']), +// compileValueWhenExpression($this->interpreter, $config['secret']) +// ); +// } +// } - if (isset($config['password'])) { + if (isset($config['username']) && isset($config['password'])) { $clientBuilder->withPassword( compileValueWhenExpression($this->interpreter, $config['username']), compileValueWhenExpression($this->interpreter, $config['password']), ); - } elseif (isset($config['refresh_token'])) { + } elseif (isset($config['token'])) { $clientBuilder->withToken( compileValueWhenExpression($this->interpreter, $config['token']), compileValueWhenExpression($this->interpreter, $config['refresh_token']), diff --git a/src/Service.php b/src/Service.php index d8d5aa8..47c6938 100644 --- a/src/Service.php +++ b/src/Service.php @@ -10,6 +10,7 @@ use Symfony\Component\Config\Definition\Exception as Symfony; use Symfony\Component\Config\Definition\Processor; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; +use PhpParser\Node; #[Configurator\Pipeline( name: 'sylius', @@ -89,11 +90,11 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep $extractor = $extractorFactory->compile($config['extractor']); $extractorBuilder = $extractor->getBuilder(); - $config['client']['api_type'] = $config['extractor']['api_type']; $client = $clientFactory->compile($config['client']); - $extractorBuilder->withClient($client->getBuilder()->getNode()); - $extractorBuilder->withApiType($config['extractor']['api_type']); + $extractorBuilder + ->withClientType(ApiType::from($config['version']) == ApiType::ADMIN ? new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusAdminClientInterface') : new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusShopClientInterface')) + ->withClient($client->getBuilder()->getNode()); $extractor->merge($client); @@ -105,11 +106,11 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep $loader = $loaderFactory->compile($config['loader']); $loaderBuilder = $loader->getBuilder(); - $config['client']['api_type'] = $config['loader']['api_type']; $client = $clientFactory->compile($config['client']); - $loaderBuilder->withClient($client->getBuilder()->getNode()); - $loaderBuilder->withApiType($config['loader']['api_type']); + $loaderBuilder + ->withClientType(ApiType::from($config['version']) == ApiType::ADMIN ? new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusAdminClientInterface') : new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusShopClientInterface')) + ->withClient($client->getBuilder()->getNode()); $loader->merge($client); diff --git a/src/Validator/ApiType.php b/src/Validator/ApiType.php deleted file mode 100644 index c0304eb..0000000 --- a/src/Validator/ApiType.php +++ /dev/null @@ -1,22 +0,0 @@ -value; - } - - return $apiTypeCases; - } -} diff --git a/src/Validator/ConfigurationValidatorInterface.php b/src/Validator/ConfigurationValidatorInterface.php deleted file mode 100644 index 13aa474..0000000 --- a/src/Validator/ConfigurationValidatorInterface.php +++ /dev/null @@ -1,10 +0,0 @@ - [ - 'listPerPage', - 'all', - 'get', - ], - 'countries' => [ - 'listPerPage', - 'all', - 'get', - ], - 'carts' => [ - 'listPerPage', - 'all', - 'get', - ], - 'currencies' => [ - 'listPerPage', - 'all', - 'get', - ], - 'customers' => [ - 'listPerPage', - 'all', - 'get', - ], - 'exchangeRates' => [ - 'listPerPage', - 'all', - 'get', - ], - 'locales' => [ - 'listPerPage', - 'all', - 'get', - ], - 'orders' => [ - 'listPerPage', - 'all', - 'get', - ], - 'payments' => [ - 'listPerPage', - 'all', - 'get', - ], - 'paymentMethods' => [ - 'listPerPage', - 'all', - 'get', - ], - 'products' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productAttributes' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productAssociationTypes' => [ - 'listPerPage', - 'all', - 'get', - ], - 'productOptions' => [ - 'listPerPage', - 'all', - 'get', - ], - 'promotions' => [ - 'listPerPage', - 'all', - 'get', - ], - 'shipments' => [ - 'listPerPage', - 'all', - 'get', - ], - 'shippingCategories' => [ - 'listPerPage', - 'all', + public const ADMIN_VALID_TYPES = [ + 'address' => [ 'get', ], - 'taxCategories' => [ - 'listPerPage', - 'all', + 'adjustment' => [ 'get', ], - 'taxRates' => [ - 'listPerPage', + 'administrator' => [ 'all', 'get', ], - 'taxons' => [ - 'listPerPage', - 'all', + 'catalogPromotionTranslation' => [ 'get', ], - 'users' => [ - 'listPerPage', + 'catalogPromotion' => [ 'all', 'get', ], - 'zones' => [ - 'listPerPage', + 'channel' => [ 'all', 'get', ], - ]; - - private static array $endpointsAdmin = [ - 'address' => [ + 'shopBillingData' => [ 'get', ], - 'adjustment' => [ - 'listPerPage', + 'country' => [ 'all', 'get', ], - 'administrator' => [ - 'listPerPage', + 'province' => [ 'all', 'get', ], - 'avatarImage' => [ - 'get', - ], - 'catalogPromotion' => [ - 'listPerPage', + 'currency' => [ 'all', 'get', ], - 'catalogPromotionTranslation' => [ - 'get', - ], - 'channel' => [ - 'listPerPage', + 'customerGroups' => [ 'all', 'get', ], - 'country' => [ - 'listPerPage', + 'customer' => [ 'all', 'get', ], - 'currency' => [ - 'listPerPage', + 'exchangeRate' => [ 'all', 'get', ], - 'customer' => [ + 'gatewayConfiguration' => [ 'get', ], - 'customerGroup' => [ - 'listPerPage', + 'locale' => [ 'all', 'get', ], - 'exchangeRate' => [ - 'listPerPage', - 'all', + 'orderItemUnit' => [ 'get', ], - 'locale' => [ - 'listPerPage', - 'all', + 'orderItem' => [ 'get', ], 'order' => [ - 'listPerPage', 'all', 'get', - 'listPaymentsPerPage', - 'allPayments', - 'listShipmentsPerPage', - 'allShipments', ], - 'orderItem' => [ - 'get', - ], - 'orderItemUnit' => [ + 'paymentMethod' => [ + 'all' , 'get', ], 'payment' => [ - 'listPerPage', 'all', 'get', ], - 'paymentMethod' => [ - 'get', - ], - 'product' => [ - 'listPerPage', + 'shipment' => [ 'all', 'get', ], 'productAssociationType' => [ - 'listPerPage', 'all', 'get', ], - 'productAssociationTypeTranslation' => [ - 'get', - ], - 'productImage' => [ - 'listPerPage', + 'productAttribute' => [ 'all', 'get', ], 'productOption' => [ - 'listPerPage', - 'all', - 'get', - 'listValuesPerPage', - 'allValues', - ], - 'productOptionTranslation' => [ - 'get', - ], - 'productOptionValue' => [ - 'listPerPage', 'all', 'get', ], 'productReview' => [ - 'listPerPage', 'all', 'get', ], 'productTaxon' => [ - 'listPerPage', - 'all', + 'all' , 'get', ], 'productTranslation' => [ 'get', ], + 'productVariantTranslation' => [ + 'get', + ], 'productVariant' => [ - 'listPerPage', 'all', 'get', ], - 'productVariantTranslation' => [ - 'get', + 'product' => [ + 'all', + 'get' ], - 'promotion' => [ - 'listPerPage', + 'promotionCoupon' => [ 'all', 'get', ], - 'province' => [ - 'listPerPage', - 'all', + 'promotionTranslation' => [ 'get', ], - 'shipment' => [ - 'listPerPage', + 'promotion' => [ 'all', 'get', ], 'shippingCategory' => [ - 'listPerPage', 'all', 'get', ], - 'shippingMethod' => [ - 'listPerPage', - 'all', + 'shippingMethodTranslation' => [ 'get', ], - 'ShopBillingData' => [ - 'listPerPage', + 'shippingMethod' => [ 'all', 'get', ], 'taxCategory' => [ - 'listPerPage', 'all', 'get', ], - 'taxon' => [ - 'listPerPage', + 'taxRate' => [ 'all', 'get', ], - 'taxonTranslation' => [ - 'listPerPage', + 'taxonImage' => [ 'all', 'get', ], - 'zone' => [ - 'listPerPage', + 'taxon' => [ 'all', 'get', ], 'zoneMember' => [ - 'listPerPage', + 'get', + ], + 'zone' => [ 'all', 'get', ], ]; - private static array $endpointsShop = [ - // Core Endpoints + public const SHOP_VALID_TYPES = [ 'address' => [ - 'listPerPage', 'all', 'get', ], 'adjustment' => [ - 'listPerPage', - 'all', 'get', ], 'catalogPromotion' => [ 'get', ], 'channel' => [ + 'all', 'get', ], 'country' => [ - 'listPerPage', 'all', 'get', ], + 'province' => [ + 'get', + ], 'currency' => [ - 'listPerPage', 'all', 'get', ], 'customer' => [ 'get', ], - 'locale' => [ - 'listPerPage', + 'exchangeRate' => [ 'all', 'get', ], - 'order' => [ - 'listPerPage', + 'locale' => [ 'all', 'get', - 'listPaymentMethodsPerPage', - 'allPaymentMethods', - 'listShipmentMethodsPerPage', - 'allShipmentMethods', - 'listAdjustmentsPerPage', - 'allAdjustments', - 'listItemsPerPage', - 'allItems', ], - 'orderItem' => [ - 'listPerPage', - 'all', + 'orderItemUnit' => [ 'get', - 'listAdjustmentsPerPage', - 'allAdjustments', ], - 'orderItemUnit' => [ + 'orderItem' => [ 'get', ], - 'payment' => [ - 'listPerPage', + 'order' => [ 'all', 'get', ], 'paymentMethod' => [ - 'listPerPage', - 'all', + 'all' , 'get', ], - 'product' => [ - 'listPerPage', - 'all', + 'payment' => [ 'get', - 'getBySlug', ], - 'productImage' => [ + 'shipment' => [ 'get', ], - 'productOption' => [ + 'productAssociationType' => [ + 'get', + ], + 'productAttribute' => [ 'get', ], - 'productOptionValue' => [ + 'productOption' => [ 'get', ], 'productReview' => [ - 'listPerPage', 'all', 'get', ], 'productTaxon' => [ 'get', ], - 'productTranslation' => [ - 'get', - ], 'productVariant' => [ - 'listPerPage', 'all', 'get', ], - 'productVariantTranslation' => [ - 'get', - ], - 'shipment' => [ - 'listPerPage', + 'product' => [ 'all', - 'get', + 'get' ], 'shippingMethod' => [ - 'listPerPage', 'all', 'get', ], - 'shippingMethodTranslation' => [ + 'taxonImage' => [ 'get', ], 'taxon' => [ - 'listPerPage', 'all', 'get', ], - 'taxonTranslation' => [ - 'get', - ], - ]; - - private static array $doubleEndpointsLegacy = [ - // Double resources Endpoints - 'productReviews', - 'productVariants', - 'promotionCoupons', ]; - private static array $doubleEndpointsAdmin = [ - // Double resources Endpoints - 'adjustment', - 'province', - 'shopBillingData', - 'zoneMember', - ]; - - private static array $doubleEndpointsShop = [ - // Double resources Endpoints - 'adjustment', - 'order', - ]; - - public static function getEndpointsApiType(): array - { - return match (self::$currentApiType) { - ApiType::ADMIN->value => self::$endpointsAdmin, - ApiType::SHOP->value => self::$endpointsShop, - ApiType::LEGACY->value => self::$endpointsLegacy, - default => throw new \UnhandledMatchError(self::$currentApiType) - }; - } - - public static function getDoubleEndpointsApiType(): array + public static function validate(string $type, string $method, string $version = 'admin'): void { - return match (self::$currentApiType) { - ApiType::ADMIN->value => self::$doubleEndpointsAdmin, - ApiType::SHOP->value => self::$doubleEndpointsShop, - ApiType::LEGACY->value => self::$doubleEndpointsLegacy, - default => throw new \UnhandledMatchError(self::$currentApiType) - }; - } - public static string $currentApiType; - public static string $currentType; + $validTypes = $version === 'admin' ? self::ADMIN_VALID_TYPES : self::SHOP_VALID_TYPES; - public static function validate(array $item): array - { - if (\in_array($item['type'], self::getDoubleEndpointsApiType()) && !\array_key_exists('code', $item)) { - throw new InvalidConfigurationException('The code parameters is required and cannot be empty because you choose a type: '.$item['type']); + if ($validTypes === null) { + throw new InvalidConfigurationException(sprintf('Unknown version "%s".', $version)); } - return $item; - } - - public static function validateApiType(string $apiType) - { - self::$currentApiType = $apiType; - if (!\in_array($apiType, ApiType::casesValue())) { - throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($apiType, \JSON_THROW_ON_ERROR))); + if (!array_key_exists($type, $validTypes)) { + throw new InvalidConfigurationException(sprintf( + 'Invalid extractor type "%s" for version "%s". Valid types are: %s.', + $type, + $version, + implode(', ', array_keys($validTypes)) + )); } - return $apiType; - } - - public static function validateType(string $type) - { - self::$currentType = $type; - $endpoints = self::getEndpointsApiType(); - $doubleEndpoints = self::getDoubleEndpointsApiType(); - if (!\in_array($type, array_merge(array_keys($endpoints), $doubleEndpoints))) { - throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', array_merge(array_keys($endpoints), $doubleEndpoints)), json_encode($type, \JSON_THROW_ON_ERROR))); + if (!in_array($method, $validTypes[$type], true)) { + throw new InvalidConfigurationException(sprintf( + 'Invalid method "%s" for extractor type "%s" and version "%s". Valid methods are: %s.', + $method, + $type, + $version, + implode(', ', $validTypes[$type]) + )); } - - return $type; - } - - public static function validateMethod(string $method) - { - $endpoints = self::getEndpointsApiType(); - $doubleEndpoints = self::getDoubleEndpointsApiType(); - if ( - \array_key_exists(self::$currentType, $endpoints) - && !\in_array($method, $endpoints[self::$currentType]) - && !\in_array(self::$currentType, $doubleEndpoints) - ) { - throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', $endpoints[self::$currentType]), json_encode($method, \JSON_THROW_ON_ERROR))); - } - - return $method; - } - - public static function validateCode(string $code) - { - $doubleEndpoints = self::getDoubleEndpointsApiType(); - if (\in_array(self::$currentType, $doubleEndpoints)) { - throw new \InvalidArgumentException(sprintf('The %s type should have a "code" field set.', self::$currentType), json_encode($code, \JSON_THROW_ON_ERROR)); - } - - return $code; } } diff --git a/src/Validator/LoaderConfigurationValidator.php b/src/Validator/LoaderConfigurationValidator.php index 04ef141..2ac6132 100644 --- a/src/Validator/LoaderConfigurationValidator.php +++ b/src/Validator/LoaderConfigurationValidator.php @@ -4,311 +4,209 @@ namespace Kiboko\Plugin\Sylius\Validator; -class LoaderConfigurationValidator implements ConfigurationValidatorInterface +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; + +class LoaderConfigurationValidator { - private static array $endpointsLegacy = [ - // Core Endpoints - 'channels' => [ - 'create', - 'delete', - ], - 'countries' => [ - 'create', - 'delete', - ], - 'carts' => [ - 'create', - 'delete', - ], - 'currencies' => [ - 'create', - 'delete', - ], - 'customers' => [ - 'create', - 'delete', - ], - 'exchangeRates' => [ - 'create', - 'delete', - ], - 'locales' => [ - 'create', - 'delete', - ], - 'orders' => [ - 'create', - 'delete', - ], - 'payments' => [ + public const ADMIN_VALID_TYPES = [ + 'address' => [ 'create', 'delete', + 'update', ], - 'paymentMethods' => [ + 'administrator' => [ 'create', 'delete', + 'update', ], - 'products' => [ + 'avatarImage' => [ 'create', - 'upsert', 'delete', ], - 'productAttributes' => [ + 'catalogPromotion' => [ 'create', + 'update', 'delete', ], - 'productAssociationTypes' => [ + 'channel' => [ 'create', + 'update', 'delete', ], - 'productOptions' => [ + 'country' => [ 'create', - 'delete', + 'update', ], - 'promotions' => [ - 'create', - 'delete', + 'province' => [ + 'update', ], - 'shipments' => [ + 'currency' => [ 'create', - 'delete', ], - 'shippingCategories' => [ + 'customerGroup' => [ 'create', 'delete', + 'update', ], - 'taxCategories' => [ + 'customer' => [ 'create', 'delete', + 'update', ], - 'taxRates' => [ + 'exchangeRate' => [ 'create', 'delete', + 'update', ], - 'taxons' => [ + 'locale' => [ 'create', 'delete', ], - 'users' => [ + 'paymentMethod' => [ 'create', 'delete', + 'update', ], - 'zones' => [ + 'productAssociationType' => [ 'create', 'delete', + 'update', ], - ]; - - private static array $endpointsAdmin = [ - // Core Endpoints - 'administrator' => [ + 'productAssociation' => [ 'create', 'delete', - 'upsert', + 'update', ], - 'avatarImage' => [ + 'productAttribute' => [ 'create', 'delete', + 'update', ], - 'catalogPromotion' => [ - 'create', - 'upsert', - ], - 'channel' => [ - 'create', + 'productImage' => [ 'delete', + 'update', ], - 'country' => [ + 'productOption' => [ 'create', + 'update', 'delete', ], - 'currency' => [ + 'productReview' => [ 'create', + 'update', 'delete', ], - 'customerGroup' => [ + 'productTaxon' => [ 'create', + 'update', 'delete', - 'upsert', ], - 'exchangeRate' => [ + 'productVariant' => [ 'create', + 'update', 'delete', - 'upsert', - ], - 'locale' => [ - 'create', - ], - 'order' => [ - 'cancel', - ], - 'payment' => [ - 'complete', ], 'product' => [ 'create', 'delete', - 'upsert', + 'update', ], - 'productAssociationType' => [ + 'promotionCoupon' => [ 'create', + 'update', 'delete', - 'upsert', - ], - 'productOption' => [ - 'create', - 'delete', - ], - 'productReview' => [ - 'upsert', - 'delete', - 'accept', - 'reject', - ], - 'productVariant' => [ - 'create', - 'upsert', ], 'promotion' => [ 'create', + 'update', 'delete', ], - 'province' => [ - 'upsert', - ], - 'resetPasswordRequest' => [ - 'create', - 'acknowledge', - ], - 'shipment' => [ - 'ship', - ], 'shippingCategory' => [ 'create', 'delete', - 'upsert', + 'update', ], 'shippingMethod' => [ 'create', 'delete', - 'upsert', - 'archive', - 'restore', + 'update', ], 'taxCategory' => [ 'create', 'delete', - 'upsert', + 'update', ], - 'taxon' => [ + 'taxRate' => [ 'create', - 'upsert', + 'delete', + 'update', + ], + 'taxonImage' => [ + 'delete', + 'update', ], - 'verifyCustomerAccount' => [ + 'taxon' => [ 'create', - 'acknowledge', + 'update', + 'delete', ], 'zone' => [ 'create', 'delete', - 'upsert', + 'update', ], ]; - private static array $endpointsShop = [ - // Core Endpoints + public const SHOP_VALID_TYPES = [ 'address' => [ 'create', 'delete', - 'upsert', + 'update', ], 'customer' => [ 'create', - 'upsert', - 'changePassword', + 'update', ], 'order' => [ 'create', - 'upsert', - 'choosePayment', - 'chooseShipment', - 'complete', + 'update', + 'delete', ], 'orderItem' => [ 'create', 'delete', - 'changeQuantity', ], 'productReview' => [ 'create', ], - 'resetPasswordRequest' => [ - 'create', - 'verify', - ], - 'verifyCustomerAccount' => [ - 'create', - 'verify', - ], ]; - public static string $currentApiType; - public static string $currentType; - - public static function getEndpointsApiType(): array - { - return match (self::$currentApiType) { - ApiType::ADMIN->value => self::$endpointsAdmin, - ApiType::SHOP->value => self::$endpointsShop, - ApiType::LEGACY->value => self::$endpointsLegacy, - default => throw new \UnhandledMatchError(self::$currentApiType), - }; - } - public static function validate(array $item): array + public static function validate(string $type, string $method, string $version = 'admin'): void { - $endpoints = self::getEndpointsApiType(); - if (!\in_array($item['type'], array_keys($endpoints))) { - throw new \InvalidArgumentException(sprintf('the value "type" should be one of [%s], got %s', implode(', ', array_keys($endpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR))); - } - if (!\in_array($item['method'], $endpoints[$item['type']])) { - throw new \InvalidArgumentException(sprintf('the value "method" should be one of [%s], got %s', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR))); - } + $validTypes = $version === 'admin' ? self::ADMIN_VALID_TYPES : self::SHOP_VALID_TYPES; - return $item; - } - - public static function validateApiType(string $apiType) - { - self::$currentApiType = $apiType; - if (!\in_array($apiType, ApiType::casesValue())) { - throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($apiType, \JSON_THROW_ON_ERROR))); + if ($validTypes === null) { + throw new InvalidConfigurationException(sprintf('Unknown version "%s".', $version)); } - return $apiType; - } - - public static function validateType(string $type) - { - self::$currentType = $type; - $endpoints = self::getEndpointsApiType(); - if (!\in_array($type, array_keys($endpoints))) { - throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', array_keys($endpoints)), json_encode($type, \JSON_THROW_ON_ERROR))); + if (!array_key_exists($type, $validTypes)) { + throw new InvalidConfigurationException(sprintf( + 'Invalid loader type "%s" for version "%s". Valid types are: %s.', + $type, + $version, + implode(', ', array_keys($validTypes)) + )); } - return $type; - } - - public static function validateMethod(string $method) - { - $endpoints = self::getEndpointsApiType(); - if ( - \array_key_exists(self::$currentType, $endpoints) - && !\in_array($method, $endpoints[self::$currentType]) - ) { - throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', $endpoints[self::$currentType]), json_encode($method, \JSON_THROW_ON_ERROR))); + if (!in_array($method, $validTypes[$type], true)) { + throw new InvalidConfigurationException(sprintf( + 'Invalid method "%s" for extractor type "%s" and version "%s". Valid methods are: %s.', + $method, + $type, + $version, + implode(', ', $validTypes[$type]) + )); } - - return $method; } } From f028844eeda455f45749de1e73ae23a7accb2a7a Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Mon, 8 Jul 2024 15:17:53 +0000 Subject: [PATCH 25/27] [rector] Rector fixes --- src/Capacity/All.php | 9 +++------ src/Capacity/Create.php | 5 +---- src/Capacity/ListPerPage.php | 9 +++------ src/Capacity/Upsert.php | 5 +---- src/Configuration/Client.php | 12 +++--------- src/Factory/Client.php | 2 +- src/Service.php | 4 ++-- src/Validator/ExtractorConfigurationValidator.php | 4 ++-- src/Validator/LoaderConfigurationValidator.php | 4 ++-- 9 files changed, 18 insertions(+), 36 deletions(-) diff --git a/src/Capacity/All.php b/src/Capacity/All.php index 57881e1..d1fd509 100644 --- a/src/Capacity/All.php +++ b/src/Capacity/All.php @@ -11,16 +11,13 @@ use function Kiboko\Component\SatelliteToolbox\Configuration\compileValueWhenExpression; -final class All implements CapacityInterface +final readonly class All implements CapacityInterface { - public function __construct(private readonly ExpressionLanguage $interpreter) {} + public function __construct(private ExpressionLanguage $interpreter) {} public function applies(array $config): bool { - $endpoints = array_merge( - Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, - Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES, - ); + $endpoints = [...Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, ...Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES]; return isset($config['type']) && \array_key_exists($config['type'], $endpoints) diff --git a/src/Capacity/Create.php b/src/Capacity/Create.php index d3b314b..0626939 100644 --- a/src/Capacity/Create.php +++ b/src/Capacity/Create.php @@ -13,10 +13,7 @@ final class Create implements CapacityInterface { public function applies(array $config): bool { - $endpoints = array_merge( - Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, - Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES, - ); + $endpoints = [...Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, ...Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES]; return isset($config['type']) && \array_key_exists($config['type'], $endpoints) diff --git a/src/Capacity/ListPerPage.php b/src/Capacity/ListPerPage.php index 3ab8dc4..ec13d11 100644 --- a/src/Capacity/ListPerPage.php +++ b/src/Capacity/ListPerPage.php @@ -11,16 +11,13 @@ use function Kiboko\Component\SatelliteToolbox\Configuration\compileValue; -final class ListPerPage implements CapacityInterface +final readonly class ListPerPage implements CapacityInterface { - public function __construct(private readonly ExpressionLanguage $interpreter) {} + public function __construct(private ExpressionLanguage $interpreter) {} public function applies(array $config): bool { - $endpoints = array_merge( - Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, - Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES, - ); + $endpoints = [...Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, ...Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES]; return isset($config['type']) && \array_key_exists($config['type'], $endpoints) diff --git a/src/Capacity/Upsert.php b/src/Capacity/Upsert.php index 17e7ed3..c524cc8 100644 --- a/src/Capacity/Upsert.php +++ b/src/Capacity/Upsert.php @@ -12,10 +12,7 @@ final class Upsert implements CapacityInterface { public function applies(array $config): bool { - $endpoints = array_merge( - Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, - Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES, - ); + $endpoints = [...Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, ...Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES]; return isset($config['type']) && \array_key_exists($config['type'], $endpoints) diff --git a/src/Configuration/Client.php b/src/Configuration/Client.php index 7ddbfc4..18c71bd 100644 --- a/src/Configuration/Client.php +++ b/src/Configuration/Client.php @@ -18,21 +18,15 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui /* @phpstan-ignore-next-line */ $builder->getRootNode() ->validate() - ->ifTrue(function ($v) { - return !empty($v['token']) && (!empty($v['username']) || !empty($v['password'])); - }) + ->ifTrue(fn($v) => !empty($v['token']) && (!empty($v['username']) || !empty($v['password']))) ->thenInvalid('You cannot specify both a token and a username/password combination.') ->end() ->validate() - ->ifTrue(function ($v) { - return (!empty($v['username']) && empty($v['password'])) || (empty($v['username']) && !empty($v['password'])); - }) + ->ifTrue(fn($v) => (!empty($v['username']) && empty($v['password'])) || (empty($v['username']) && !empty($v['password']))) ->thenInvalid('Both username and password must be defined together.') ->end() ->validate() - ->ifTrue(function ($v) { - return empty($v['token']) && (empty($v['username']) || empty($v['password'])); - }) + ->ifTrue(fn($v) => empty($v['token']) && (empty($v['username']) || empty($v['password']))) ->thenInvalid('You must specify either a token or a username and password combination.') ->end() ->children() diff --git a/src/Factory/Client.php b/src/Factory/Client.php index 87c1cf6..4016b57 100644 --- a/src/Factory/Client.php +++ b/src/Factory/Client.php @@ -94,7 +94,7 @@ public function compile(array $config): Repository\Client $clientBuilder->withClientBuilder( new Node\Expr\New_( new Node\Name\FullyQualified( - $this->type == Sylius\ApiType::ADMIN ? 'Diglin\\Sylius\\ApiClient\\SyliusAdminClientBuilder' : 'Diglin\\Sylius\\ApiClient\\SyliusShopClientBuilder' + $this->type == Sylius\ApiType::ADMIN ? \Diglin\Sylius\ApiClient\SyliusAdminClientBuilder::class : \Diglin\Sylius\ApiClient\SyliusShopClientBuilder::class ), ) ); diff --git a/src/Service.php b/src/Service.php index 47c6938..f0289a2 100644 --- a/src/Service.php +++ b/src/Service.php @@ -93,7 +93,7 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep $client = $clientFactory->compile($config['client']); $extractorBuilder - ->withClientType(ApiType::from($config['version']) == ApiType::ADMIN ? new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusAdminClientInterface') : new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusShopClientInterface')) + ->withClientType(ApiType::from($config['version']) == ApiType::ADMIN ? new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusAdminClientInterface::class) : new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusShopClientInterface::class)) ->withClient($client->getBuilder()->getNode()); $extractor->merge($client); @@ -109,7 +109,7 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep $client = $clientFactory->compile($config['client']); $loaderBuilder - ->withClientType(ApiType::from($config['version']) == ApiType::ADMIN ? new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusAdminClientInterface') : new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusShopClientInterface')) + ->withClientType(ApiType::from($config['version']) == ApiType::ADMIN ? new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusAdminClientInterface::class) : new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusShopClientInterface::class)) ->withClient($client->getBuilder()->getNode()); $loader->merge($client); diff --git a/src/Validator/ExtractorConfigurationValidator.php b/src/Validator/ExtractorConfigurationValidator.php index 7919040..f1ccb94 100644 --- a/src/Validator/ExtractorConfigurationValidator.php +++ b/src/Validator/ExtractorConfigurationValidator.php @@ -8,7 +8,7 @@ class ExtractorConfigurationValidator { - public const ADMIN_VALID_TYPES = [ + final public const ADMIN_VALID_TYPES = [ 'address' => [ 'get', ], @@ -167,7 +167,7 @@ class ExtractorConfigurationValidator ], ]; - public const SHOP_VALID_TYPES = [ + final public const SHOP_VALID_TYPES = [ 'address' => [ 'all', 'get', diff --git a/src/Validator/LoaderConfigurationValidator.php b/src/Validator/LoaderConfigurationValidator.php index 2ac6132..563e009 100644 --- a/src/Validator/LoaderConfigurationValidator.php +++ b/src/Validator/LoaderConfigurationValidator.php @@ -8,7 +8,7 @@ class LoaderConfigurationValidator { - public const ADMIN_VALID_TYPES = [ + final public const ADMIN_VALID_TYPES = [ 'address' => [ 'create', 'delete', @@ -157,7 +157,7 @@ class LoaderConfigurationValidator ], ]; - public const SHOP_VALID_TYPES = [ + final public const SHOP_VALID_TYPES = [ 'address' => [ 'create', 'delete', From 9d0ccf1572dfb1fe013d8c38f92b5c5d9e5f6109 Mon Sep 17 00:00:00 2001 From: sebprt Date: Mon, 8 Jul 2024 17:20:30 +0200 Subject: [PATCH 26/27] Ran php-cs-fixer --- src/ApiType.php | 2 + src/Builder/Capacity/All.php | 8 ++-- src/Builder/Capacity/Create.php | 8 ++-- src/Builder/Capacity/ListPerPage.php | 8 ++-- src/Builder/Capacity/Upsert.php | 8 ++-- src/Builder/Client.php | 7 +-- src/Builder/Extractor.php | 3 +- src/Builder/Loader.php | 5 +- src/Builder/Search.php | 9 ++-- src/Capacity/All.php | 10 +++- src/Capacity/Create.php | 1 - src/Capacity/ListPerPage.php | 10 +++- src/Capacity/Upsert.php | 5 +- src/Configuration.php | 1 - src/Configuration/Client.php | 8 ++-- src/Configuration/Extractor.php | 3 +- src/Configuration/Loader.php | 3 +- src/Configuration/Search.php | 2 +- src/Factory/Client.php | 48 +++++++++---------- src/Factory/NoApplicableCapacityException.php | 4 +- src/MissingAuthenticationMethodException.php | 4 +- src/MissingEndpointException.php | 4 +- src/MissingParameterException.php | 4 +- src/Service.php | 12 +++-- .../ExtractorConfigurationValidator.php | 33 +++++-------- .../LoaderConfigurationValidator.php | 24 +++------- 26 files changed, 116 insertions(+), 118 deletions(-) diff --git a/src/ApiType.php b/src/ApiType.php index df140cb..8be5d1d 100644 --- a/src/ApiType.php +++ b/src/ApiType.php @@ -1,5 +1,7 @@ getRootNode() ->validate() - ->ifTrue(fn($v) => !empty($v['token']) && (!empty($v['username']) || !empty($v['password']))) + ->ifTrue(fn ($value) => !empty($value['token']) && (!empty($value['username']) || !empty($value['password']))) ->thenInvalid('You cannot specify both a token and a username/password combination.') ->end() ->validate() - ->ifTrue(fn($v) => (!empty($v['username']) && empty($v['password'])) || (empty($v['username']) && !empty($v['password']))) + ->ifTrue(fn ($value) => (!empty($value['username']) && empty($value['password'])) || (empty($value['username']) && !empty($value['password']))) ->thenInvalid('Both username and password must be defined together.') ->end() ->validate() - ->ifTrue(fn($v) => empty($v['token']) && (empty($v['username']) || empty($v['password']))) + ->ifTrue(fn ($value) => empty($value['token']) && (empty($value['username']) || empty($value['password']))) ->thenInvalid('You must specify either a token or a username and password combination.') ->end() ->children() diff --git a/src/Configuration/Extractor.php b/src/Configuration/Extractor.php index 81a5de9..e491b82 100644 --- a/src/Configuration/Extractor.php +++ b/src/Configuration/Extractor.php @@ -4,7 +4,6 @@ namespace Kiboko\Plugin\Sylius\Configuration; -use Kiboko\Plugin\Sylius\Validator\ExtractorConfigurationValidator; use Symfony\Component\Config; use function Kiboko\Component\SatelliteToolbox\Configuration\asExpression; @@ -12,7 +11,7 @@ final class Extractor implements Config\Definition\ConfigurationInterface { - public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder + public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder { $filters = new Search(); diff --git a/src/Configuration/Loader.php b/src/Configuration/Loader.php index 1c45a0d..74c812e 100644 --- a/src/Configuration/Loader.php +++ b/src/Configuration/Loader.php @@ -4,12 +4,11 @@ namespace Kiboko\Plugin\Sylius\Configuration; -use Kiboko\Plugin\Sylius\Validator\LoaderConfigurationValidator; use Symfony\Component\Config; final class Loader implements Config\Definition\ConfigurationInterface { - public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder + public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder { $builder = new Config\Definition\Builder\TreeBuilder('loader'); diff --git a/src/Configuration/Search.php b/src/Configuration/Search.php index b03d484..0ab9dc7 100644 --- a/src/Configuration/Search.php +++ b/src/Configuration/Search.php @@ -11,7 +11,7 @@ final class Search implements Config\Definition\ConfigurationInterface { - public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder + public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder { $builder = new Config\Definition\Builder\TreeBuilder('search'); diff --git a/src/Factory/Client.php b/src/Factory/Client.php index 4016b57..98877a6 100644 --- a/src/Factory/Client.php +++ b/src/Factory/Client.php @@ -76,39 +76,39 @@ public function compile(array $config): Repository\Client compileValueWhenExpression($this->interpreter, $config['api_url']), ); -// if (isset($config['context'])) { -// if (isset($config['context']['http_client'])) { -// $clientBuilder->withHttpClient($this->buildFactoryNode($config['context']['http_client'])); -// } -// if (isset($config['context']['http_request_factory'])) { -// $clientBuilder->withHttpRequestFactory($this->buildFactoryNode($config['context']['http_request_factory'])); -// } -// if (isset($config['context']['http_stream_factory'])) { -// $clientBuilder->withHttpStreamFactory($this->buildFactoryNode($config['context']['http_stream_factory'])); -// } -// if (isset($config['context']['filesystem'])) { -// $clientBuilder->withFileSystem($this->buildFactoryNode($config['context']['filesystem'])); -// } -// } + // if (isset($config['context'])) { + // if (isset($config['context']['http_client'])) { + // $clientBuilder->withHttpClient($this->buildFactoryNode($config['context']['http_client'])); + // } + // if (isset($config['context']['http_request_factory'])) { + // $clientBuilder->withHttpRequestFactory($this->buildFactoryNode($config['context']['http_request_factory'])); + // } + // if (isset($config['context']['http_stream_factory'])) { + // $clientBuilder->withHttpStreamFactory($this->buildFactoryNode($config['context']['http_stream_factory'])); + // } + // if (isset($config['context']['filesystem'])) { + // $clientBuilder->withFileSystem($this->buildFactoryNode($config['context']['filesystem'])); + // } + // } $clientBuilder->withClientBuilder( new Node\Expr\New_( new Node\Name\FullyQualified( - $this->type == Sylius\ApiType::ADMIN ? \Diglin\Sylius\ApiClient\SyliusAdminClientBuilder::class : \Diglin\Sylius\ApiClient\SyliusShopClientBuilder::class + Sylius\ApiType::ADMIN == $this->type ? 'Diglin\\Sylius\\ApiClient\\SyliusAdminClientBuilder' : 'Diglin\\Sylius\\ApiClient\\SyliusShopClientBuilder' ), ) ); -// if (isset($config['client_id']) && isset($config['secret'])) { -// if (isset($config['api_type']) && $config['api_type'] === Sylius\Validator\ApiType::LEGACY->value) { -// $clientBuilder->withSecret( -// compileValueWhenExpression($this->interpreter, $config['client_id']), -// compileValueWhenExpression($this->interpreter, $config['secret']) -// ); -// } -// } + // if (isset($config['client_id']) && isset($config['secret'])) { + // if (isset($config['api_type']) && $config['api_type'] === Sylius\Validator\ApiType::LEGACY->value) { + // $clientBuilder->withSecret( + // compileValueWhenExpression($this->interpreter, $config['client_id']), + // compileValueWhenExpression($this->interpreter, $config['secret']) + // ); + // } + // } - if (isset($config['username']) && isset($config['password'])) { + if (isset($config['username'], $config['password'])) { $clientBuilder->withPassword( compileValueWhenExpression($this->interpreter, $config['username']), compileValueWhenExpression($this->interpreter, $config['password']), diff --git a/src/Factory/NoApplicableCapacityException.php b/src/Factory/NoApplicableCapacityException.php index 2922447..1ab18bc 100644 --- a/src/Factory/NoApplicableCapacityException.php +++ b/src/Factory/NoApplicableCapacityException.php @@ -4,4 +4,6 @@ namespace Kiboko\Plugin\Sylius\Factory; -final class NoApplicableCapacityException extends \OutOfRangeException {} +final class NoApplicableCapacityException extends \OutOfRangeException +{ +} diff --git a/src/MissingAuthenticationMethodException.php b/src/MissingAuthenticationMethodException.php index 8389a55..577827b 100644 --- a/src/MissingAuthenticationMethodException.php +++ b/src/MissingAuthenticationMethodException.php @@ -4,4 +4,6 @@ namespace Kiboko\Plugin\Sylius; -final class MissingAuthenticationMethodException extends \RuntimeException {} +final class MissingAuthenticationMethodException extends \RuntimeException +{ +} diff --git a/src/MissingEndpointException.php b/src/MissingEndpointException.php index 847c93b..ea86064 100644 --- a/src/MissingEndpointException.php +++ b/src/MissingEndpointException.php @@ -4,4 +4,6 @@ namespace Kiboko\Plugin\Sylius; -final class MissingEndpointException extends \RuntimeException {} +final class MissingEndpointException extends \RuntimeException +{ +} diff --git a/src/MissingParameterException.php b/src/MissingParameterException.php index 5119d4b..2cb38d5 100644 --- a/src/MissingParameterException.php +++ b/src/MissingParameterException.php @@ -4,4 +4,6 @@ namespace Kiboko\Plugin\Sylius; -final class MissingParameterException extends \UnexpectedValueException {} +final class MissingParameterException extends \UnexpectedValueException +{ +} diff --git a/src/Service.php b/src/Service.php index f0289a2..cdb1acf 100644 --- a/src/Service.php +++ b/src/Service.php @@ -7,10 +7,10 @@ use Kiboko\Contract\Configurator; use Kiboko\Contract\Configurator\ConfigurationExceptionInterface; use Kiboko\Contract\Configurator\InvalidConfigurationException; +use PhpParser\Node; use Symfony\Component\Config\Definition\Exception as Symfony; use Symfony\Component\Config\Definition\Processor; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; -use PhpParser\Node; #[Configurator\Pipeline( name: 'sylius', @@ -93,8 +93,9 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep $client = $clientFactory->compile($config['client']); $extractorBuilder - ->withClientType(ApiType::from($config['version']) == ApiType::ADMIN ? new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusAdminClientInterface::class) : new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusShopClientInterface::class)) - ->withClient($client->getBuilder()->getNode()); + ->withClientType(ApiType::ADMIN == ApiType::from($config['version']) ? new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusAdminClientInterface') : new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusShopClientInterface')) + ->withClient($client->getBuilder()->getNode()) + ; $extractor->merge($client); @@ -109,8 +110,9 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep $client = $clientFactory->compile($config['client']); $loaderBuilder - ->withClientType(ApiType::from($config['version']) == ApiType::ADMIN ? new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusAdminClientInterface::class) : new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusShopClientInterface::class)) - ->withClient($client->getBuilder()->getNode()); + ->withClientType(ApiType::ADMIN == ApiType::from($config['version']) ? new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusAdminClientInterface') : new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusShopClientInterface')) + ->withClient($client->getBuilder()->getNode()) + ; $loader->merge($client); diff --git a/src/Validator/ExtractorConfigurationValidator.php b/src/Validator/ExtractorConfigurationValidator.php index f1ccb94..ef5d2e6 100644 --- a/src/Validator/ExtractorConfigurationValidator.php +++ b/src/Validator/ExtractorConfigurationValidator.php @@ -75,7 +75,7 @@ class ExtractorConfigurationValidator 'get', ], 'paymentMethod' => [ - 'all' , + 'all', 'get', ], 'payment' => [ @@ -103,7 +103,7 @@ class ExtractorConfigurationValidator 'get', ], 'productTaxon' => [ - 'all' , + 'all', 'get', ], 'productTranslation' => [ @@ -118,7 +118,7 @@ class ExtractorConfigurationValidator ], 'product' => [ 'all', - 'get' + 'get', ], 'promotionCoupon' => [ 'all', @@ -215,7 +215,7 @@ class ExtractorConfigurationValidator 'get', ], 'paymentMethod' => [ - 'all' , + 'all', 'get', ], 'payment' => [ @@ -246,7 +246,7 @@ class ExtractorConfigurationValidator ], 'product' => [ 'all', - 'get' + 'get', ], 'shippingMethod' => [ 'all', @@ -263,29 +263,18 @@ class ExtractorConfigurationValidator public static function validate(string $type, string $method, string $version = 'admin'): void { - $validTypes = $version === 'admin' ? self::ADMIN_VALID_TYPES : self::SHOP_VALID_TYPES; + $validTypes = 'admin' === $version ? self::ADMIN_VALID_TYPES : self::SHOP_VALID_TYPES; - if ($validTypes === null) { + if (null === $validTypes) { throw new InvalidConfigurationException(sprintf('Unknown version "%s".', $version)); } - if (!array_key_exists($type, $validTypes)) { - throw new InvalidConfigurationException(sprintf( - 'Invalid extractor type "%s" for version "%s". Valid types are: %s.', - $type, - $version, - implode(', ', array_keys($validTypes)) - )); + if (!\array_key_exists($type, $validTypes)) { + throw new InvalidConfigurationException(sprintf('Invalid extractor type "%s" for version "%s". Valid types are: %s.', $type, $version, implode(', ', array_keys($validTypes)))); } - if (!in_array($method, $validTypes[$type], true)) { - throw new InvalidConfigurationException(sprintf( - 'Invalid method "%s" for extractor type "%s" and version "%s". Valid methods are: %s.', - $method, - $type, - $version, - implode(', ', $validTypes[$type]) - )); + if (!\in_array($method, $validTypes[$type], true)) { + throw new InvalidConfigurationException(sprintf('Invalid method "%s" for extractor type "%s" and version "%s". Valid methods are: %s.', $method, $type, $version, implode(', ', $validTypes[$type]))); } } } diff --git a/src/Validator/LoaderConfigurationValidator.php b/src/Validator/LoaderConfigurationValidator.php index 563e009..c5ed589 100644 --- a/src/Validator/LoaderConfigurationValidator.php +++ b/src/Validator/LoaderConfigurationValidator.php @@ -181,32 +181,20 @@ class LoaderConfigurationValidator ], ]; - public static function validate(string $type, string $method, string $version = 'admin'): void { - $validTypes = $version === 'admin' ? self::ADMIN_VALID_TYPES : self::SHOP_VALID_TYPES; + $validTypes = 'admin' === $version ? self::ADMIN_VALID_TYPES : self::SHOP_VALID_TYPES; - if ($validTypes === null) { + if (null === $validTypes) { throw new InvalidConfigurationException(sprintf('Unknown version "%s".', $version)); } - if (!array_key_exists($type, $validTypes)) { - throw new InvalidConfigurationException(sprintf( - 'Invalid loader type "%s" for version "%s". Valid types are: %s.', - $type, - $version, - implode(', ', array_keys($validTypes)) - )); + if (!\array_key_exists($type, $validTypes)) { + throw new InvalidConfigurationException(sprintf('Invalid loader type "%s" for version "%s". Valid types are: %s.', $type, $version, implode(', ', array_keys($validTypes)))); } - if (!in_array($method, $validTypes[$type], true)) { - throw new InvalidConfigurationException(sprintf( - 'Invalid method "%s" for extractor type "%s" and version "%s". Valid methods are: %s.', - $method, - $type, - $version, - implode(', ', $validTypes[$type]) - )); + if (!\in_array($method, $validTypes[$type], true)) { + throw new InvalidConfigurationException(sprintf('Invalid method "%s" for extractor type "%s" and version "%s". Valid methods are: %s.', $method, $type, $version, implode(', ', $validTypes[$type]))); } } } From 9c209165c9460a26d6752af66898085c05b7cfb5 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Mon, 8 Jul 2024 15:23:06 +0000 Subject: [PATCH 27/27] [rector] Rector fixes --- src/Capacity/All.php | 5 +---- src/Capacity/ListPerPage.php | 5 +---- src/Capacity/Upsert.php | 5 +---- src/Factory/Client.php | 2 +- src/Service.php | 4 ++-- 5 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/Capacity/All.php b/src/Capacity/All.php index 35b07a0..fd4a51f 100644 --- a/src/Capacity/All.php +++ b/src/Capacity/All.php @@ -20,10 +20,7 @@ public function __construct( public function applies(array $config): bool { - $endpoints = array_merge( - Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, - Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES, - ); + $endpoints = [...Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, ...Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES]; return isset($config['type']) && \array_key_exists($config['type'], $endpoints) diff --git a/src/Capacity/ListPerPage.php b/src/Capacity/ListPerPage.php index 8573f2b..3c7c60f 100644 --- a/src/Capacity/ListPerPage.php +++ b/src/Capacity/ListPerPage.php @@ -20,10 +20,7 @@ public function __construct( public function applies(array $config): bool { - $endpoints = array_merge( - Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, - Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES, - ); + $endpoints = [...Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, ...Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES]; return isset($config['type']) && \array_key_exists($config['type'], $endpoints) diff --git a/src/Capacity/Upsert.php b/src/Capacity/Upsert.php index 0d41819..c524cc8 100644 --- a/src/Capacity/Upsert.php +++ b/src/Capacity/Upsert.php @@ -12,10 +12,7 @@ final class Upsert implements CapacityInterface { public function applies(array $config): bool { - $endpoints = array_merge( - Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, - Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES, - ); + $endpoints = [...Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES, ...Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES]; return isset($config['type']) && \array_key_exists($config['type'], $endpoints) diff --git a/src/Factory/Client.php b/src/Factory/Client.php index 98877a6..6b28900 100644 --- a/src/Factory/Client.php +++ b/src/Factory/Client.php @@ -94,7 +94,7 @@ public function compile(array $config): Repository\Client $clientBuilder->withClientBuilder( new Node\Expr\New_( new Node\Name\FullyQualified( - Sylius\ApiType::ADMIN == $this->type ? 'Diglin\\Sylius\\ApiClient\\SyliusAdminClientBuilder' : 'Diglin\\Sylius\\ApiClient\\SyliusShopClientBuilder' + Sylius\ApiType::ADMIN == $this->type ? \Diglin\Sylius\ApiClient\SyliusAdminClientBuilder::class : \Diglin\Sylius\ApiClient\SyliusShopClientBuilder::class ), ) ); diff --git a/src/Service.php b/src/Service.php index cdb1acf..9c89c3f 100644 --- a/src/Service.php +++ b/src/Service.php @@ -93,7 +93,7 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep $client = $clientFactory->compile($config['client']); $extractorBuilder - ->withClientType(ApiType::ADMIN == ApiType::from($config['version']) ? new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusAdminClientInterface') : new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusShopClientInterface')) + ->withClientType(ApiType::ADMIN == ApiType::from($config['version']) ? new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusAdminClientInterface::class) : new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusShopClientInterface::class)) ->withClient($client->getBuilder()->getNode()) ; @@ -110,7 +110,7 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep $client = $clientFactory->compile($config['client']); $loaderBuilder - ->withClientType(ApiType::ADMIN == ApiType::from($config['version']) ? new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusAdminClientInterface') : new Node\Name\FullyQualified(name: 'Diglin\\Sylius\\ApiClient\\SyliusShopClientInterface')) + ->withClientType(ApiType::ADMIN == ApiType::from($config['version']) ? new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusAdminClientInterface::class) : new Node\Name\FullyQualified(name: \Diglin\Sylius\ApiClient\SyliusShopClientInterface::class)) ->withClient($client->getBuilder()->getNode()) ;