WIP Coupon

Refactor : creating dedicated reusable module for Constraints
Adding ConstraintManager
Secured :
- Effects : RemoveXPercent + RemoveXAmount
- Validators : all  except ModelParam (need CustomerModelParam, AreaModelParam, CountryModelParam ?)
- Conditions : AvailableForTotalAmount
This commit is contained in:
gmorel
2013-08-28 09:33:33 +02:00
parent c24e82758f
commit 50d50d3b91
19 changed files with 350 additions and 265 deletions

View File

@@ -61,14 +61,16 @@ class AvailableForTotalAmount extends CouponRuleAbstract
/**
* Constructor
*
* @param array $validators Array of RuleValidator
* validating $paramsToValidate against
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
* @param array $validators Array of RuleValidator
* validating $paramsToValidate against
*
* @throws \Thelia\Exception\InvalidRuleException
*/
public function __construct(array $validators)
public function __construct(CouponAdapterInterface $adapter, array $validators)
{
parent::__construct($validators);
parent::__construct($adapter, $validators);
if (isset($validators[self::PARAM1_PRICE])
&& $validators[self::PARAM1_PRICE] instanceof RuleValidator
@@ -77,7 +79,6 @@ class AvailableForTotalAmount extends CouponRuleAbstract
} else {
throw new InvalidRuleException(get_class());
}
}
@@ -109,8 +110,6 @@ class AvailableForTotalAmount extends CouponRuleAbstract
$this->checkBackOfficeInputsOperators();
return $this->isPriceValid($price->getPrice());
}
@@ -129,9 +128,9 @@ class AvailableForTotalAmount extends CouponRuleAbstract
throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
}
$quantity = $this->paramsToValidate[self::PARAM1_PRICE];
$price = $this->paramsToValidate[self::PARAM1_PRICE];
return $this->isPriceValid($quantity);
return $this->isPriceValid($price);
}
/**
@@ -157,15 +156,12 @@ class AvailableForTotalAmount extends CouponRuleAbstract
/**
* Generate current Rule param to be validated from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @return $this
*/
protected function setParametersToValidate(CouponAdapterInterface $adapter)
protected function setParametersToValidate()
{
$this->paramsToValidate = array(
self::PARAM1_PRICE => $adapter->getCartTotalPrice()
self::PARAM1_PRICE => $this->adapter->getCartTotalPrice()
);
return $this;

View File

@@ -23,6 +23,11 @@
namespace Thelia\Constraint\Rule;
use Thelia\Constraint\Validator\QuantityParam;
use Thelia\Constraint\Validator\RuleValidator;
use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Exception\InvalidRuleValueException;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
@@ -36,15 +41,90 @@ namespace Thelia\Constraint\Rule;
*/
class AvailableForXArticles extends CouponRuleAbstract
{
/** Rule 1st parameter : price */
CONST PARAM1_QUANTITY = 'quantity';
/** @var array Available Operators (Operators::CONST) */
protected $availableOperators = array(
Operators::INFERIOR,
Operators::EQUAL,
Operators::SUPERIOR,
);
/** @var RuleValidator Quantity Validator */
protected $quantityValidator = null;
/**
* Constructor
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
* @param array $validators Array of RuleValidator
* validating $paramsToValidate against
*
* @throws InvalidRuleException
*/
public function __construct(CouponAdapterInterface $adapter, array $validators)
{
parent::__construct($adapter, $validators);
if (isset($validators[self::PARAM1_QUANTITY])
&& $validators[self::PARAM1_QUANTITY] instanceof RuleValidator
) {
$this->quantityValidator = $validators[self::PARAM1_QUANTITY];
} else {
throw new InvalidRuleException(get_class());
}
$this->adapter = $adapter;
}
/**
* Check if backoffice inputs are relevant or not
*
* @throws InvalidRuleOperatorException if Operator is not allowed
* @throws InvalidRuleValueException if Value is not allowed
* @return bool
*/
public function checkBackOfficeInput()
{
// TODO: Implement checkBackOfficeInput() method.
if (!isset($this->validators)
|| empty($this->validators)
||!isset($this->validators[self::PARAM1_QUANTITY])
||!isset($this->validators[self::PARAM1_QUANTITY])
) {
throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY);
}
/** @var RuleValidator $ruleValidator */
$ruleValidator = $this->validators[self::PARAM1_QUANTITY];
/** @var QuantityParam $quantity */
$quantity = $ruleValidator->getParam();
if (!$quantity instanceof QuantityParam) {
throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY);
}
$this->checkBackOfficeInputsOperators();
return $this->isQuantityValid($quantity->getInteger());
}
/**
* Generate current Rule param to be validated from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @return $this
*/
protected function setParametersToValidate()
{
$this->paramsToValidate = array(
self::PARAM1_QUANTITY => $this->adapter->getCartTotalPrice()
);
return $this;
}
/**
@@ -57,4 +137,61 @@ class AvailableForXArticles extends CouponRuleAbstract
// TODO: Implement checkCheckoutInput() method.
}
/**
* Check if a quantity is valid
*
* @param float $quantity Quantity to check
*
* @throws InvalidRuleValueException if Value is not allowed
* @return bool
*/
protected function isQuantityValid($quantity)
{
$quantityValidator = $this->quantityValidator;
try {
$quantityValidator->getParam()->compareTo($quantity);
} catch(\InvalidArgumentException $e) {
throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY);
}
return true;
}
/**
* Get I18n name
*
* @return string
*/
public function getName()
{
return $this->adapter
->getTranslator()
->trans('Number of articles in cart', null, 'constraint');
}
/**
* Get I18n tooltip
*
* @return string
*/
public function getToolTip()
{
$i18nOperator = Operators::getI18n(
$this->adapter, $this->priceValidator->getOperator()
);
$toolTip = $this->adapter
->getTranslator()
->trans(
'If cart products quantity is %operator% %quantity%',
array(
'%operator%' => $i18nOperator,
'%quantity%' => $this->quantityValidator->getParam()->getInteger(),
),
'constraint'
);
return $toolTip;
}
}

