Skip to content

Commit

Permalink
Improved the Sylius plugin for 1;9 or more recent version
Browse files Browse the repository at this point in the history
  • Loading branch information
sebprt committed Jul 8, 2024
1 parent 65caa5f commit 09e8740
Show file tree
Hide file tree
Showing 18 changed files with 306 additions and 1,144 deletions.
9 changes: 9 additions & 0 deletions src/ApiType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Kiboko\Plugin\Sylius;

enum ApiType: string
{
case ADMIN = 'admin';
case SHOP = 'shop';
}
45 changes: 11 additions & 34 deletions src/Builder/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

use Diglin\Sylius\ApiClient\SyliusAdminClientBuilder;
use Diglin\Sylius\ApiClient\SyliusShopClientBuilder;
use Kiboko\Plugin\Sylius\ApiType;
use Kiboko\Plugin\Sylius\MissingAuthenticationMethodException;
use Kiboko\Plugin\Sylius\Validator\ApiType;
use PhpParser\Builder;
use PhpParser\Node;

final class Client implements Builder
{

private ?Node\Expr $clientId = null;
private ?Node\Expr $secret = null;
private ?Node\Expr $username = null;
Expand All @@ -24,11 +23,7 @@ final class Client implements Builder
private ?Node\Expr $httpRequestFactory = null;
private ?Node\Expr $httpStreamFactory = null;
private ?Node\Expr $fileSystem = null;
private string $apiType;

public const API_ADMIN_KEY = 'admin';
public const API_SHOP_KEY = 'shop';
public const API_LEGACY_KEY = 'legacy';
private ?Node\Expr $client = null;

public function __construct(private readonly Node\Expr $baseUrl) {}

Expand All @@ -48,13 +43,6 @@ public function withToken(Node\Expr $token, Node\Expr $refreshToken): self
return $this;
}

public function withApiType(string $apiType): self
{
$this->apiType = $apiType;

return $this;
}

public function withPassword(Node\Expr $username, Node\Expr $password): self
{
$this->username = $username;
Expand Down Expand Up @@ -91,9 +79,16 @@ public function withFileSystem(Node\Expr $fileSystem): self
return $this;
}

public function withClientBuilder(Node\Expr $client): self
{
$this->client = $client;

return $this;
}

