From 927b27d04ad5b76ead83590709ffd4214fa1ae01 Mon Sep 17 00:00:00 2001 From: gmorel Date: Thu, 12 Sep 2013 15:53:16 +0200 Subject: [PATCH] WIP : Coupon event dispatcher + action --- core/lib/Thelia/Action/Coupon.php | 37 +++++-- .../Thelia/Config/Resources/routing/admin.xml | 4 +- .../Controller/Admin/CouponController.php | 25 +++++ ...isableEvent.php => CouponConsumeEvent.php} | 101 +++++++++++------ .../Core/Event/Coupon/CouponEnableEvent.php | 103 ------------------ core/lib/Thelia/Core/Event/TheliaEvents.php | 30 ----- 6 files changed, 127 insertions(+), 173 deletions(-) rename core/lib/Thelia/Core/Event/Coupon/{CouponDisableEvent.php => CouponConsumeEvent.php} (54%) delete mode 100644 core/lib/Thelia/Core/Event/Coupon/CouponEnableEvent.php diff --git a/core/lib/Thelia/Action/Coupon.php b/core/lib/Thelia/Action/Coupon.php index d50f72aa8..cdaa3424a 100755 --- a/core/lib/Thelia/Action/Coupon.php +++ b/core/lib/Thelia/Action/Coupon.php @@ -23,10 +23,15 @@ namespace Thelia\Action; +use Symfony\Component\Config\Definition\Exception\Exception; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Thelia\Constraint\ConstraintFactory; +use Thelia\Core\Event\Coupon\CouponConsumeEvent; use Thelia\Core\Event\Coupon\CouponCreateOrUpdateEvent; use Thelia\Core\Event\TheliaEvents; +use Thelia\Coupon\CouponFactory; +use Thelia\Coupon\CouponManager; +use Thelia\Coupon\Type\CouponInterface; use Thelia\Model\Coupon as CouponModel; /** @@ -45,7 +50,7 @@ class Coupon extends BaseAction implements EventSubscriberInterface /** * Occurring when a Coupon is about to be created * - * @param CouponCreateOrUpdateEvent $event Event creation or update Event + * @param CouponCreateOrUpdateEvent $event Event creation or update Coupon */ public function create(CouponCreateOrUpdateEvent $event) { @@ -57,7 +62,7 @@ class Coupon extends BaseAction implements EventSubscriberInterface /** * Occurring when a Coupon is about to be updated * - * @param CouponCreateOrUpdateEvent $event Event creation or update Event + * @param CouponCreateOrUpdateEvent $event Event creation or update Coupon */ public function update(CouponCreateOrUpdateEvent $event) { @@ -69,7 +74,7 @@ class Coupon extends BaseAction implements EventSubscriberInterface /** * Occurring when a Coupon rule is about to be updated * - * @param CouponCreateOrUpdateEvent $event Event creation or update Event + * @param CouponCreateOrUpdateEvent $event Event creation or update Coupon Rule */ public function updateRule(CouponCreateOrUpdateEvent $event) { @@ -81,11 +86,29 @@ class Coupon extends BaseAction implements EventSubscriberInterface /** * Occurring when a Coupon rule is about to be consumed * - * @param CouponCreateOrUpdateEvent $event Event creation or update Event + * @param CouponConsumeEvent $event Event consuming Coupon */ - public function consume(CouponCreateOrUpdateEvent $event) + public function consume(CouponConsumeEvent $event) { - // @todo implements + $totalDiscount = 0; + + /** @var CouponFactory $couponFactory */ + $couponFactory = $this->container->get('thelia.coupon.factory'); + + /** @var CouponManager $couponManager */ + $couponManager = $this->container->get('thelia.coupon.manager'); + + /** @var CouponInterface $coupon */ + $coupon = $couponFactory->buildCouponFromCode($event->getCode()); + + + $isValid = $coupon->isMatching(); + if ($isValid) { + $totalDiscount = $couponManager->getDiscount(); + } + + $event->setIsValid($isValid); + $event->setDiscount($totalDiscount); } /** @@ -165,8 +188,6 @@ class Coupon extends BaseAction implements EventSubscriberInterface return array( TheliaEvents::COUPON_CREATE => array("create", 128), TheliaEvents::COUPON_UPDATE => array("update", 128), - TheliaEvents::COUPON_DISABLE => array("disable", 128), - TheliaEvents::COUPON_ENABLE => array("enable", 128), TheliaEvents::COUPON_CONSUME => array("consume", 128), TheliaEvents::COUPON_RULE_UPDATE => array("updateRule", 128) ); diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 5e34aec39..5fe4c452a 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -111,7 +111,9 @@ Thelia\Controller\Admin\CouponController::updateRulesAction - + + Thelia\Controller\Admin\CouponController::consumeAction + diff --git a/core/lib/Thelia/Controller/Admin/CouponController.php b/core/lib/Thelia/Controller/Admin/CouponController.php index cd8521d7a..9718e655d 100755 --- a/core/lib/Thelia/Controller/Admin/CouponController.php +++ b/core/lib/Thelia/Controller/Admin/CouponController.php @@ -30,6 +30,7 @@ use Thelia\Constraint\ConstraintFactoryTest; use Thelia\Constraint\Rule\AvailableForTotalAmount; use Thelia\Constraint\Rule\CouponRuleInterface; use Thelia\Constraint\Validator\PriceParam; +use Thelia\Core\Event\Coupon\CouponConsumeEvent; use Thelia\Core\Event\Coupon\CouponCreateEvent; use Thelia\Core\Event\Coupon\CouponCreateOrUpdateEvent; use Thelia\Core\Event\Coupon\CouponEvent; @@ -39,6 +40,7 @@ use Thelia\Core\Security\Exception\AuthenticationException; use Thelia\Core\Security\Exception\AuthorizationException; use Thelia\Core\Translation\Translator; use Thelia\Coupon\CouponAdapterInterface; +use Thelia\Coupon\CouponFactory; use Thelia\Coupon\CouponManager; use Thelia\Coupon\CouponRuleCollection; use Thelia\Coupon\Type\CouponInterface; @@ -475,6 +477,29 @@ class CouponController extends BaseAdminController ); } + /** + * Test Coupon consuming + * + * @param string $couponCode Coupon code + * + */ + public function consumeAction($couponCode) + { + // @todo remove (event dispatcher testing purpose) + $couponConsumeEvent = new CouponConsumeEvent($couponCode); + $eventToDispatch = TheliaEvents::COUPON_CONSUME; + + // Dispatch Event to the Action + $this->dispatch( + $eventToDispatch, + $couponConsumeEvent + ); + + var_dump('test'); + + exit(); + } + /** * Build a Coupon from its form * diff --git a/core/lib/Thelia/Core/Event/Coupon/CouponDisableEvent.php b/core/lib/Thelia/Core/Event/Coupon/CouponConsumeEvent.php similarity index 54% rename from core/lib/Thelia/Core/Event/Coupon/CouponDisableEvent.php rename to core/lib/Thelia/Core/Event/Coupon/CouponConsumeEvent.php index db8e14243..df5cef7a9 100644 --- a/core/lib/Thelia/Core/Event/Coupon/CouponDisableEvent.php +++ b/core/lib/Thelia/Core/Event/Coupon/CouponConsumeEvent.php @@ -22,6 +22,8 @@ /**********************************************************************************/ namespace Thelia\Core\Event\Coupon; +use Thelia\Core\Event\ActionEvent; +use Thelia\Coupon\CouponRuleCollection; use Thelia\Model\Coupon; /** @@ -29,75 +31,112 @@ use Thelia\Model\Coupon; * Date: 8/29/13 * Time: 3:45 PM * - * Occurring when a Coupon is disabled + * Occurring when a Coupon is consumed * * @package Coupon * @author Guillaume MOREL * */ -class CouponDisableEvent extends ActionEvent +class CouponConsumeEvent extends ActionEvent { - /** @var int Coupon id */ - protected $couponId; + /** @var string Coupon code */ + protected $code = null; - /** @var Coupon Coupon being disabled */ - protected $disabledCoupon; + /** @var float Total discount given by this coupon */ + protected $discount = 0; + + /** @var bool If Coupon is valid or if Customer meets coupon conditions */ + protected $isValid = null; /** * Constructor * - * @param int $id Coupon Id + * @param string $code Coupon code + * @param float $discount Total discount given by this coupon + * @param bool $isValid If Coupon is valid or + * if Customer meets coupon conditions */ - public function __construct($id) + function __construct($code, $discount = null, $isValid = null) { - $this->id = $id; + $this->code = $code; + $this->discount = $discount; + $this->isValid = $isValid; } /** - * Get Coupon id + * Set Coupon code * - * @return int - */ - public function getId() - { - return $this->id; - } - - /** - * Set Coupon id - * - * @param int $id Coupon id + * @param string $code Coupon code * * @return $this */ - public function setId($id) + public function setCode($code) { - $this->id = $id; + $this->code = $code; return $this; } /** - * Get Coupon being disabled + * Get Coupon code * - * @return Coupon + * @return string */ - public function getDisabledCoupon() + public function getCode() { - return $this->disabledCoupon; + return $this->code; } /** - * Set Coupon to be disabled + * Set total discount given by this coupon * - * @param Coupon $disabledCoupon Coupon to disable + * @param float $discount Total discount given by this coupon * * @return $this */ - public function setDisabledCoupon(Coupon $disabledCoupon) + public function setDiscount($discount) { - $this->disabledCoupon = $disabledCoupon; + $this->discount = $discount; return $this; } + + /** + * Get total discount given by this coupon + * + * @return float + */ + public function getDiscount() + { + return $this->discount; + } + + /** + * Set if Coupon is valid or if Customer meets coupon conditions + * + * @param boolean $isValid if Coupon is valid or + * if Customer meets coupon conditions + * + * @return $this + */ + public function setIsValid($isValid) + { + $this->isValid = $isValid; + + return $this; + } + + /** + * Get if Coupon is valid or if Customer meets coupon conditions + * + * @return boolean + */ + public function getIsValid() + { + return $this->isValid; + } + + + + } diff --git a/core/lib/Thelia/Core/Event/Coupon/CouponEnableEvent.php b/core/lib/Thelia/Core/Event/Coupon/CouponEnableEvent.php deleted file mode 100644 index ab06953e5..000000000 --- a/core/lib/Thelia/Core/Event/Coupon/CouponEnableEvent.php +++ /dev/null @@ -1,103 +0,0 @@ -. */ -/* */ -/**********************************************************************************/ - -namespace Thelia\Core\Event\Coupon; -use Thelia\Model\Coupon; - -/** - * Created by JetBrains PhpStorm. - * Date: 8/29/13 - * Time: 3:45 PM - * - * Occurring when a Coupon is enabled - * - * @package Coupon - * @author Guillaume MOREL - * - */ -class CouponEnableEvent extends ActionEvent -{ - /** @var int Coupon id */ - protected $couponId; - - /** @var Coupon Coupon being enabled */ - protected $enabledCoupon; - - /** - * Constructor - * - * @param int $id Coupon Id - */ - public function __construct($id) - { - $this->id = $id; - } - - /** - * Get Coupon id - * - * @return int - */ - public function getId() - { - return $this->id; - } - - /** - * Set Coupon id - * - * @param int $id Coupon id - * - * @return $this - */ - public function setId($id) - { - $this->id = $id; - - return $this; - } - - /** - * Get Coupon being enabled - * - * @return Coupon - */ - public function getEnabledCoupon() - { - return $this->enabledCoupon; - } - - /** - * Set Coupon to be enabled - * - * @param Coupon $enabledCoupon Coupon to enabled - * - * @return $this - */ - public function setEnabledCoupon(Coupon $enabledCoupon) - { - $this->enabledCoupon = $enabledCoupon; - - return $this; - } -} diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 4cbaad675..c67963a2e 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -238,36 +238,6 @@ final class TheliaEvents */ const AFTER_UPDATE_COUPON = "action.after_update_coupon"; - /** - * Sent when disabling a Coupon - */ - const COUPON_DISABLE = "action.disable_coupon"; - - /** - * Sent just before a successful disable of a new Coupon in the database. - */ - const BEFORE_DISABLE_COUPON = "action.before_disable_coupon"; - - /** - * Sent just after a successful disable of a new Coupon in the database. - */ - const AFTER_DISABLE_COUPON = "action.after_disable_coupon"; - - /** - * Sent when enabling a Coupon - */ - const COUPON_ENABLE = "action.enable_coupon"; - - /** - * Sent just before a successful enable of a new Coupon in the database. - */ - const BEFORE_ENABLE_COUPON = "action.before_enable_coupon"; - - /** - * Sent just after a successful enable of a new Coupon in the database. - */ - const AFTER_ENABLE_COUPON = "action.after_enable_coupon"; - /** * Sent when attempting to use a Coupon */