refacto cart process

This commit is contained in:
Manuel Raynaud
2013-07-26 10:43:17 +02:00
parent 69059ea6b9
commit 69bb513fbd
3 changed files with 57 additions and 25 deletions

View File

@@ -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()
{
}
}

View File

@@ -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();

View File

@@ -1,12 +1,25 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: manu
* Date: 11/07/13
* Time: 10:34
* To change this template use File | Settings | File Templates.
*/
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Tests\Command;