Skip to content

Commit 09e8740

Browse files
committed
Improved the Sylius plugin for 1;9 or more recent version
1 parent 65caa5f commit 09e8740

18 files changed

+306
-1144
lines changed

src/ApiType.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Kiboko\Plugin\Sylius;
4+
5+
enum ApiType: string
6+
{
7+
case ADMIN = 'admin';
8+
case SHOP = 'shop';
9+
}

src/Builder/Client.php

+11-34
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66

77
use Diglin\Sylius\ApiClient\SyliusAdminClientBuilder;
88
use Diglin\Sylius\ApiClient\SyliusShopClientBuilder;
9+
use Kiboko\Plugin\Sylius\ApiType;
910
use Kiboko\Plugin\Sylius\MissingAuthenticationMethodException;
10-
use Kiboko\Plugin\Sylius\Validator\ApiType;
1111
use PhpParser\Builder;
1212
use PhpParser\Node;
1313

1414
final class Client implements Builder
1515
{
16-
1716
private ?Node\Expr $clientId = null;
1817
private ?Node\Expr $secret = null;
1918
private ?Node\Expr $username = null;
@@ -24,11 +23,7 @@ final class Client implements Builder
2423
private ?Node\Expr $httpRequestFactory = null;
2524
private ?Node\Expr $httpStreamFactory = null;
2625
private ?Node\Expr $fileSystem = null;
27-
private string $apiType;
28-
29-
public const API_ADMIN_KEY = 'admin';
30-
public const API_SHOP_KEY = 'shop';
31-
public const API_LEGACY_KEY = 'legacy';
26+
private ?Node\Expr $client = null;
3227

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

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

51-
public function withApiType(string $apiType): self
52-
{
53-
$this->apiType = $apiType;
54-
55-
return $this;
56-
}
57-
5846
public function withPassword(Node\Expr $username, Node\Expr $password): self
5947
{
6048
$this->username = $username;
@@ -91,9 +79,16 @@ public function withFileSystem(Node\Expr $fileSystem): self
9179
return $this;
9280
}
9381

82+
public function withClientBuilder(Node\Expr $client): self
83+
{
84+
$this->client = $client;
85+
86+
return $this;
87+
}
88+
9489
public function getNode(): Node\Expr\MethodCall
9590
{
96-
$instance = $this->getClientBuilderNode();
91+
$instance = $this->client;
9792

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

145140
private function getClientBuilderNode(): Node\Expr\MethodCall
146141
{
147-
$className = match ($this->apiType) {
148-
ApiType::ADMIN->value => SyliusAdminClientBuilder::class,
149-
ApiType::SHOP->value => SyliusShopClientBuilder::class,
150-
ApiType::LEGACY->value => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder',
151-
default => throw new \UnhandledMatchError($this->apiType)
152-
};
153-
154142
return new Node\Expr\MethodCall(
155-
var: new Node\Expr\New_(
156-
new Node\Name\FullyQualified($className),
157-
),
143+
var: $this->client,
158144
name: new Node\Identifier('setBaseUri'),
159145
args: [
160146
new Node\Arg($this->baseUrl),
@@ -178,15 +164,6 @@ private function getFactoryMethod(): string
178164
private function getFactoryArguments(): array
179165
{
180166
if (null !== $this->password) {
181-
if ($this->apiType === ApiType::LEGACY->value) {
182-
return [
183-
$this->clientId,
184-
$this->secret,
185-
$this->username,
186-
$this->password,
187-
];
188-
}
189-
190167
return [
191168
$this->username,
192169
$this->password,

src/Builder/Extractor.php

+7-12
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ final class Extractor implements StepBuilderInterface
1212
{
1313
private ?Node\Expr $logger = null;
1414
private ?Node\Expr $client = null;
15-
private string $apiType;
15+
private ?Node $type = null;
1616

17-
public function __construct(private readonly Builder $capacity) {}
17+
public function __construct(
18+
private readonly Builder $capacity,
19+
) {}
1820

1921
public function withClient(Node\Expr $client): self
2022
{
@@ -23,9 +25,9 @@ public function withClient(Node\Expr $client): self
2325
return $this;
2426
}
2527

26-
public function withApiType(string $apiType): self
28+
public function withClientType(Node $type): self
2729
{
28-
$this->apiType = $apiType;
30+
$this->type = $type;
2931

3032
return $this;
3133
}
@@ -131,17 +133,10 @@ class: new Node\Stmt\Class_(
131133

132134
public function getParamsNode(): array
133135
{
134-
$className = match ($this->apiType) {
135-
Client::API_ADMIN_KEY => \Diglin\Sylius\ApiClient\SyliusAdminClientInterface::class,
136-
Client::API_LEGACY_KEY => \Diglin\Sylius\ApiClient\SyliusLegacyClientInterface::class,
137-
Client::API_SHOP_KEY => \Diglin\Sylius\ApiClient\SyliusShopClientInterface::class,
138-
default => throw new \UnhandledMatchError($this->apiType)
139-
};
140-
141136
return [
142137
new Node\Param(
143138
var: new Node\Expr\Variable('client'),
144-
type: new Node\Name\FullyQualified(name: $className),
139+
type: $this->type,
145140
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
146141
),
147142
new Node\Param(

src/Builder/Loader.php

+4-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class Loader implements StepBuilderInterface
1212
{
1313
private ?Node\Expr $logger = null;
1414
private ?Node\Expr $client = null;
15-
private string $apiType;
15+
private ?Node $type = null;
1616

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

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

26-
public function withApiType(string $apiType): self
26+
public function withClientType(Node $type): self
2727
{
28-
$this->apiType = $apiType;
28+
$this->type = $type;
2929

3030
return $this;
3131
}
@@ -137,17 +137,10 @@ class: new Node\Stmt\Class_(
137137

138138
public function getParamsNode(): array
139139
{
140-
$className = match ($this->apiType) {
141-
Client::API_ADMIN_KEY => \Diglin\Sylius\ApiClient\SyliusAdminClientInterface::class,
142-
Client::API_LEGACY_KEY => \Diglin\Sylius\ApiClient\SyliusLegacyClientInterface::class,
143-
Client::API_SHOP_KEY => \Diglin\Sylius\ApiClient\SyliusShopClientInterface::class,
144-
default => throw new \UnhandledMatchError($this->apiType)
145-
};
146-
147140
return [
148141
new Node\Param(
149142
var: new Node\Expr\Variable('client'),
150-
type: new Node\Name\FullyQualified(name: $className),
143+
type: $this->type,
151144
flags: Node\Stmt\Class_::MODIFIER_PRIVATE,
152145
),
153146
new Node\Param(

src/Capacity/All.php

+11-130
Original file line numberDiff line numberDiff line change
@@ -4,145 +4,26 @@
44

55
namespace Kiboko\Plugin\Sylius\Capacity;
66

7-
use Kiboko\Contract\Configurator\InvalidConfigurationException;
87
use Kiboko\Plugin\Sylius;
9-
use Kiboko\Plugin\Sylius\Validator\ApiType;
108
use PhpParser\Builder;
119
use PhpParser\Node;
1210
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1311

14-
use function Kiboko\Component\SatelliteToolbox\Configuration\compileValue;
12+
use function Kiboko\Component\SatelliteToolbox\Configuration\compileValueWhenExpression;
1513

1614
final class All implements CapacityInterface
1715
{
18-
private static array $endpointsLegacy = [
19-
// Simple resources Endpoints
20-
'channels',
21-
'countries',
22-
'carts',
23-
'channels',
24-
'countries',
25-
'currencies',
26-
'customers',
27-
'exchangeRates',
28-
'locales',
29-
'orders',
30-
'payments',
31-
'paymentMethods',
32-
'products',
33-
'productAttributes',
34-
'productAssociationTypes',
35-
'productOptions',
36-
'promotions',
37-
'shipments',
38-
'shippingCategories',
39-
'taxCategories',
40-
'taxRates',
41-
'taxons',
42-
'users',
43-
'zones',
44-
];
45-
46-
private static array $endpointsAdmin = [
47-
// Simple Ressource Endpoints
48-
'adjustment',
49-
'administrator',
50-
'catalogPromotion',
51-
'channel',
52-
'country',
53-
'currency',
54-
'customerGroup',
55-
'exchangeRate',
56-
'locale',
57-
'order',
58-
'payment',
59-
'product',
60-
'productAssociationType',
61-
'productImage',
62-
'productOption',
63-
'productOptionValue',
64-
'productReview',
65-
'productTaxon',
66-
'productVariant',
67-
'promotion',
68-
'province',
69-
'shipment',
70-
'shippingCategory',
71-
'shippingMethod',
72-
'ShopBillingData',
73-
'taxCategory',
74-
'taxon',
75-
'taxonTranslation',
76-
'zone',
77-
'zoneMember',
78-
];
79-
80-
private static array $endpointsShop = [
81-
// Simple Ressource Endpoints
82-
'address',
83-
'adjustment',
84-
'country',
85-
'currency',
86-
'locale',
87-
'order',
88-
'orderItem',
89-
'payment',
90-
'paymentMethod',
91-
'product',
92-
'productReview',
93-
'productVariant',
94-
'shipment',
95-
'shippingMethod',
96-
'taxon',
97-
];
98-
99-
private static array $doubleEndpointsLegacy = [
100-
// Double resources Endpoints
101-
'productReviews',
102-
'productVariants',
103-
'promotionCoupons',
104-
];
105-
106-
private static array $doubleEndpointsAdmin = [
107-
// Double resources Endpoints
108-
'adjustment',
109-
'province',
110-
'shopBillingData',
111-
'zoneMember',
112-
];
113-
114-
private static array $doubleEndpointsShop = [
115-
// Double resources Endpoints
116-
'adjustment',
117-
'order',
118-
];
119-
12016
public function __construct(private readonly ExpressionLanguage $interpreter) {}
12117

12218
public function applies(array $config): bool
12319
{
124-
if (!isset($config['api_type'])) {
125-
throw new InvalidConfigurationException('Your Sylius API configuration is using some unsupported capacity, check your "api_type" properties to a suitable set.');
126-
}
127-
switch ($config['api_type']) {
128-
case 'admin':
129-
$endpoints = self::$endpointsAdmin;
130-
$doubleEndpoints = self::$doubleEndpointsAdmin;
131-
break;
132-
case 'shop':
133-
$endpoints = self::$endpointsShop;
134-
$doubleEndpoints = self::$doubleEndpointsShop;
135-
break;
136-
case 'legacy':
137-
$endpoints = self::$endpointsLegacy;
138-
$doubleEndpoints = self::$doubleEndpointsLegacy;
139-
break;
140-
default:
141-
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)));
142-
}
20+
$endpoints = array_merge(
21+
Sylius\Validator\ExtractorConfigurationValidator::ADMIN_VALID_TYPES,
22+
Sylius\Validator\ExtractorConfigurationValidator::SHOP_VALID_TYPES,
23+
);
14324

14425
return isset($config['type'])
145-
&& (\in_array($config['type'], $endpoints) || \in_array($config['type'], $doubleEndpoints))
26+
&& \array_key_exists($config['type'], $endpoints)
14627
&& isset($config['method'])
14728
&& 'all' === $config['method'];
14829
}
@@ -152,11 +33,11 @@ private function compileFilters(array ...$filters): Node\Expr
15233
$builder = new Sylius\Builder\Search();
15334
foreach ($filters as $filter) {
15435
$builder->addFilter(
155-
field: compileValue($this->interpreter, $filter['field']),
156-
operator: compileValue($this->interpreter, $filter['operator']),
157-
value: compileValue($this->interpreter, $filter['value']),
158-
scope: \array_key_exists('scope', $filter) ? compileValue($this->interpreter, $filter['scope']) : null,
159-
locale: \array_key_exists('locale', $filter) ? compileValue($this->interpreter, $filter['locale']) : null
36+
field: compileValueWhenExpression($this->interpreter, $filter['field']),
37+
operator: compileValueWhenExpression($this->interpreter, $filter['operator']),
38+
value: compileValueWhenExpression($this->interpreter, $filter['value']),
39+
scope: \array_key_exists('scope', $filter) ? compileValueWhenExpression($this->interpreter, $filter['scope']) : null,
40+
locale: \array_key_exists('locale', $filter) ? compileValueWhenExpression($this->interpreter, $filter['locale']) : null
16041
);
16142
}
16243

0 commit comments

Comments
 (0)