public function getNode(): Node\Expr\MethodCall
{
$instance = $this->getClientBuilderNode();
$instance = $this->client;

if (null !== $this->httpClient) {
$instance = new Node\Expr\MethodCall(
Expand Down Expand Up @@ -144,17 +139,8 @@ public function getNode(): Node\Expr\MethodCall

private function getClientBuilderNode(): Node\Expr\MethodCall

Check failure on line 140 in src/Builder/Client.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Method Kiboko\Plugin\Sylius\Builder\Client::getClientBuilderNode() is unused.

Check failure on line 140 in src/Builder/Client.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Method Kiboko\Plugin\Sylius\Builder\Client::getClientBuilderNode() is unused.

Check failure on line 140 in src/Builder/Client.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Kiboko\Plugin\Sylius\Builder\Client::getClientBuilderNode() is unused.
{
$className = match ($this->apiType) {
ApiType::ADMIN->value => SyliusAdminClientBuilder::class,
ApiType::SHOP->value => SyliusShopClientBuilder::class,
ApiType::LEGACY->value => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder',
default => throw new \UnhandledMatchError($this->apiType)
};

return new Node\Expr\MethodCall(
var: new Node\Expr\New_(
new Node\Name\FullyQualified($className),
),
var: $this->client,

Check failure on line 143 in src/Builder/Client.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Parameter $var of class PhpParser\Node\Expr\MethodCall constructor expects PhpParser\Node\Expr, PhpParser\Node\Expr|null given.
name: new Node\Identifier('setBaseUri'),
args: [
new Node\Arg($this->baseUrl),
Expand All @@ -178,15 +164,6 @@ private function getFactoryMethod(): string
private function getFactoryArguments(): array

Check failure on line 164 in src/Builder/Client.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Method Kiboko\Plugin\Sylius\Builder\Client::getFactoryArguments() return type has no value type specified in iterable type array.

Check failure on line 164 in src/Builder/Client.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Method Kiboko\Plugin\Sylius\Builder\Client::getFactoryArguments() return type has no value type specified in iterable type array.
{
if (null !== $this->password) {
if ($this->apiType === ApiType::LEGACY->value) {
return [
$this->clientId,
$this->secret,
$this->username,
$this->password,
];
}

return [
$this->username,
$this->password,
Expand Down
19 changes: 7 additions & 12 deletions src/Builder/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ final class Extractor implements StepBuilderInterface
{
private ?Node\Expr $logger = null;
private ?Node\Expr $client = null;
private string $apiType;
private ?Node $type = null;

public function __construct(private readonly Builder $capacity) {}
public function __construct(
private readonly Builder $capacity,
) {}

public function withClient(Node\Expr $client): self
{
Expand All @@ -23,9 +25,9 @@ public function withClient(Node\Expr $client): self
return $this;
}

public function withApiType(string $apiType): self
public function withClientType(Node $type): self
{
$this->apiType = $apiType;
$this->type = $type;

return $this;
}
Expand Down Expand Up @@ -131,17 +133,10 @@ class: new Node\Stmt\Class_(

public function getParamsNode(): array

Check failure on line 134 in src/Builder/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Method Kiboko\Plugin\Sylius\Builder\Extractor::getParamsNode() return type has no value type specified in iterable type array.
{
$className = match ($this->apiType) {
Client::API_ADMIN_KEY => \Diglin\Sylius\ApiClient\SyliusAdminClientInterface::class,
Client::API_LEGACY_KEY => \Diglin\Sylius\ApiClient\SyliusLegacyClientInterface::class,
Client::API_SHOP_KEY => \Diglin\Sylius\ApiClient\SyliusShopClientInterface::class,
default => throw new \UnhandledMatchError($this->apiType)
};

return [
new Node\Param(
var: new Node\Expr\Variable('client'),
type: new Node\Name\FullyQualified(name: $className),
type: $this->type,

Check failure on line 139 in src/Builder/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Parameter $type of class PhpParser\Node\Param constructor expects PhpParser\Node\ComplexType|PhpParser\Node\Identifier|PhpParser\Node\Name|string|null, PhpParser\Node|null given.
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
),
new Node\Param(
Expand Down
15 changes: 4 additions & 11 deletions src/Builder/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class Loader implements StepBuilderInterface
{
private ?Node\Expr $logger = null;
private ?Node\Expr $client = null;
private string $apiType;
private ?Node $type = null;

public function __construct(private readonly Builder $capacity) {}

Expand All @@ -23,9 +23,9 @@ public function withClient(Node\Expr $client): self
return $this;
}

public function withApiType(string $apiType): self
public function withClientType(Node $type): self
{
$this->apiType = $apiType;
$this->type = $type;

return $this;
}
Expand Down Expand Up @@ -137,17 +137,10 @@ class: new Node\Stmt\Class_(

public function getParamsNode(): array
{
$className = match ($this->apiType) {
Client::API_ADMIN_KEY => \Diglin\Sylius\ApiClient\SyliusAdminClientInterface::class,
Client::API_LEGACY_KEY => \Diglin\Sylius\ApiClient\SyliusLegacyClientInterface::class,
Client::API_SHOP_KEY => \Diglin\Sylius\ApiClient\SyliusShopClientInterface::class,
default => throw new \UnhandledMatchError($this->apiType)
};

return [
new Node\Param(
var: new Node\Expr\Variable('client'),
type: new Node\Name\FullyQualified(name: $className),
type: $this->type,
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
),
new Node\Param(
Expand Down
141 changes: 11 additions & 130 deletions src/Capacity/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,145 +4,26 @@

namespace Kiboko\Plugin\Sylius\Capacity;

use Kiboko\Contract\Configurator\InvalidConfigurationException;
use Kiboko\Plugin\Sylius;
use Kiboko\Plugin\Sylius\Validator\ApiType;
use PhpParser\Builder;
use PhpParser\Node;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

use function Kiboko\Component\SatelliteToolbox\Configuration\compileValue;
use function Kiboko\Component\SatelliteToolbox\Configuration\compileValueWhenExpression;

final class All implements CapacityInterface
{
private static array $endpointsLegacy = [
// Simple resources Endpoints
'channels',
'countries',
'carts',
'channels',
'countries',
'currencies',
'customers',
'exchangeRates',
'locales',
'orders',
'payments',
'paymentMethods',
'products',
'productAttributes',
'productAssociationTypes',
'productOptions',
'promotions',
'shipments',
'shippingCategories',
'taxCategories',
'taxRates',
'taxons',
'users',
'zones',
];

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
{
if (!isset($config['api_type'])) {
throw new InvalidConfigurationException('Your Sylius API configuration is using some unsupported capacity, check your "api_type" properties to a suitable set.');
}
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:
throw new \InvalidArgumentException(sprintf('The value of api_type should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($config['api_type'], \JSON_THROW_ON_ERROR)));
}
$endpoints = array_merge(
Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES,
Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES,
);

return isset($config['type'])
&& (\in_array($config['type'], $endpoints) || \in_array($config['type'], $doubleEndpoints))
&& \array_key_exists($config['type'], $endpoints)
&& isset($config['method'])
&& 'all' === $config['method'];
}
Expand All @@ -152,11 +33,11 @@ private function compileFilters(array ...$filters): Node\Expr
$builder = new Sylius\Builder\Search();
foreach ($filters as $filter) {
$builder->addFilter(
field: compileValue($this->interpreter, $filter['field']),
operator: compileValue($this->interpreter, $filter['operator']),
value: compileValue($this->interpreter, $filter['value']),
scope: \array_key_exists('scope', $filter) ? compileValue($this->interpreter, $filter['scope']) : null,
locale: \array_key_exists('locale', $filter) ? compileValue($this->interpreter, $filter['locale']) : null
field: compileValueWhenExpression($this->interpreter, $filter['field']),
operator: compileValueWhenExpression($this->interpreter, $filter['operator']),
value: compileValueWhenExpression($this->interpreter, $filter['value']),
scope: \array_key_exists('scope', $filter) ? compileValueWhenExpression($this->interpreter, $filter['scope']) : null,
locale: \array_key_exists('locale', $filter) ? compileValueWhenExpression($this->interpreter, $filter['locale']) : null
);
}

Expand Down
Loading

0 comments on commit 09e8740

Please sign in to comment.