-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[Api][Cart] change quantity on cart #11695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
GSadee
merged 1 commit into
Sylius:master
from
AdamKasp:api-cart-operation-change-quantity
Aug 3, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/Sylius/Bundle/ApiBundle/Command/Cart/ChangeItemQuantityInCart.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Paweł Jędrzejewski | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sylius\Bundle\ApiBundle\Command\Cart; | ||
|
|
||
| use Sylius\Bundle\ApiBundle\Command\OrderTokenValueAwareInterface; | ||
|
|
||
| final class ChangeItemQuantityInCart implements OrderTokenValueAwareInterface | ||
| { | ||
| /** @var string|null */ | ||
| public $orderTokenValue; | ||
|
|
||
| /** | ||
| * @var string | ||
| * @psalm-immutable | ||
| */ | ||
| public $orderItemId; | ||
|
|
||
| /** | ||
| * @var int | ||
| * @psalm-immutable | ||
| */ | ||
| public $newQuantity; | ||
|
|
||
| public function __construct(string $orderItemId, int $newQuantity) | ||
| { | ||
| $this->orderItemId = $orderItemId; | ||
| $this->newQuantity = $newQuantity; | ||
| } | ||
|
|
||
| public static function createFromData(string $tokenValue, string $orderItemId, int $newQuantity): self | ||
| { | ||
| $command = new self($orderItemId, $newQuantity); | ||
|
|
||
| $command->orderTokenValue = $tokenValue; | ||
|
|
||
| return $command; | ||
| } | ||
|
|
||
| public function getOrderTokenValue(): ?string | ||
| { | ||
| return $this->orderTokenValue; | ||
| } | ||
|
|
||
| public function setOrderTokenValue(?string $orderTokenValue): void | ||
| { | ||
| $this->orderTokenValue = $orderTokenValue; | ||
| } | ||
| } | ||
66 changes: 66 additions & 0 deletions
66
src/Sylius/Bundle/ApiBundle/CommandHandler/Cart/ChangeItemQuantityInCartHandler.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Paweł Jędrzejewski | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sylius\Bundle\ApiBundle\CommandHandler\Cart; | ||
|
|
||
| use Sylius\Bundle\ApiBundle\Command\Cart\ChangeItemQuantityInCart; | ||
| use Sylius\Component\Core\Model\OrderInterface; | ||
| use Sylius\Component\Core\Model\OrderItemInterface; | ||
| use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface; | ||
| use Sylius\Component\Order\Processor\OrderProcessorInterface; | ||
| use Sylius\Component\Order\Repository\OrderItemRepositoryInterface; | ||
| use Symfony\Component\Messenger\Handler\MessageHandlerInterface; | ||
| use Webmozart\Assert\Assert; | ||
|
|
||
| final class ChangeItemQuantityInCartHandler implements MessageHandlerInterface | ||
| { | ||
| /** @var OrderItemRepositoryInterface */ | ||
| private $orderItemRepository; | ||
|
|
||
| /** @var OrderItemQuantityModifierInterface */ | ||
| private $orderItemQuantityModifier; | ||
|
|
||
| /** @var OrderProcessorInterface */ | ||
| private $orderProcessor; | ||
|
|
||
| public function __construct( | ||
| OrderItemRepositoryInterface $orderItemRepository, | ||
| OrderItemQuantityModifierInterface $orderItemQuantityModifier, | ||
| OrderProcessorInterface $orderProcessor | ||
| ) { | ||
| $this->orderItemRepository = $orderItemRepository; | ||
| $this->orderItemQuantityModifier = $orderItemQuantityModifier; | ||
| $this->orderProcessor = $orderProcessor; | ||
| } | ||
|
|
||
| public function __invoke(ChangeItemQuantityInCart $command): OrderInterface | ||
| { | ||
| /** @var OrderItemInterface|null $orderItem */ | ||
| $orderItem = $this->orderItemRepository->findOneByIdAndCartTokenValue( | ||
| $command->orderItemId, | ||
| $command->orderTokenValue | ||
| ); | ||
|
|
||
| Assert::notNull($orderItem); | ||
|
|
||
| /** @var OrderInterface $cart */ | ||
| $cart = $orderItem->getOrder(); | ||
|
|
||
| Assert::same($cart->getTokenValue(), $command->orderTokenValue); | ||
|
|
||
| $this->orderItemQuantityModifier->modify($orderItem, $command->newQuantity); | ||
| $this->orderProcessor->process($cart); | ||
|
|
||
| return $cart; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...undle/ApiBundle/Resources/config/serialization/Commands/Cart/ChangeItemQuantityInCart.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?xml version="1.0" ?> | ||
|
|
||
| <!-- | ||
|
|
||
| This file is part of the Sylius package. | ||
|
|
||
| (c) Paweł Jędrzejewski | ||
|
|
||
| For the full copyright and license information, please view the LICENSE | ||
| file that was distributed with this source code. | ||
|
|
||
| --> | ||
|
|
||
| <serializer xmlns="http://symfony.com/schema/dic/serializer-mapping" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd" | ||
| > | ||
| <class name="Sylius\Bundle\ApiBundle\Command\Cart\ChangeItemQuantityInCart"> | ||
| <attribute name="orderItemId"> | ||
| <group>cart:change_quantity</group> | ||
| </attribute> | ||
|
|
||
| <attribute name="newQuantity"> | ||
| <group>cart:change_quantity</group> | ||
| </attribute> | ||
| </class> | ||
| </serializer> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/Sylius/Bundle/ApiBundle/spec/CommandHandler/Cart/ChangeItemQuantityInCartHandlerSpec.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Paweł Jędrzejewski | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace spec\Sylius\Bundle\ApiBundle\CommandHandler\Cart; | ||
|
|
||
| use PhpSpec\ObjectBehavior; | ||
| use Sylius\Bundle\ApiBundle\Command\Cart\ChangeItemQuantityInCart; | ||
| use Sylius\Bundle\OrderBundle\Doctrine\ORM\OrderItemRepository; | ||
| use Sylius\Component\Core\Model\OrderInterface; | ||
| use Sylius\Component\Core\Model\OrderItemInterface; | ||
| use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface; | ||
| use Sylius\Component\Order\Processor\OrderProcessorInterface; | ||
| use Sylius\Component\Order\Repository\OrderItemRepositoryInterface; | ||
| use Symfony\Component\Messenger\Handler\MessageHandlerInterface; | ||
|
|
||
| final class ChangeItemQuantityInCartHandlerSpec extends ObjectBehavior | ||
| { | ||
| function let( | ||
| OrderItemRepositoryInterface $orderItemRepository, | ||
| OrderItemQuantityModifierInterface $orderItemQuantityModifier, | ||
| OrderProcessorInterface $orderProcessor | ||
| ) { | ||
| $this->beConstructedWith($orderItemRepository, $orderItemQuantityModifier, $orderProcessor); | ||
| } | ||
|
|
||
| function it_is_a_message_handler(): void | ||
| { | ||
| $this->shouldImplement(MessageHandlerInterface::class); | ||
| } | ||
|
|
||
| function it_changes_order_item_quantity( | ||
| OrderItemRepositoryInterface $orderItemRepository, | ||
| OrderItemQuantityModifierInterface $orderItemQuantityModifier, | ||
| OrderProcessorInterface $orderProcessor, | ||
| OrderInterface $cart, | ||
| OrderItemInterface $cartItem | ||
| ): void { | ||
| $orderItemRepository->findOneByIdAndCartTokenValue( | ||
| 'ORDER_ITEM_ID', | ||
| 'TOKEN_VALUE' | ||
| )->willReturn($cartItem); | ||
|
|
||
| $cartItem->getOrder()->willReturn($cart); | ||
|
|
||
| $cart->getTokenValue()->willReturn('TOKEN_VALUE'); | ||
|
|
||
| $orderItemQuantityModifier->modify($cartItem, 5)->shouldBeCalled(); | ||
|
AdamKasp marked this conversation as resolved.
|
||
| $orderProcessor->process($cart)->shouldBeCalled(); | ||
|
|
||
| $this(ChangeItemQuantityInCart::createFromData( 'TOKEN_VALUE', 'ORDER_ITEM_ID', 5)); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i chose
newQuantitybecause this name is visible in the request body, and IMOnewQuantityis more descriptive than justquantity