View File

@@ -57,6 +57,9 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
/** @var array Parameters to be validated */
protected $paramsToValidate = array();
/** @var CouponAdapterInterface Provide necessary value from Thelia */
protected $adapter = null;
/**
* Constructor
* Ex:
@@ -70,14 +73,16 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
* Param 2 :
* $paramsToValidate[AvailableForTotalAmount::PARAM1_PRICE] = 9
*
* @param array $validators Array of RuleValidator
* validating $paramsToValidate against
*
* @throws InvalidRuleException
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
* @param array $validators Array of RuleValidator
* validating $paramsToValidate against
*/
public function __construct(array $validators)
public function __construct(CouponAdapterInterface $adapter, array $validators)
{
$this->setValidators($validators);
$this->adapter = $adapter;
$this->setParametersToValidate($this->adapter);
}
/**
@@ -104,14 +109,10 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
/**
* Check if the current Checkout matches this condition
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @return bool
*/
public function isMatching(CouponAdapterInterface $adapter)
public function isMatching()
{
$this->setParametersToValidate($adapter);
$this->checkBackOfficeInput();
$this->checkCheckoutInput();
@@ -165,13 +166,10 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
/**
* Generate current Rule param to be validated from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @throws \Thelia\Exception\NotImplementedException
* @return $this
*/
protected function setParametersToValidate(CouponAdapterInterface $adapter)
protected function setParametersToValidate()
{
throw new \Thelia\Exception\NotImplementedException();
}

View File

@@ -55,12 +55,9 @@ interface CouponRuleInterface
/**
* Check if the current Checkout matches this condition
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @return bool
*/
public function isMatching(CouponAdapterInterface $adapter);
public function isMatching();
/**
* Return all available Operators for this Rule