. */ /* */ /*************************************************************************************/ namespace Front\Controller; use Propel\Runtime\Exception\PropelException; use Thelia\Controller\Front\BaseFrontController; use Thelia\Core\Event\Coupon\CouponConsumeEvent; use Thelia\Form\CouponCode; use Thelia\Form\Exception\FormValidationException; use Thelia\Core\Event\TheliaEvents; use Thelia\Log\Tlog; use Thelia\Model\Order; /** * Class CouponController * @package Thelia\Controller\Front * @author Guillaume MOREL */ class CouponController extends BaseFrontController { /** * Test Coupon consuming */ public function consumeAction() { $this->checkAuth(); $this->checkCartNotEmpty(); $message = false; $couponCodeForm = new CouponCode($this->getRequest()); try { $form = $this->validateForm($couponCodeForm, 'post'); $couponCode = $form->get('coupon-code')->getData(); if (null === $couponCode || empty($couponCode)) { $message = true; throw new \Exception('Coupon code can\'t be empty'); } $couponConsumeEvent = new CouponConsumeEvent($couponCode); // Dispatch Event to the Action $this->getDispatcher()->dispatch(TheliaEvents::COUPON_CONSUME, $couponConsumeEvent); } catch (FormValidationException $e) { $message = sprintf('Please check your coupon code: %s', $e->getMessage()); } catch (PropelException $e) { $this->getParserContext()->setGeneralError($e->getMessage()); } catch (\Exception $e) { $message = sprintf('Sorry, an error occurred: %s', $e->getMessage()); } if ($message !== false) { Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage())); $couponCodeForm->setErrorMessage($message); $this->getParserContext() ->addForm($couponCodeForm) ->setGeneralError($message); } } }