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

Skip to content

Commit f8baa28

Browse files
committed
Modify DataTransformer
1 parent 0fa0b0c commit f8baa28

10 files changed

Lines changed: 161 additions & 51 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,15 @@ public function iProceededWithShippingMethod(ShippingMethodInterface $shippingMe
214214
{
215215
$this->client->request(
216216
Request::METHOD_PATCH,
217-
\sprintf('/new-api/orders/%s/shipments/{shipmentId}', $this->sharedStorage->get('cart_token')),
217+
\sprintf(
218+
'/new-api/orders/%s/shipments/%s',
219+
$this->sharedStorage->get('cart_token'),
220+
(string) $this->iriConverter->getItemFromIri($this->getCart()['shipments'][0])->getId()
221+
),
218222
[],
219223
[],
220224
$this->getHeaders(),
221225
json_encode([
222-
'shipmentId' => (string) $this->iriConverter->getItemFromIri($this->getCart()['shipments'][0])->getId(),
223226
'shippingMethodCode' => $shippingMethod->getCode(),
224227
], \JSON_THROW_ON_ERROR)
225228
);

src/Sylius/Bundle/ApiBundle/Command/Checkout/ChooseShippingMethod.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
namespace Sylius\Bundle\ApiBundle\Command\Checkout;
1515

1616
use Sylius\Bundle\ApiBundle\Command\OrderTokenValueAwareInterface;
17+
use Sylius\Bundle\ApiBundle\Command\SubresourceIdAwareInterfaceInterface;
1718

18-
class ChooseShippingMethod implements OrderTokenValueAwareInterface
19+
class ChooseShippingMethod implements OrderTokenValueAwareInterface, SubresourceIdAwareInterfaceInterface
1920
{
2021
/** @var string|null */
2122
public $orderTokenValue;
@@ -32,9 +33,8 @@ class ChooseShippingMethod implements OrderTokenValueAwareInterface
3233
*/
3334
public $shippingMethodCode;
3435

35-
public function __construct(string $shipmentId, string $shippingMethodCode)
36+
public function __construct(string $shippingMethodCode)
3637
{
37-
$this->shipmentId = $shipmentId;
3838
$this->shippingMethodCode = $shippingMethodCode;
3939
}
4040

@@ -47,4 +47,19 @@ public function setOrderTokenValue(?string $orderTokenValue): void
4747
{
4848
$this->orderTokenValue = $orderTokenValue;
4949
}
50+
51+
public function getSubresourceId(): ?string
52+
{
53+
return $this->shipmentId;
54+
}
55+
56+
public function setSubresourceId(?string $secondSubresourceId): void
57+
{
58+
$this->shipmentId = $secondSubresourceId;
59+
}
60+
61+
public function getSubresourceIdAttributeKey(): string
62+
{
63+
return 'shipmentId';
64+
}
5065
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\ApiBundle\Command;
15+
16+
interface CommandAwareDataTransformerInterface
17+
{
18+
}

src/Sylius/Bundle/ApiBundle/Command/OrderTokenValueAwareInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Sylius\Bundle\ApiBundle\Command;
1515

16-
interface OrderTokenValueAwareInterface
16+
interface OrderTokenValueAwareInterface extends CommandAwareDataTransformerInterface
1717
{
1818
public function getOrderTokenValue(): ?string;
1919

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\ApiBundle\Command;
15+
16+
interface SubresourceIdAwareInterfaceInterface extends CommandAwareDataTransformerInterface
17+
{
18+
public function getSubresourceId(): ?string;
19+
20+
public function setSubresourceId(?string $secondSubresourceId): void;
21+
22+
public function getSubresourceIdAttributeKey(): string;
23+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\ApiBundle\DataTransformer;
15+
16+
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
17+
use Sylius\Bundle\ApiBundle\Command\CommandAwareDataTransformerInterface;
18+
use Sylius\Bundle\ApiBundle\Command\OrderTokenValueAwareInterface;
19+
use Sylius\Bundle\ApiBundle\Command\SubresourceIdAwareInterfaceInterface;
20+
use Sylius\Component\Core\Model\OrderInterface;
21+
use Symfony\Component\HttpFoundation\RequestStack;
22+
use Webmozart\Assert\Assert;
23+
24+
final class CommandAwareInputDataTransformer implements DataTransformerInterface
25+
{
26+
/** @var RequestStack */
27+
private $requestStack;
28+
29+
public function __construct(RequestStack $requestStack)
30+
{
31+
$this->requestStack = $requestStack;
32+
}
33+
34+
public function transform($object, string $to, array $context = [])
35+
{
36+
if ($object instanceof OrderTokenValueAwareInterface) {
37+
$object = $this->assignOrderTokenValue($object, $to, $context);
38+
}
39+
40+
if ($object instanceof SubresourceIdAwareInterfaceInterface) {
41+
$object = $this->assignSubresourceId($object, $to, $context);
42+
}
43+
44+
return $object;
45+
}
46+
47+
public function supportsTransformation($data, string $to, array $context = []): bool
48+
{
49+
return is_a($context['input']['class'], CommandAwareDataTransformerInterface::class, true);
50+
}
51+
52+
private function assignOrderTokenValue(OrderTokenValueAwareInterface $object, string $to, array $context = [])
53+
{
54+
/** @var OrderInterface $cart */
55+
$cart = $context['object_to_populate'];
56+
57+
$object->setOrderTokenValue($cart->getTokenValue());
58+
59+
return $object;
60+
}
61+
62+
private function assignSubresourceId(SubresourceIdAwareInterfaceInterface $object, string $to, array $context = [])
63+
{
64+
$attributes = $this->requestStack->getCurrentRequest()->attributes->all();
65+
66+
/** @var string|null $subresourceId */
67+
$subresourceId = $attributes[$object->getSubresourceIdAttributeKey()] ?? null;
68+
69+
Assert::notNull($subresourceId, 'Path does not have subresource id');
70+
Assert::string($subresourceId, 'Attribute type must be a string');
71+
72+
$object->setSubresourceId($subresourceId);
73+
74+
return $object;
75+
}
76+
}

src/Sylius/Bundle/ApiBundle/DataTransformer/OrderTokenValueAwareInputDataTransformer.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@
9595
<attribute name="security">is_granted('IS_AUTHENTICATED_ANONYMOUSLY')</attribute>
9696
<attribute name="openapi_context">
9797
<attribute name="summary">Select shipping methods for particular shipment</attribute>
98+
<attribute name="parameters">
99+
<attribute>
100+
<attribute name="name">id</attribute>
101+
<attribute name="in">path</attribute>
102+
<attribute name="required">true</attribute>
103+
<attribute name="schema">
104+
<attribute name="type">string</attribute>
105+
</attribute>
106+
</attribute>
107+
<attribute>
108+
<attribute name="name">shipmentId</attribute>
109+
<attribute name="in">path</attribute>
110+
<attribute name="required">true</attribute>
111+
<attribute name="schema">
112+
<attribute name="type">string</attribute>
113+
</attribute>
114+
</attribute>
115+
</attribute>
98116
</attribute>
99117
<attribute name="method">PATCH</attribute>
100118
<attribute name="path">/orders/{id}/shipments/{shipmentId}</attribute>

src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Commands/Checkout/ChooseShippingMethodHandler.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,5 @@
1919
<attribute name="shippingMethodCode">
2020
<group>cart:select_shipping_method</group>
2121
</attribute>
22-
<attribute name="shipmentId">
23-
<group>cart:select_shipping_method</group>
24-
</attribute>
2522
</class>
2623
</serializer>

src/Sylius/Bundle/ApiBundle/Resources/config/services.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969
<tag name="property_info.list_extractor" priority="-2000" />
7070
</service>
7171

72-
<service class="Sylius\Bundle\ApiBundle\DataTransformer\OrderTokenValueAwareInputDataTransformer" id="sylius_bundle_api.data_transformer.add_item_to_cart_input_data_transformer">
72+
<service class="Sylius\Bundle\ApiBundle\DataTransformer\CommandAwareInputDataTransformer" id="sylius_bundle_api.data_transformer.add_item_to_cart_input_data_transformer">
73+
<argument type="service" id="request_stack" />
7374
<tag name="api_platform.data_transformer"/>
7475
</service>
7576

0 commit comments

Comments
 (0)