Skip to content
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

[RFC] [POC] [Cart] Introduce CartModifier service #5970

Merged
merged 4 commits into from
Sep 6, 2016

Conversation

lchrusciel
Copy link
Member

@lchrusciel lchrusciel commented Sep 3, 2016

Q A
Bug fix? yes
New feature? yes
BC breaks? no
Related tickets
License MIT

Concept

Small proof of concept / RFC. In this PR I have extracted logic of adding a cart item from CartItemController to a separate service. This service will be responsible for determining if the new cart item should be added or just increase number of units in some existing one, and then it will recalculate the whole cart.

Profit

Such a solution ensure us, that we have only one true way of adding new items, where we are sure that whole cart will be recalculated after each addition. It can be also useful If one would like to add Order now feature.

Idea

I would like to decouple Sylius logic as much as it is possible form symfony events. They are nice, but only for end users. I am pretty sure, that managing this kind of logic with regular service is much better approach.

The problem with the previous implementation is, that order recalculation is handled via Cart event SyliusCartEvents::CART_CHANGE.

TODOs for now:

  • Extract cart item removal
  • Remove SyliusCartEvents::CART_CHANGE recalculation (at least for cart add/remove actions)

Ideas for next iteration

  • Remove OrderRecalculationListener
  • I think that cart clearing logic could be also added to this service.
  • Another concept, that can be discussed later is some CartResolver, which will provide customers cart. Thanks to this it will be possible to remove one of arguments in CartModifier::addToCart(). By default it could take a cart from context, but if one would need it, it will be possible to change the way, how the cart is resolved.

Edit

I have change names of events for add/remove actions, because removing OrderRecalculationListener in this PR would result in ugly diff and also could create some problems in other system parts.

* @param OrderInterface $cart
* @param CartItemInterface $cartItem
*/
public function addToCart(OrderInterface $cart, CartItemInterface $cartItem);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OrderInterface 💃

@lchrusciel lchrusciel force-pushed the cart-modifier-poc branch 2 times, most recently from b1736e0 to 7c0d10c Compare September 4, 2016 07:57
@lchrusciel lchrusciel changed the title [RFC] [POC] [WIP] [Cart] Introduce CartModifier service [RFC] [POC] [Cart] Introduce CartModifier service Sep 4, 2016
@pjedrzejewski pjedrzejewski added Enhancement Minor issues and PRs improving the current solutions (optimizations, typo fixes, etc.). RFC Discussions about potential changes or new features. labels Sep 4, 2016
@lchrusciel lchrusciel force-pushed the cart-modifier-poc branch 3 times, most recently from d13b3e8 to 72c03a9 Compare September 4, 2016 19:49
* @param CartInterface $cart
* @param CartItemInterface $item
*/
private function resolveCartItem(CartInterface $cart, CartItemInterface $item)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it only resolve the cart item? Or resolves and merges with the passed one if found? It would probably need some better naming.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any ideas?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as we won't came up with a better idea, I think it could be merged and I will create an issue with naming.

@michalmarcinkowski
Copy link
Contributor

Good job Łukasz! 👍

@lchrusciel lchrusciel deleted the cart-modifier-poc branch September 6, 2016 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Minor issues and PRs improving the current solutions (optimizations, typo fixes, etc.). RFC Discussions about potential changes or new features.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants