Skip to content

Commit

Permalink
refactor #472 [Cart] Remove channel code from cart routes (GSadee)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.0-dev branch.

Discussion
----------



Commits
-------

26d961c [Cart] Remove channel code from cart routes
  • Loading branch information
lchrusciel authored Jul 22, 2019
2 parents 73c35cd + 26d961c commit 0806b31
Show file tree
Hide file tree
Showing 22 changed files with 133 additions and 336 deletions.
5 changes: 4 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
| Old Route | New route |
|:-----------------------------------------|:------------------------------------|
| `{channelCode}/address-book/*` | `address-book/*` |
| `{channelCode}/carts/*` | `carts/*` |
| `{channelCode}/checkout/*` | `checkout/*` |
| `{channelCode}/me` | `me` |
| `{channelCode}/orders/*` | `orders/*` |
Expand All @@ -42,7 +43,9 @@
| `{channelCode}/taxons/*` | `taxons/*` |

* The channel code has been added as a second argument to `AddProductReviewByCodeRequest`,
`AddProductReviewBySlugRequest`, `ResendVerificationTokenRequest` and `RegisterCustomerRequest` classes.
`AddProductReviewBySlugRequest`, `ResendVerificationTokenRequest` and `RegisterCustomerRequest` classes.

* The argument in constructor of `PickupCartRequest` class has been changed from `Request $request `to `string channelCode`.

# UPGRADE FROM 1.0.0-beta.17 to 1.0.0-beta.18

Expand Down
27 changes: 12 additions & 15 deletions doc/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ parameters:
type: "string"

paths:
/{channelCode}/carts:
parameters:
- $ref: "#/parameters/ChannelCode"
/carts:
post:
tags:
- "cart"
Expand All @@ -85,9 +83,9 @@ paths:
description: "Invalid input"
schema:
$ref: "#/definitions/GeneralError"
/{channelCode}/carts/{token}:

/carts/{token}:
parameters:
- $ref: "#/parameters/ChannelCode"
- $ref: "#/parameters/CartToken"
get:
tags:
Expand Down Expand Up @@ -117,9 +115,9 @@ paths:
description: "Invalid input (E.g. token has not been found)"
schema:
$ref: "#/definitions/GeneralError"
/{channelCode}/carts/{token}/items:

/carts/{token}/items:
parameters:
- $ref: "#/parameters/ChannelCode"
- $ref: "#/parameters/CartToken"
post:
tags:
Expand All @@ -143,9 +141,9 @@ paths:
description: "Invalid input, validation failed."
schema:
$ref: "#/definitions/GeneralError"
/{channelCode}/carts/{token}/multiple-items:

/carts/{token}/multiple-items:
parameters:
- $ref: "#/parameters/ChannelCode"
- $ref: "#/parameters/CartToken"
post:
tags:
Expand All @@ -169,9 +167,9 @@ paths:
description: "Invalid input, validation failed."
schema:
$ref: "#/definitions/GeneralError"
/{channelCode}/carts/{token}/items/{identifier}:

/carts/{token}/items/{identifier}:
parameters:
- $ref: "#/parameters/ChannelCode"
- $ref: "#/parameters/CartToken"
- name: "identifier"
in: "path"
Expand Down Expand Up @@ -209,9 +207,9 @@ paths:
description: "Invalid input (E.g. token has not been found)"
schema:
$ref: "#/definitions/GeneralError"
/{channelCode}/carts/{token}/estimated-shipping-cost:

/carts/{token}/estimated-shipping-cost:
parameters:
- $ref: "#/parameters/ChannelCode"
- $ref: "#/parameters/CartToken"
get:
tags:
Expand Down Expand Up @@ -239,9 +237,8 @@ paths:
schema:
$ref: "#/definitions/GeneralError"

