Skip to content

Commit

Permalink
extract endpoints for capacity All, fix a return type for filters on …
Browse files Browse the repository at this point in the history
…All capacity file
  • Loading branch information
JoMessina committed Oct 26, 2023
1 parent ca7e08f commit 3c3000c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/Builder/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -28,7 +28,7 @@ public function addFilter(
value: $operator,
),
new Node\Arg(
value: $value,
value: $value ?? new Node\Expr\ConstFetch(new Node\Name('null')),
),
];

Expand All @@ -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'),
);
}
Expand All @@ -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)
Expand Down
93 changes: 89 additions & 4 deletions src/Capacity/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

final class All implements CapacityInterface
{
private static array $endpoints = [
private static array $endpointsLegacy = [
// Simple resources Endpoints
'channels',
'countries',
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 3c3000c

Please sign in to comment.