- Add Coupon, Rules, CouponManager, Adapter as Services
- Refactor Coupon to use these services
This commit is contained in:
gmorel
2013-09-06 11:47:00 +02:00
parent eea29cba06
commit 8a5e12f814
12 changed files with 190 additions and 149 deletions

View File

@@ -23,6 +23,9 @@
namespace Thelia\Constraint;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Thelia\Constraint\Rule\CouponRuleInterface;
use Thelia\Constraint\Rule\SerializableRule;
use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Coupon\CouponRuleCollection;
@@ -40,6 +43,9 @@ use Thelia\Coupon\CouponRuleCollection;
*/
class ConstraintManager
{
/** @var ContainerInterface Service Container */
protected $container = null;
/** @var CouponAdapterInterface Provide necessary value from Thelia*/
protected $adapter;
@@ -49,27 +55,28 @@ class ConstraintManager
/**
* Constructor
*
* @param CouponAdapterInterface $adapter Provide necessary value from Thelia
* @param CouponRuleCollection $rules Rules associated with the Constraint
* @param ContainerInterface $container Service container
*/
function __construct(CouponAdapterInterface $adapter, CouponRuleCollection $rules)
function __construct(ContainerInterface $container)
{
$this->adapter = $adapter;
$this->rule = $rules;
$this->container = $container;
$this->adapter = $container->get('thelia.adapter');
}
/**
* Check if the current Coupon is matching its conditions (Rules)
* Thelia variables are given by the CouponAdapterInterface
*
* @param CouponRuleCollection $collection A collection of rules
*
* @return bool
*/
public function isMatching()
public function isMatching(CouponRuleCollection $collection)
{
$isMatching = true;
/** @var CouponRuleInterface $rule */
foreach ($this->rules->getRules() as $rule) {
foreach ($collection->getRules() as $rule) {
if (!$rule->isMatching($this->adapter)) {
$isMatching = false;
}
@@ -78,5 +85,53 @@ class ConstraintManager
return $isMatching;
}
/**
* Serialize a collection of rules
*
* @param CouponRuleCollection $collection A collection of rules
*
* @return string A ready to be stored Rule collection
*/
public function serializeCouponRuleCollection(CouponRuleCollection $collection)
{
$serializableRules = array();
$rules = $collection->getRules();
if ($rules !== null) {
/** @var $rule CouponRuleInterface */
foreach ($rules as $rule) {
$serializableRules[] = $rule->getSerializableRule();
}
}
return (string) base64_encode(serialize($serializableRules));
}
/**
* Unserialize a collection of rules
*
* @param string $serializedRules Serialized Rules
*
* @return CouponRuleCollection Rules ready to be processed
*/
public function unserializeCouponRuleCollection($serializedRules)
{
$unserializedRules = unserialize(base64_decode($serializedRules));
$collection = new CouponRuleCollection();
if (!empty($serializedRules) && !empty($unserializedRules)) {
/** @var SerializableRule $rule */
foreach ($unserializedRules as $rule) {
if ($this->container->has($rule->ruleServiceId)) {
/** @var CouponRuleInterface $couponRule */
$couponRule = $this->container->get($rule->ruleServiceId);
$couponRule->populateFromForm(
$rule->operators,
$rule->values
);
$collection->add($couponRule);
}
}
}
return $collection;
}
}

View File

@@ -62,30 +62,6 @@ class AvailableForTotalAmount extends CouponRuleAbstract
/** @var PriceParam Price Validator */
protected $priceValidator = null;
/**
* Constructor
*
* @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(CouponAdapterInterface $adapter, array $validators)
{
parent::__construct($adapter, $validators);
if (isset($validators[self::PARAM1_PRICE])
&& $validators[self::PARAM1_PRICE] instanceof RuleValidator
) {
$this->priceValidator = $validators[self::PARAM1_PRICE];
} else {
throw new InvalidRuleException(get_class());
}
}
/**
* Check if backoffice inputs are relevant or not
*
@@ -171,17 +147,6 @@ class AvailableForTotalAmount extends CouponRuleAbstract
return $this;
}
/**
* Return all validators
* Serialization purpose
*
* @return array
*/
public function getValidators()
{
return $this->validators;
}
/**
* Get I18n name
*
@@ -203,13 +168,11 @@ class AvailableForTotalAmount extends CouponRuleAbstract
*/
public function getToolTip()
{
/** @var Translator $translator */
$translator = $this->get('thelia.translator');
$i18nOperator = Operators::getI18n(
$translator, $this->priceValidator->getOperator()
$this->translator, $this->priceValidator->getOperator()
);
$toolTip = $translator->trans(
$toolTip = $this->translator->trans(
'If cart total amount is <strong>%operator%</strong> %amount% %currency%',
array(
'%operator%' => $i18nOperator,

View File

@@ -56,31 +56,6 @@ class AvailableForXArticles extends CouponRuleAbstract
/** @var QuantityParam 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 = null)
{
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
*
@@ -176,10 +151,7 @@ class AvailableForXArticles extends CouponRuleAbstract
*/
public function getName()
{
/** @var Translator $translator */
$translator = $this->adapter->get('thelia.translator');
return $translator->trans(
return $this->translator->trans(
'Number of articles in cart',
array(),
'constraint'
@@ -193,14 +165,11 @@ class AvailableForXArticles extends CouponRuleAbstract
*/
public function getToolTip()
{
/** @var Translator $translator */
$translator = $this->adapter->get('thelia.translator');
$i18nOperator = Operators::getI18n(
$translator, $this->priceValidator->getOperator()
$this->translator, $this->priceValidator->getOperator()
);
$toolTip = $translator->trans(
$toolTip = $this->translator->trans(
'If cart products quantity is <strong>%operator%</strong> %quantity%',
array(
'%operator%' => $i18nOperator,
@@ -256,5 +225,4 @@ class AvailableForXArticles extends CouponRuleAbstract
return $serializableRule;
}
}

View File

@@ -24,6 +24,7 @@
namespace Thelia\Constraint\Rule;
use Symfony\Component\Intl\Exception\NotImplementedException;
use Symfony\Component\Translation\Translator;
use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Constraint\Validator\ComparableInterface;
use Thelia\Constraint\Validator\RuleValidator;
@@ -60,29 +61,17 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
/** @var CouponAdapterInterface Provide necessary value from Thelia */
protected $adapter = null;
/** @var Translator Service Translator */
protected $translator = null;
/**
* Constructor
* Ex:
* Param 1 :
* $priceValidator = new RuleValidator(
* Operators::INFERIOR,
* new IntegerParam(10)
* )
* $validators[AvailableForTotalAmount::PARAM1_PRICE] = $priceValidator
*
* Param 2 :
* $paramsToValidate[AvailableForTotalAmount::PARAM1_PRICE] = 9
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
* @param array $validators Array of RuleValidator
* validating $paramsToValidate against
* @param Translator $translator Service translator
*/
public function __construct(CouponAdapterInterface $adapter, array $validators)
function __construct(Translator $translator)
{
$this->setValidators($validators);
$this->adapter = $adapter;
$this->setParametersToValidate($this->adapter);
$this->translator($translator);
}
/**
@@ -180,4 +169,15 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
throw new \Thelia\Exception\NotImplementedException();
}
/**
* Return all validators
* Serialization purpose
*
* @return array
*/
public function getValidators()
{
return $this->validators;
}
}

View File

@@ -39,6 +39,13 @@ use Thelia\Coupon\CouponAdapterInterface;
*/
interface CouponRuleInterface
{
/**
* Constructor
*
* @param Translator $translator Service translator
*/
function __construct(Translator $translator);
/**
* Check if backoffice inputs are relevant or not
*

View File

@@ -37,7 +37,7 @@ namespace Thelia\Constraint\Rule;
class SerializableRule
{
/** @var string Rule Service id */
public $ruleClassName = null;
public $ruleServiceId = null;
/** @var array Operators set by Admin for this Rule */
public $operators = array();