From 87b5b69d39a74ffd2e70dce79fbdae4cdf89f935 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Fri, 27 Oct 2023 13:57:02 +0200 Subject: [PATCH] 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; + } +}