Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e352a3

Browse files
authored
refactor #11779 Api refactor simple entities part 1 (AdamKasp)
This PR was merged into the 1.8 branch. Discussion ---------- | Q | A | --------------- | ----- | Branch? | 1.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Related tickets | part of #11250, based on #11776 | License | MIT <!-- - Bug fixes must be submitted against the 1.7 branch (the lowest possible) - Features and deprecations must be submitted against the master branch - Make sure that the correct base branch is set To be sure you are not breaking any Backward Compatibilities, check the documentation: https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html --> Commits ------- dc45032 [API] refactor endpoint for api resources
2 parents 31eceff + dc45032 commit 3e352a3

18 files changed

Lines changed: 82 additions & 113 deletions

src/Sylius/Behat/Client/ApiPlatformClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(
4040
AbstractBrowser $client,
4141
SharedStorageInterface $sharedStorage,
4242
string $resource,
43-
string $section = null
43+
?string $section = null
4444
) {
4545
$this->client = $client;
4646
$this->sharedStorage = $sharedStorage;
@@ -65,7 +65,7 @@ public function showByIri(string $iri): Response
6565

6666
public function subResourceIndex(string $subResource, string $id): Response
6767
{
68-
$request = Request::subResourceIndex($this->resource, $id, $subResource);
68+
$request = Request::subResourceIndex($this->section, $this->resource, $id, $subResource);
6969
$request->authorize($this->getToken());
7070

7171
return $this->request($request);
@@ -105,7 +105,7 @@ public function sort(array $sorting): Response
105105

106106
public function applyTransition(string $id, string $transition, array $content = []): Response
107107
{
108-
$request = Request::transition($this->resource, $id, $transition);
108+
$request = Request::transition($this->section, $this->resource, $id, $transition);
109109
$request->authorize($this->getToken());
110110
$request->setContent($content);
111111

@@ -114,7 +114,7 @@ public function applyTransition(string $id, string $transition, array $content =
114114

115115
public function customItemAction(string $id, string $type, string $action): Response
116116
{
117-
$request = Request::customItemAction($this->resource, $id, $type, $action);
117+
$request = Request::customItemAction($this->section, $this->resource, $id, $type, $action);
118118
$request->authorize($this->getToken());
119119

120120
return $this->request($request);

src/Sylius/Behat/Client/Request.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public static function index(?string $section, string $resource, ?string $token
4949
return new self(sprintf('/new-api/%s%s', self::prepareSection($section), $resource), HttpRequest::METHOD_GET, $headers);
5050
}
5151

52-
public static function subResourceIndex(string $resource, string $id, string $subResource): RequestInterface
52+
public static function subResourceIndex(?string $section, string $resource, string $id, string $subResource): RequestInterface
5353
{
5454
return new self(
55-
sprintf('/new-api/%s/%s/%s', $resource, $id, $subResource),
55+
sprintf('/new-api/%s%s/%s/%s', self::prepareSection($section), $resource, $id, $subResource),
5656
HttpRequest::METHOD_GET
5757
);
5858
}
@@ -99,15 +99,15 @@ public static function delete(?string $section, string $resource, string $id, st
9999
);
100100
}
101101

102-
public static function transition(string $resource, string $id, string $transition): RequestInterface
102+
public static function transition(?string $section, string $resource, string $id, string $transition): RequestInterface
103103
{
104-
return self::customItemAction($resource, $id, HttpRequest::METHOD_PATCH, $transition);
104+
return self::customItemAction($section, $resource, $id, HttpRequest::METHOD_PATCH, $transition);
105105
}
106106

107-
public static function customItemAction(string $resource, string $id, string $type, string $action): RequestInterface
107+
public static function customItemAction(?string $section, string $resource, string $id, string $type, string $action): RequestInterface
108108
{
109109
return new self(
110-
sprintf('/new-api/%s/%s/%s', $resource, $id, $action),
110+
sprintf('/new-api/%s%s/%s/%s',$section, $resource, $id, $action),
111111
$type,
112112
['CONTENT_TYPE' => 'application/merge-patch+json']
113113
);
@@ -214,7 +214,7 @@ private function mergeArraysUniquely(array $firstArray, array $secondArray): arr
214214

215215
private static function prepareSection(?string $section): string
216216
{
217-
if($section === null) {
217+
if ($section === null) {
218218
return '';
219219
}
220220

src/Sylius/Behat/Client/RequestInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface RequestInterface
1717
{
1818
public static function index(?string $section, string $resource, string $token): self;
1919

20-
public static function subResourceIndex(string $resource, string $id, string $subResource): self;
20+
public static function subResourceIndex(?string $section, string $resource, string $id, string $subResource): self;
2121

2222
public static function show(?string $section, string $resource, string $id, string $token): self;
2323

@@ -27,7 +27,7 @@ public static function update(?string $section, string $resource, string $id, st
2727

2828
public static function delete(?string $section, string $resource, string $id, string $token): self;
2929

30-
public static function transition(string $resource, string $id, string $transition): self;
30+
public static function transition(?string $section, string $resource, string $id, string $transition): self;
3131

3232
public static function upload(?string $section, string $resource, string $token): self;
3333

src/Sylius/Behat/Context/Api/Admin/ManagingExchangeRatesContext.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ public function iSpecifyItsRatioAs(?string $ratio = null): void
8787
*/
8888
public function iChooseAsTheSourceCurrency(string $currencyCode): void
8989
{
90-
$this->client->addRequestData('sourceCurrency', '/new-api/currencies/' . $currencyCode);
90+
$this->client->addRequestData('sourceCurrency', '/new-api/admin/currencies/' . $currencyCode);
9191
}
9292

9393
/**
9494
* @When I choose :currencyCode as the target currency
9595
*/
9696
public function iChooseAsTheTargetCurrency(string $currencyCode): void
9797
{
98-
$this->client->addRequestData('targetCurrency', '/new-api/currencies/' . $currencyCode);
98+
$this->client->addRequestData('targetCurrency', '/new-api/admin/currencies/' . $currencyCode);
9999
}
100100

101101
/**
@@ -349,12 +349,12 @@ private function assertIfNotBeAbleToEditItCurrency(string $currencyType): void
349349
{
350350
$this->client->buildUpdateRequest($this->sharedStorage->get('exchange_rate_id'));
351351

352-
$this->client->addRequestData($currencyType, '/new-api/currencies/EUR');
352+
$this->client->addRequestData($currencyType, '/new-api/admin/currencies/EUR');
353353
$this->client->update();
354354

355355
Assert::false(
356356
$this->responseChecker->hasItemOnPositionWithValue(
357-
$this->client->index(), 0, $currencyType, '/new-api/currencies/EUR'
357+
$this->client->index(), 0, $currencyType, '/new-api/admin/currencies/EUR'
358358
),
359359
sprintf('It was possible to change %s', $currencyType)
360360
);
@@ -367,8 +367,8 @@ private function getExchangeRateFromResponse(
367367
/** @var array $item */
368368
foreach ($this->responseChecker->getCollection($this->client->index()) as $item) {
369369
if (
370-
$item['sourceCurrency'] === '/new-api/currencies/' . $sourceCurrency->getCode() &&
371-
$item['targetCurrency'] === '/new-api/currencies/' . $targetCurrency->getCode()
370+
$item['sourceCurrency'] === '/new-api/admin/currencies/' . $sourceCurrency->getCode() &&
371+
$item['targetCurrency'] === '/new-api/admin/currencies/' . $targetCurrency->getCode()
372372
) {
373373
return $item;
374374
}

src/Sylius/Behat/Context/Api/Shop/CartContext.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public function iShouldSeeWithQuantityInMyCart(string $productName, int $quantit
254254

255255
private function putProductToCart(ProductInterface $product, string $tokenValue, int $quantity = 1): void
256256
{
257-
$request = Request::customItemAction('orders', $tokenValue, HttpRequest::METHOD_PATCH, 'items');
257+
$request = Request::customItemAction(null,'orders', $tokenValue, HttpRequest::METHOD_PATCH, 'items');
258258

259259
$request->updateContent([
260260
'productCode' => $product->getCode(),
@@ -267,7 +267,7 @@ private function putProductToCart(ProductInterface $product, string $tokenValue,
267267

268268
private function putProductVariantToCart(ProductVariantInterface $productVariant, string $tokenValue, int $quantity = 1): void
269269
{
270-
$request = Request::customItemAction('orders', $tokenValue, HttpRequest::METHOD_PATCH, 'items');
270+
$request = Request::customItemAction(null, 'orders', $tokenValue, HttpRequest::METHOD_PATCH, 'items');
271271

272272
$request->updateContent([
273273
'productCode' => $productVariant->getProduct()->getCode(),
@@ -280,7 +280,7 @@ private function putProductVariantToCart(ProductVariantInterface $productVariant
280280

281281
private function removeOrderItemFromCart(string $orderItemId, string $tokenValue): void
282282
{
283-
$request = Request::customItemAction('orders', $tokenValue, HttpRequest::METHOD_PATCH, 'remove');
283+
$request = Request::customItemAction(null, 'orders', $tokenValue, HttpRequest::METHOD_PATCH, 'remove');
284284

285285
$request->updateContent(['orderItemId' => $orderItemId]);
286286

@@ -346,7 +346,7 @@ private function geOrderItemIdForProductInCart(ProductInterface $product, string
346346

347347
private function changeQuantityOfOrderItem(string $orderItemId, int $quantity, string $tokenValue): void
348348
{
349-
$request = Request::customItemAction('orders', $tokenValue, HttpRequest::METHOD_PATCH, 'change-quantity');
349+
$request = Request::customItemAction(null, 'orders', $tokenValue, HttpRequest::METHOD_PATCH, 'change-quantity');
350350

351351
$request->updateContent(['orderItemId' => $orderItemId, 'newQuantity' => $quantity]);
352352

src/Sylius/Behat/Resources/config/services/api.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,32 @@
3030

3131
<service id="sylius.behat.api_platform_client.channel" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
3232
<argument>channels</argument>
33+
<argument>admin</argument>
3334
</service>
3435

3536
<service id="sylius.behat.api_platform_client.country" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
3637
<argument>countries</argument>
38+
<argument>admin</argument>
3739
</service>
3840

3941
<service id="sylius.behat.api_platform_client.currency" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
4042
<argument>currencies</argument>
43+
<argument>admin</argument>
4144
</service>
4245

4346
<service id="sylius.behat.api_platform_client.customer_group" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
4447
<argument>customer-groups</argument>
48+
<argument>admin</argument>
4549
</service>
4650

4751
<service id="sylius.behat.api_platform_client.exchange_rate" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
4852
<argument>exchange-rates</argument>
53+
<argument>admin</argument>
4954
</service>
5055

5156
<service id="sylius.behat.api_platform_client.locale" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
5257
<argument>locales</argument>
58+
<argument>admin</argument>
5359
</service>
5460

5561
<service id="sylius.behat.api_platform_client.payment" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
@@ -58,6 +64,7 @@
5864

5965
<service id="sylius.behat.api_platform_client.product_association_type" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
6066
<argument>product-association-types</argument>
67+
<argument>admin</argument>
6168
</service>
6269

6370
<service id="sylius.behat.api_platform_client.product_option" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
@@ -74,6 +81,7 @@
7481

7582
<service id="sylius.behat.api_platform_client.province" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
7683
<argument>provinces</argument>
84+
<argument>admin</argument>
7785
</service>
7886

7987
<service id="sylius.behat.api_platform_client.cart" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
@@ -90,6 +98,7 @@
9098

9199
<service id="sylius.behat.api_platform_client.tax_category" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
92100
<argument>tax-categories</argument>
101+
<argument>admin</argument>
93102
</service>
94103

95104
<service id="sylius.behat.api_platform_client.shipment" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">

src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Channel.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@
1616
xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd"
1717
>
1818
<resource class="%sylius.model.channel.class%" shortName="Channel">
19+
<attribute name="route_prefix">admin</attribute>
1920
<attribute name="normalization_context">
2021
<attribute name="groups">
2122
<attribute>channel:read</attribute>
2223
</attribute>
2324
</attribute>
2425

2526
<collectionOperations>
26-
<collectionOperation name="get">
27-
<attribute name="security">is_granted('ROLE_API_ACCESS')</attribute>
28-
</collectionOperation>
27+
<collectionOperation name="get" />
2928
<collectionOperation name="post">
30-
<attribute name="security">is_granted('ROLE_API_ACCESS')</attribute>
3129
<attribute name="denormalization_context">
3230
<attribute name="groups">channel:create</attribute>
3331
</attribute>
@@ -36,7 +34,6 @@
3634

3735
<itemOperations>
3836
<itemOperation name="get">
39-
<attribute name="security">is_granted('ROLE_API_ACCESS')</attribute>
4037
<attribute name="groups">
4138
<attribute>channel:read</attribute>
4239
</attribute>

src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Country.xml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd"
1717
>
1818
<resource class="%sylius.model.country.class%" shortName="Country">
19+
<attribute name="route_prefix">admin</attribute>
20+
1921
<attribute name="normalization_context">
2022
<attribute name="groups">
2123
<attribute>country:read</attribute>
@@ -25,23 +27,17 @@
2527
<attribute name="validation_groups">sylius</attribute>
2628

2729
<collectionOperations>
28-
<collectionOperation name="get">
29-
<attribute name="security">is_granted('ROLE_API_ACCESS')</attribute>
30-
</collectionOperation>
30+
<collectionOperation name="get" />
3131
<collectionOperation name="post">
32-
<attribute name="security">is_granted('ROLE_API_ACCESS')</attribute>
3332
<attribute name="denormalization_context">
3433
<attribute name="groups">country:create</attribute>
3534
</attribute>
3635
</collectionOperation>
3736
</collectionOperations>
3837

3938
<itemOperations>
40-
<itemOperation name="get">
41-
<attribute name="security">is_granted('ROLE_API_ACCESS')</attribute>
42-
</itemOperation>
39+
<itemOperation name="get" />
4340
<itemOperation name="put">
44-
<attribute name="security">is_granted('ROLE_API_ACCESS')</attribute>
4541
<attribute name="denormalization_context">
4642
<attribute name="groups">country:update</attribute>
4743
</attribute>

src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Currency.xml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,17 @@
1616
xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd"
1717
>
1818
<resource class="%sylius.model.currency.class%" shortName="Currency">
19+
<attribute name="route_prefix">admin</attribute>
20+
1921
<attribute name="validation_groups">sylius</attribute>
2022

2123
<collectionOperations>
22-
<collectionOperation name="get">
23-
<attribute name="security">is_granted('ROLE_API_ACCESS')</attribute>
24-
</collectionOperation>
25-
<collectionOperation name="post">
26-
<attribute name="security">is_granted('ROLE_API_ACCESS')</attribute>
27-
</collectionOperation>
24+
<collectionOperation name="get" />
25+
<collectionOperation name="post" />
2826
</collectionOperations>
2927

3028
<itemOperations>
31-
<itemOperation name="get">
32-
<attribute name="security">is_granted('ROLE_API_ACCESS')</attribute>
33-
</itemOperation>
29+
<itemOperation name="get" />
3430
</itemOperations>
3531

3632
<property name="id" identifier="false" writable="false" />

src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Customer.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd"
1717
>
1818
<resource class="%sylius.model.customer.class%" shortName="Customer">
19+
<attribute name="route_name">admin</attribute>
20+
1921
<attribute name="normalization_context">
2022
<attribute name="groups">
2123
<attribute>customer:read</attribute>
@@ -26,7 +28,6 @@
2628

2729
<itemOperations>
2830
<itemOperation name="get">
29-
<attribute name="security">is_granted('ROLE_API_ACCESS')</attribute>
3031
<attribute name="groups">
3132
<attribute>customer:read</attribute>
3233
</attribute>

0 commit comments

Comments
 (0)