Refactor rule => condition

adapter => facade
This commit is contained in:
gmorel
2013-11-23 18:35:21 +01:00
parent 59a04f6a09
commit 8ce1030178
11 changed files with 52 additions and 64 deletions

View File

@@ -35,7 +35,7 @@ use Thelia\Type\FloatType;
* Date: 8/19/13
* Time: 3:24 PM
*
* Assist in writing a condition of whether the Rule is applied or not
* Assist in writing a condition of whether the Condition is applied or not
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>

View File

@@ -47,7 +47,7 @@ interface ConditionManagerInterface
function __construct(FacadeInterface $adapter);
/**
* Get Rule Service id
* Get Condition Service id
*
* @return string
*/

View File

@@ -48,7 +48,7 @@ class CouponFactory
protected $container = null;
/** @var FacadeInterface Provide necessary value from Thelia*/
protected $adapter;
protected $facade;
/**
* Constructor
@@ -58,7 +58,7 @@ class CouponFactory
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->adapter = $container->get('thelia.facade');
$this->facade = $container->get('thelia.facade');
}
/**
@@ -74,7 +74,7 @@ class CouponFactory
public function buildCouponFromCode($couponCode)
{
/** @var Coupon $couponModel */
$couponModel = $this->adapter->findOneCouponByCode($couponCode);
$couponModel = $this->facade->findOneCouponByCode($couponCode);
if ($couponModel === null) {
throw new NotFoundResourceException(
'Coupon ' . $couponCode . ' not found in Database'
@@ -115,7 +115,7 @@ class CouponFactory
/** @var CouponInterface $couponManager*/
$couponManager = $this->container->get($model->getType());
$couponManager->set(
$this->adapter,
$this->facade,
$model->getCode(),
$model->getTitle(),
$model->getShortDescription(),

View File

@@ -42,7 +42,7 @@ use Thelia\Model\Coupon;
class CouponManager
{
/** @var FacadeInterface Provides necessary value from Thelia */
protected $adapter = null;
protected $facade = null;
/** @var ContainerInterface Service Container */
protected $container = null;
@@ -64,8 +64,8 @@ class CouponManager
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->adapter = $container->get('thelia.facade');
$this->coupons = $this->adapter->getCurrentCoupons();
$this->facade = $container->get('thelia.facade');
$this->coupons = $this->facade->getCurrentCoupons();
}
@@ -87,12 +87,12 @@ class CouponManager
$discount = $this->getEffect($couponsKept);
if ($isRemovingPostage) {
$postage = $this->adapter->getCheckoutPostagePrice();
$postage = $this->facade->getCheckoutPostagePrice();
$discount += $postage;
}
// Just In Case test
$checkoutTotalPrice = $this->adapter->getCartTotalPrice();
$checkoutTotalPrice = $this->facade->getCartTotalPrice();
if ($discount >= $checkoutTotalPrice) {
$discount = $checkoutTotalPrice;
}
@@ -164,7 +164,7 @@ class CouponManager
/** @var CouponInterface $coupon */
foreach ($coupons as $coupon) {
if ($coupon->isMatching($this->adapter)) {
if ($coupon->isMatching($this->facade)) {
$couponsKept[] = $coupon;
}
}
@@ -184,7 +184,7 @@ class CouponManager
$discount = 0.00;
/** @var CouponInterface $coupon */
foreach ($coupons as $coupon) {
$discount += $coupon->exec($this->adapter);
$discount += $coupon->exec($this->facade);
}
return $discount;

View File

@@ -45,7 +45,7 @@ use Thelia\Exception\InvalidConditionException;
abstract class CouponAbstract implements CouponInterface
{
/** @var FacadeInterface Provide necessary value from Thelia */
protected $adapter = null;
protected $facade = null;
/** @var Translator Service Translator */
protected $translator = null;
@@ -104,13 +104,13 @@ abstract class CouponAbstract implements CouponInterface
/**
* Constructor
*
* @param FacadeInterface $adapter Service adapter
* @param FacadeInterface $facade Service facade
*/
public function __construct(FacadeInterface $adapter)
public function __construct(FacadeInterface $facade)
{
$this->adapter = $adapter;
$this->translator = $adapter->getTranslator();
$this->conditionEvaluator = $adapter->getConditionEvaluator();
$this->facade = $facade;
$this->translator = $facade->getTranslator();
$this->conditionEvaluator = $facade->getConditionEvaluator();
}
/**

View File

@@ -24,6 +24,7 @@
namespace Thelia\Coupon\Type;
use Thelia\Condition\ConditionCollection;
use Thelia\Coupon\FacadeInterface;
/**
* Created by JetBrains PhpStorm.
@@ -62,7 +63,7 @@ interface CouponInterface
/**
* Set Coupon
*
* @param CouponInterface $adapter Provides necessary value from Thelia
* @param FacadeInterface $facade Provides necessary value from Thelia
* @param string $code Coupon code (ex: XMAS)
* @param string $title Coupon title (ex: Coupon for XMAS)
* @param string $shortDescription Coupon short description
@@ -77,7 +78,7 @@ interface CouponInterface
* @param \Datetime $expirationDate When the Code is expiring
*/
public function set(
$adapter,
FacadeInterface $facade,
$code,
$title,
$shortDescription,
@@ -144,15 +145,15 @@ interface CouponInterface
public function getConditions();
/**
* Replace the existing Rules by those given in parameter
* If one Rule is badly implemented, no Rule will be added
* Replace the existing Conditions by those given in parameter
* If one Condition is badly implemented, no Condition will be added
*
* @param ConditionCollection $rules ConditionManagerInterface to add
* @param ConditionCollection $conditions ConditionManagerInterface to add
*
* @return $this
* @throws \Thelia\Exception\InvalidConditionException
*/
public function setConditions(ConditionCollection $rules);
public function setConditions(ConditionCollection $conditions);
/**
* Return Coupon expiration date
@@ -198,7 +199,7 @@ interface CouponInterface
* A positive value
*
* Effects could also affect something else than the final Checkout price
* CouponAdapter $adapter could be use to directly pass a Session value
* FacadeInterface $facade could be used to directly pass a Session value
* some would wish to modify
* Hence affecting a wide variety of Thelia elements
*
@@ -207,7 +208,7 @@ interface CouponInterface
public function exec();
/**
* Check if the current Coupon is matching its conditions (Rules)
* Check if the current Coupon is matching its conditions
* Thelia variables are given by the FacadeInterface
*
* @return bool

View File

@@ -23,6 +23,7 @@
namespace Thelia\Coupon\Type;
use Thelia\Coupon\FacadeInterface;
use Thelia\Coupon\Type\CouponAbstract;
/**
@@ -44,7 +45,7 @@ class RemoveXAmountManager extends CouponAbstract
/**
* Set Coupon
*
* @param CouponInterface $adapter Provides necessary value from Thelia
* @param FacadeInterface $facade Provides necessary value from Thelia
* @param string $code Coupon code (ex: XMAS)
* @param string $title Coupon title (ex: Coupon for XMAS)
* @param string $shortDescription Coupon short description
@@ -59,7 +60,7 @@ class RemoveXAmountManager extends CouponAbstract
* @param \Datetime $expirationDate When the Code is expiring
*/
public function set(
$adapter,
FacadeInterface $facade,
$code,
$title,
$shortDescription,
@@ -87,7 +88,7 @@ class RemoveXAmountManager extends CouponAbstract
$this->isEnabled = $isEnabled;
$this->maxUsage = $maxUsage;
$this->expirationDate = $expirationDate;
$this->adapter = $adapter;
$this->facade = $facade;
}
/**
@@ -97,7 +98,7 @@ class RemoveXAmountManager extends CouponAbstract
*/
public function getName()
{
return $this->adapter
return $this->facade
->getTranslator()
->trans('Remove X amount to total cart', array(), 'constraint');
}
@@ -109,7 +110,7 @@ class RemoveXAmountManager extends CouponAbstract
*/
public function getToolTip()
{
$toolTip = $this->adapter
$toolTip = $this->facade
->getTranslator()
->trans(
'This coupon will remove the entered amount to the customer total checkout. If the discount is superior to the total checkout price the customer will only pay the postage. Unless if the coupon is set to remove postage too.',

View File

@@ -23,8 +23,9 @@
namespace Thelia\Coupon\Type;
use Thelia\Coupon\FacadeInterface;
use Thelia\Coupon\Type\CouponAbstract;
use Thelia\Exception\MissingAdapterException;
use Thelia\Exception\MissingFacadeException;
/**
* Created by JetBrains PhpStorm.
@@ -45,7 +46,7 @@ class RemoveXPercentManager extends CouponAbstract
/**
* Set Coupon
*
* @param CouponInterface $adapter Provides necessary value from Thelia
* @param FacadeInterface $facade Provides necessary value from Thelia
* @param string $code Coupon code (ex: XMAS)
* @param string $title Coupon title (ex: Coupon for XMAS)
* @param string $shortDescription Coupon short description
@@ -60,7 +61,7 @@ class RemoveXPercentManager extends CouponAbstract
* @param \Datetime $expirationDate When the Code is expiring
*/
public function set(
$adapter,
FacadeInterface $facade,
$code,
$title,
$shortDescription,
@@ -87,14 +88,14 @@ class RemoveXPercentManager extends CouponAbstract
$this->isEnabled = $isEnabled;
$this->maxUsage = $maxUsage;
$this->expirationDate = $expirationDate;
$this->adapter = $adapter;
$this->facade = $facade;
}
/**
* Return effects generated by the coupon
* A negative value
*
* @throws \Thelia\Exception\MissingAdapterException
* @throws \Thelia\Exception\MissingFacadeException
* @throws \InvalidArgumentException
* @return float
*/
@@ -106,7 +107,7 @@ class RemoveXPercentManager extends CouponAbstract
);
}
$basePrice = $this->adapter->getCartTotalPrice();
$basePrice = $this->facade->getCartTotalPrice();
return $basePrice * (( $this->percent ) / 100);
}
@@ -119,7 +120,7 @@ class RemoveXPercentManager extends CouponAbstract
*/
public function getName()
{
return $this->adapter
return $this->facade
->getTranslator()
->trans('Remove X percent to total cart', array(), 'constraint');
}
@@ -131,7 +132,7 @@ class RemoveXPercentManager extends CouponAbstract
*/
public function getToolTip()
{
$toolTip = $this->adapter
$toolTip = $this->facade
->getTranslator()
->trans(
'This coupon will remove the entered percentage to the customer total checkout. If the discount is superior to the total checkout price the customer will only pay the postage. Unless if the coupon is set to remove postage too.',

View File

@@ -30,13 +30,13 @@ use Thelia\Log\Tlog;
* Date: 8/19/13
* Time: 3:24 PM
*
* Thrown when the Adapter is not set
* Thrown when the Facade is not set
*
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class MissingAdapterException extends \RuntimeException
class MissingFacadeException extends \RuntimeException
{
/**
* {@inheritdoc}

View File

@@ -38,27 +38,6 @@ use Thelia\Condition\ConditionOrganizer;
*/
class ConditionOrganizerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ConditionOrganizer
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = new ConditionOrganizer;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
/**
* @covers Thelia\Coupon\RuleOrganizer::organize

View File

@@ -27,6 +27,7 @@ use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\ConditionFactory;
use Thelia\Condition\Implementation\MatchForTotalAmountManager;
use Thelia\Condition\Operators;
use Thelia\Coupon\Type\RemoveXAmountManager;
use Thelia\Model\Coupon;
use Thelia\Model\CurrencyQuery;
@@ -192,9 +193,14 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua
$stubFacade->expects($this->any())
->method('findOneCouponByCode')
->will($this->returnValue($expected));
$stubContainer->expects($this->any())
$stubContainer->expects($this->at(0))
->method('get')
->will($this->returnValue($stubFacade));
$couponManager = new RemoveXAmountManager();
$stubContainer->expects($this->at(1))
->method('get')
->will($this->returnValue($couponManager));
$stubContainer->expects($this->any())
->method('has')
->will($this->returnValue(true));