Session::getCart return a Thelia\Model\Cart instance or null if cart is

not valid
This commit is contained in:
Manuel Raynaud
2013-07-25 11:14:51 +02:00
parent 12f78f999e
commit 69059ea6b9

View File

@@ -25,12 +25,13 @@ namespace Thelia\Core\HttpFoundation\Session;
use Symfony\Component\HttpFoundation\Session\Session as BaseSession;
use Thelia\Core\Security\User\UserInterface;
use Thelia\Form\BaseForm;
use Thelia\Model\ConfigQuery;
use Thelia\Exception\InvalidCartException;
use Thelia\Model\Base\CartQuery;
use Thelia\Model\Cart;
use Thelia\Tools\URL;
class Session extends BaseSession {
class Session extends BaseSession
{
// -- Language ------------------------------------------------------------
public function getLocale()
@@ -122,7 +123,28 @@ class Session extends BaseSession {
*/
public function getCart()
{
return $this->get("cart_id");
$cart_id = $this->get("cart_id");
$cart = null;
if ($cart_id) {
$cart = CartQuery::create()->findPk($cart_id);
try {
$this->verifyValidCart($cart);
} catch (InvalidCartException $e) {
$cart = null;
}
}
return $cart;
}
protected function verifyValidCart(Cart $cart)
{
$customer = $this->getCustomerUser();
if ($customer && $cart->getCustomerId() != $customer->getId()) {
throw new InvalidCartException("customer in session and customer_id in cart are not the same");
} else if($customer === null && $cart->getCustomerId() !== null) {
throw new InvalidCartException("Customer exists in cart and not in session");
}
}
/**