/{channelCode}/carts/{token}/coupon:
/carts/{token}/coupon:
parameters:
- $ref: "#/parameters/ChannelCode"
- $ref: "#/parameters/CartToken"
put:
tags:
Expand Down
48 changes: 20 additions & 28 deletions spec/Normalizer/RequestCartTokenNormalizerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\ShopApiPlugin\Command\Cart\PickupCart;
use Sylius\ShopApiPlugin\Normalizer\RequestCartTokenNormalizerInterface;
use Sylius\ShopApiPlugin\Request\Cart\PickupCartRequest;
Expand All @@ -18,9 +20,9 @@

final class RequestCartTokenNormalizerSpec extends ObjectBehavior
{
function let(ValidatorInterface $validator, MessageBusInterface $bus): void
function let(ValidatorInterface $validator, MessageBusInterface $bus, ChannelContextInterface $channelContext): void
{
$this->beConstructedWith($validator, $bus);
$this->beConstructedWith($validator, $bus, $channelContext);
}

function it_implements_request_cart_token_normalizer_interface(): void
Expand All @@ -29,53 +31,43 @@ function it_implements_request_cart_token_normalizer_interface(): void
}

function it_returns_passed_request_if_cart_token_was_set(
Request $request,
ValidatorInterface $validator,
MessageBusInterface $bus
): void {
$request->attributes = new ParameterBag([
'token' => 'sample_cart_token',
'channelCode' => 'en_GB',
]);

$validator->validate(Argument::any())->shouldNotBeCalled();
$bus->dispatch(Argument::any())->shouldNotBeCalled();

$this->doNotAllowNullCartToken($request)->shouldReturn($request);
}

function it_throws_exception_when_pickup_cart_request_is_not_valid(
Request $request,
ValidatorInterface $validator,
MessageBusInterface $bus,
ConstraintViolationListInterface $constraintViolationList
ChannelContextInterface $channelContext,
ChannelInterface $channel,
Request $request
): void {
$request->attributes = new ParameterBag(['channelCode' => 'non_existing_channel']);
$channelContext->getChannel()->willReturn($channel);
$channel->getCode()->willReturn('WEB_GB');

$constraintViolationList->count()->willReturn(1);

$validator->validate(Argument::type(PickupCartRequest::class))->willReturn($constraintViolationList);
$request->attributes = new ParameterBag(['token' => 'sample_cart_token']);

$validator->validate(Argument::any())->shouldNotBeCalled();
$bus->dispatch(Argument::any())->shouldNotBeCalled();

$this->shouldThrow(\InvalidArgumentException::class)->during('doNotAllowNullCartToken', [$request]);
$this->doNotAllowNullCartToken($request)->shouldReturn($request);
}

