WIP : Coupon event dispatcher + action

This commit is contained in:
gmorel
2013-09-12 15:53:16 +02:00
parent 173d0ad743
commit 927b27d04a
6 changed files with 127 additions and 173 deletions

View File

@@ -23,10 +23,15 @@
namespace Thelia\Action; namespace Thelia\Action;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Constraint\ConstraintFactory; use Thelia\Constraint\ConstraintFactory;
use Thelia\Core\Event\Coupon\CouponConsumeEvent;
use Thelia\Core\Event\Coupon\CouponCreateOrUpdateEvent; use Thelia\Core\Event\Coupon\CouponCreateOrUpdateEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Coupon\CouponFactory;
use Thelia\Coupon\CouponManager;
use Thelia\Coupon\Type\CouponInterface;
use Thelia\Model\Coupon as CouponModel; 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 * 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) public function create(CouponCreateOrUpdateEvent $event)
{ {
@@ -57,7 +62,7 @@ class Coupon extends BaseAction implements EventSubscriberInterface
/** /**
* Occurring when a Coupon is about to be updated * 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) 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 * 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) 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 * 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( return array(
TheliaEvents::COUPON_CREATE => array("create", 128), TheliaEvents::COUPON_CREATE => array("create", 128),
TheliaEvents::COUPON_UPDATE => array("update", 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_CONSUME => array("consume", 128),
TheliaEvents::COUPON_RULE_UPDATE => array("updateRule", 128) TheliaEvents::COUPON_RULE_UPDATE => array("updateRule", 128)
); );

View File

@@ -111,7 +111,9 @@
<route id="admin.coupon.rule.update" path="/admin/coupon/{couponId}/rule/update/"> <route id="admin.coupon.rule.update" path="/admin/coupon/{couponId}/rule/update/">
<default key="_controller">Thelia\Controller\Admin\CouponController::updateRulesAction</default> <default key="_controller">Thelia\Controller\Admin\CouponController::updateRulesAction</default>
</route> </route>
<route id="admin.coupon.consume" path="/admin/coupon/consume/{couponCode}">
<default key="_controller">Thelia\Controller\Admin\CouponController::consumeAction</default>
</route>

View File

@@ -30,6 +30,7 @@ use Thelia\Constraint\ConstraintFactoryTest;
use Thelia\Constraint\Rule\AvailableForTotalAmount; use Thelia\Constraint\Rule\AvailableForTotalAmount;
use Thelia\Constraint\Rule\CouponRuleInterface; use Thelia\Constraint\Rule\CouponRuleInterface;
use Thelia\Constraint\Validator\PriceParam; use Thelia\Constraint\Validator\PriceParam;
use Thelia\Core\Event\Coupon\CouponConsumeEvent;
use Thelia\Core\Event\Coupon\CouponCreateEvent; use Thelia\Core\Event\Coupon\CouponCreateEvent;
use Thelia\Core\Event\Coupon\CouponCreateOrUpdateEvent; use Thelia\Core\Event\Coupon\CouponCreateOrUpdateEvent;
use Thelia\Core\Event\Coupon\CouponEvent; 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\Security\Exception\AuthorizationException;
use Thelia\Core\Translation\Translator; use Thelia\Core\Translation\Translator;
use Thelia\Coupon\CouponAdapterInterface; use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Coupon\CouponFactory;
use Thelia\Coupon\CouponManager; use Thelia\Coupon\CouponManager;
use Thelia\Coupon\CouponRuleCollection; use Thelia\Coupon\CouponRuleCollection;
use Thelia\Coupon\Type\CouponInterface; 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 * Build a Coupon from its form
* *

View File

@@ -22,6 +22,8 @@
/**********************************************************************************/ /**********************************************************************************/
namespace Thelia\Core\Event\Coupon; namespace Thelia\Core\Event\Coupon;
use Thelia\Core\Event\ActionEvent;
use Thelia\Coupon\CouponRuleCollection;
use Thelia\Model\Coupon; use Thelia\Model\Coupon;
/** /**
@@ -29,75 +31,112 @@ use Thelia\Model\Coupon;
* Date: 8/29/13 * Date: 8/29/13
* Time: 3:45 PM * Time: 3:45 PM
* *
* Occurring when a Coupon is disabled * Occurring when a Coupon is consumed
* *
* @package Coupon * @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr> * @author Guillaume MOREL <gmorel@openstudio.fr>
* *
*/ */
class CouponDisableEvent extends ActionEvent class CouponConsumeEvent extends ActionEvent
{ {
/** @var int Coupon id */ /** @var string Coupon code */
protected $couponId; protected $code = null;
/** @var Coupon Coupon being disabled */ /** @var float Total discount given by this coupon */
protected $disabledCoupon; protected $discount = 0;
/** @var bool If Coupon is valid or if Customer meets coupon conditions */
protected $isValid = null;
/** /**
* Constructor * 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 * @param string $code Coupon code
*/
public function getId()
{
return $this->id;
}
/**
* Set Coupon id
*
* @param int $id Coupon id
* *
* @return $this * @return $this
*/ */
public function setId($id) public function setCode($code)
{ {
$this->id = $id; $this->code = $code;
return $this; 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 * @return $this
*/ */
public function setDisabledCoupon(Coupon $disabledCoupon) public function setDiscount($discount)
{ {
$this->disabledCoupon = $disabledCoupon; $this->discount = $discount;
return $this; 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;
}
} }

View File

@@ -1,103 +0,0 @@
<?php
/**********************************************************************************/
/* */
/* 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\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 <gmorel@openstudio.fr>
*
*/
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;
}
}

View File

@@ -238,36 +238,6 @@ final class TheliaEvents
*/ */
const AFTER_UPDATE_COUPON = "action.after_update_coupon"; 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 * Sent when attempting to use a Coupon
*/ */