From 69bb513fbd2d39f875871e29e8b3d81f39410c92 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 26 Jul 2013 10:43:17 +0200 Subject: [PATCH] refacto cart process --- core/lib/Thelia/Action/Cart.php | 41 ++++++++++++------- .../Core/HttpFoundation/Session/Session.php | 12 ++++-- .../Thelia/Tests/Command/BaseCommandTest.php | 29 +++++++++---- 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index b160c82fd..e4e51c755 100755 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -98,15 +98,16 @@ class Cart implements EventSubscriberInterface public function getCart(Request $request) { - if (null !== $cartId = $request->getSession()->getCart()){ - $cart = CartQuery::create()->findPk($cartId); + + if(null !== $cart = $request->getSession()->getCart()){ + return $cart; } if ($request->cookies->has("thelia_cart")) { //le cookie de panier existe, on le récupère - $cookie = $request->cookies->get("thelia_cart"); + $token = $request->cookies->get("thelia_cart"); - $cart = CartQuery::create()->findOneByToken($cookie); + $cart = CartQuery::create()->findOneByToken($token); if ($cart) { //le panier existe en base @@ -115,12 +116,12 @@ class Cart implements EventSubscriberInterface if ($customer) { if($cart->getCustomerId() != $customer->getId()) { //le customer du panier n'est pas le mm que celui connecté, il faut cloner le panier sans le customer_id - $cart = $cart->duplicate($customer); + $cart = $this->duplicateCart($cart, $request->getSession(), $customer); } } else { if ($cart->getCustomerId() != null) { //il faut dupliquer le panier sans le customer_id - $cart = $cart->duplicate(); + $cart = $this->duplicateCart($cart, $request->getSession()); } } @@ -135,7 +136,11 @@ class Cart implements EventSubscriberInterface return $cart; } - public function createCart(Session $session) + /** + * @param Session $session + * @return CartModel + */ + protected function createCart(Session $session) { $cart = new CartModel(); $cart->setToken($this->generateCookie()); @@ -152,17 +157,25 @@ class Cart implements EventSubscriberInterface } + /** + * @param CartModel $cart + * @param Session $session + * @param Customer $customer + * @return CartModel + */ + protected function duplicateCart(CartModel $cart, Session $session, Customer $customer = null) + { + $newCart = $cart->duplicate($this->generateCookie(), $customer); + $session->setCart($newCart->getId()); + + return $newCart; + } + public function generateCookie() { $id = uniqid('', true); - - setcookie("thelia_cart", $id, time()); + setcookie("thelia_cart", $id, uniqid('', true)); return $id; } - - public function addItem() - { - - } } diff --git a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php index a50ab92f5..cde686ee4 100755 --- a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php +++ b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php @@ -26,7 +26,7 @@ namespace Thelia\Core\HttpFoundation\Session; use Symfony\Component\HttpFoundation\Session\Session as BaseSession; use Thelia\Core\Security\User\UserInterface; use Thelia\Exception\InvalidCartException; -use Thelia\Model\Base\CartQuery; +use Thelia\Model\CartQuery; use Thelia\Model\Cart; use Thelia\Tools\URL; @@ -117,9 +117,9 @@ class Session extends BaseSession // -- Cart ------------------------------------------------------------------ /** - * retrieve cart id in session + * return cart if exists and is valid (checking customer) * - * @return int cart id + * @return \Thelia\Model\Cart|null */ public function getCart() { @@ -137,6 +137,12 @@ class Session extends BaseSession return $cart; } + /** + * + * + * @param \Thelia\Model\Cart $cart + * @throws \Thelia\Exception\InvalidCartException + */ protected function verifyValidCart(Cart $cart) { $customer = $this->getCustomerUser(); diff --git a/core/lib/Thelia/Tests/Command/BaseCommandTest.php b/core/lib/Thelia/Tests/Command/BaseCommandTest.php index ca9d0a632..356478ab9 100644 --- a/core/lib/Thelia/Tests/Command/BaseCommandTest.php +++ b/core/lib/Thelia/Tests/Command/BaseCommandTest.php @@ -1,12 +1,25 @@ . */ +/* */ +/*************************************************************************************/ namespace Thelia\Tests\Command;