function it_picks_up_new_cart_and_sets_its_token_on_request_if_token_was_not_passed(
Request $request,
ValidatorInterface $validator,
MessageBusInterface $bus,
ChannelContextInterface $channelContext,
ChannelInterface $channel,
Request $request,
ConstraintViolationListInterface $constraintViolationList
): void {
$request->attributes = new ParameterBag(['channelCode' => 'en_GB']);
$channelContext->getChannel()->willReturn($channel);
$channel->getCode()->willReturn('WEB_GB');

$request->attributes = new ParameterBag();

$constraintViolationList->count()->willReturn(0);

$validator->validate(Argument::type(PickupCartRequest::class))->willReturn($constraintViolationList);

$bus
->dispatch(Argument::that(function (PickupCart $command): bool {
return !empty($command->orderToken()) && $command->channelCode() === 'en_GB';
return !empty($command->orderToken()) && $command->channelCode() === 'WEB_GB';
}))
->willReturn(new Envelope(new \stdClass()))
->shouldBeCalled()
Expand Down
12 changes: 10 additions & 2 deletions src/Controller/Cart/PickupCartAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Request\Cart\PickupCartRequest;
use Sylius\ShopApiPlugin\ViewRepository\Cart\CartViewRepositoryInterface;
Expand All @@ -32,23 +33,30 @@ final class PickupCartAction
/** @var CartViewRepositoryInterface */
private $cartQuery;

/** @var ChannelContextInterface */
private $channelContext;

public function __construct(
ViewHandlerInterface $viewHandler,
MessageBusInterface $bus,
ValidatorInterface $validator,
ValidationErrorViewFactoryInterface $validationErrorViewFactory,
CartViewRepositoryInterface $cartQuery
CartViewRepositoryInterface $cartQuery,
ChannelContextInterface $channelContext
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->validator = $validator;
$this->validationErrorViewFactory = $validationErrorViewFactory;
$this->cartQuery = $cartQuery;
$this->channelContext = $channelContext;
}

public function __invoke(Request $request): Response
{
$pickupRequest = new PickupCartRequest($request);
$channel = $this->channelContext->getChannel();

$pickupRequest = new PickupCartRequest($channel->getCode());

$validationResults = $this->validator->validate($pickupRequest);

Expand Down
15 changes: 12 additions & 3 deletions src/Normalizer/RequestCartTokenNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sylius\ShopApiPlugin\Normalizer;

use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\ShopApiPlugin\Request\Cart\PickupCartRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Messenger\MessageBusInterface;
Expand All @@ -17,10 +18,17 @@ final class RequestCartTokenNormalizer implements RequestCartTokenNormalizerInte
/** @var MessageBusInterface */
private $bus;

public function __construct(ValidatorInterface $validator, MessageBusInterface $bus)
{
/** @var ChannelContextInterface */
private $channelContext;

public function __construct(
ValidatorInterface $validator,
MessageBusInterface $bus,
ChannelContextInterface $channelContext
) {
$this->validator = $validator;
$this->bus = $bus;
$this->channelContext = $channelContext;
}

public function doNotAllowNullCartToken(Request $request): Request
Expand All @@ -29,7 +37,8 @@ public function doNotAllowNullCartToken(Request $request): Request
return $request;
}

$pickupRequest = new PickupCartRequest($request);
$channel = $this->channelContext->getChannel();
$pickupRequest = new PickupCartRequest($channel->getCode());

$validationResults = $this->validator->validate($pickupRequest);

Expand Down
9 changes: 4 additions & 5 deletions src/Request/Cart/PickupCartRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@

use Ramsey\Uuid\Uuid;
use Sylius\ShopApiPlugin\Command\Cart\PickupCart;
use Symfony\Component\HttpFoundation\Request;

class PickupCartRequest
{
/** @var string */
protected $token;

/** @var string */
protected $channel;
protected $channelCode;

public function __construct(Request $request)
public function __construct(string $channelCode)
{
$this->token = Uuid::uuid4()->toString();
$this->channel = $request->attributes->get('channelCode');
$this->channelCode = $channelCode;
}

public function getCommand(): PickupCart
{
return new PickupCart($this->token, $this->channel);
return new PickupCart($this->token, $this->channelCode);
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/routing.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
sylius_shop_api_cart:
resource: "@ShopApiPlugin/Resources/config/routing/cart.yml"
prefix: /shop-api/{channelCode}
prefix: /shop-api

sylius_shop_api_product_by_slug:
resource: "@ShopApiPlugin/Resources/config/routing/productBySlug.yml"
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
>
<argument type="service" id="validator" />
<argument type="service" id="sylius_shop_api_plugin.command_bus" />
<argument type="service" id="sylius.context.channel" />
</service>

<!-- Removing the create cart context from composite context (see: https://github.com/Sylius/Sylius/issues/10192) -->
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/actions/cart.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<argument type="service" id="validator" />
<argument type="service" id="sylius.shop_api_plugin.factory.validation_error_view_factory" />
<argument type="service" id="sylius.shop_api_plugin.view_repository.cart_view_repository" />
<argument type="service" id="sylius.context.channel" />
</service>

<service id="sylius.shop_api_plugin.controller.cart.change_item_quantity_action"
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/validation/cart/PickupCartRequest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\ShopApiPlugin\Request\Cart\PickupCartRequest">
<property name="channel">
<property name="channelCode">
<constraint name="NotNull">
<option name="message">sylius.shop_api.channel.not_null</option>
</constraint>
Expand Down
Loading

0 comments on commit 0806b31

Please sign in to comment.