From 3c3000c21593bd131aaa75e335119ce6cf027b5c Mon Sep 17 00:00:00 2001 From: Jonathan Date: Thu, 26 Oct 2023 11:10:35 +0200 Subject: [PATCH] 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) {