From 69059ea6b93f2a4dc095aabe35106c9ef6fd1d59 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Thu, 25 Jul 2013 11:14:51 +0200 Subject: [PATCH] Session::getCart return a Thelia\Model\Cart instance or null if cart is not valid --- .../Core/HttpFoundation/Session/Session.php | 56 +++++++++++++------ 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php index 9c6358dbf..a50ab92f5 100755 --- a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php +++ b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php @@ -25,13 +25,14 @@ 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 { - - // -- Language ------------------------------------------------------------ +class Session extends BaseSession +{ + // -- Language ------------------------------------------------------------ public function getLocale() { @@ -47,34 +48,34 @@ class Session extends BaseSession { public function setCustomerUser(UserInterface $user) { - $this->set('customer_user', $user); + $this->set('customer_user', $user); } public function getCustomerUser() { - return $this->get('customer_user'); + return $this->get('customer_user'); } public function clearCustomerUser() { - return $this->remove('customer_user'); + return $this->remove('customer_user'); } // -- Admin user ----------------------------------------------------------- public function setAdminUser(UserInterface $user) { - $this->set('admin_user', $user); + $this->set('admin_user', $user); } public function getAdminUser() { - return $this->get('admin_user'); + return $this->get('admin_user'); } public function clearAdminUser() { - return $this->remove('admin_user'); + return $this->remove('admin_user'); } // -- Error form ----------------------------------------------------------- @@ -84,24 +85,24 @@ class Session extends BaseSession { */ public function setErrorFormName($formName) { - $this->set('error_form', $formName); + $this->set('error_form', $formName); } public function getErrorFormName() { - return $this->get('error_form', null); + return $this->get('error_form', null); } public function clearErrorFormName() { - return $this->remove('error_form'); + return $this->remove('error_form'); } // -- Return page ---------------------------------------------------------- public function setReturnToUrl($url) { - $this->set('return_to_url', $url); + $this->set('return_to_url', $url); } /** @@ -110,7 +111,7 @@ class Session extends BaseSession { */ public function getReturnToUrl() { - return $this->get('return_to_url', URL::getIndexPage()); + return $this->get('return_to_url', URL::getIndexPage()); } // -- Cart ------------------------------------------------------------------ @@ -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"); + } } /**