Skip to content

Commit

Permalink
extract endpoints/methods from ShopApi on extractor/loader class
Browse files Browse the repository at this point in the history
  • Loading branch information
JoMessina committed Oct 26, 2023
1 parent 28283e1 commit ca7e08f
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 30 deletions.
152 changes: 130 additions & 22 deletions src/Configuration/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ final class Extractor implements Config\Definition\ConfigurationInterface
'get',
'listPaymentsPerPage',
'allPayments',
'listShipmentPerPage',
'listShipmentsPerPage',
'allShipments'
],
'orderItem' => [
Expand Down Expand Up @@ -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 = [
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -380,36 +504,20 @@ 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()
->cannotBeEmpty()
->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')
Expand Down
36 changes: 34 additions & 2 deletions src/Configuration/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']])) {
Expand Down
8 changes: 2 additions & 6 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down

0 comments on commit ca7e08f

Please sign in to comment.