From 3f412ac3c39fccffe5a9c5234ccef0ab72b8ca76 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Fri, 27 Oct 2023 16:46:21 +0200 Subject: [PATCH] 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)));