Improved condition code structure (still backward compatible)

This commit is contained in:
Franck Allimant
2014-05-10 01:37:09 +02:00
parent 38e16f0502
commit c39de8bf87
5 changed files with 193 additions and 283 deletions

View File

@@ -17,6 +17,7 @@ use Thelia\Condition\Operators;
use Thelia\Condition\SerializableCondition;
use Thelia\Core\Translation\Translator;
use Thelia\Coupon\FacadeInterface;
use Thelia\Exception\InvalidConditionOperatorException;
use Thelia\Exception\InvalidConditionValueException;
use Thelia\Model\Base\CurrencyQuery;
use Thelia\Model\Currency;
@@ -31,10 +32,6 @@ use Thelia\Type\FloatType;
*/
abstract class ConditionAbstract implements ConditionInterface
{
/** @var string Service Id from Resources/config.xml */
protected $serviceId = null;
/** @var array Available Operators (Operators::CONST) */
protected $availableOperators = [];
@@ -69,13 +66,27 @@ abstract class ConditionAbstract implements ConditionInterface
}
/**
* Return all available Operators for this Condition
* @param array $operatorList the list of comparison operator values, as entered in the condition parameter form
* @param string $parameterName the name of the parameter to check
*
* @return array Operators::CONST
* @return $this
*
* @throws \Thelia\Exception\InvalidConditionOperatorException if the operator value is not in the allowed value
*/
public function getAvailableOperators()
{
return $this->availableOperators;
protected function checkComparisonOperatorValue($operatorList, $parameterName) {
$isOperator1Legit = $this->isOperatorLegit(
$operatorList[$parameterName],
$this->availableOperators[$parameterName]
);
if (!$isOperator1Legit) {
throw new InvalidConditionOperatorException(
get_class(), $parameterName
);
}
return $this;
}
/**
@@ -88,8 +99,11 @@ abstract class ConditionAbstract implements ConditionInterface
$this->validators = $this->generateInputs();
$translatedInputs = [];
foreach ($this->validators as $key => $validator) {
$translatedOperators = [];
foreach ($validator['availableOperators'] as $availableOperators) {
$translatedOperators[$availableOperators] = Operators::getI18n(
$this->translator,
@@ -98,18 +112,23 @@ abstract class ConditionAbstract implements ConditionInterface
}
$validator['availableOperators'] = $translatedOperators;
$translatedInputs[$key] = $validator;
}
$validators = [];
$validators['inputs'] = $translatedInputs;
$validators['setOperators'] = $this->operators;
$validators['setValues'] = $this->values;
$validators = [
'inputs' => $translatedInputs,
'setOperators' => $this->operators,
'setValues' => $this->values
];
return $validators;
}
/**
* Generate inputs ready to be drawn
* Generate inputs ready to be drawn.
*
* TODO: what these "inputs ready to be drawn" is not clear.
*
* @throws \Thelia\Exception\NotImplementedException
* @return array
@@ -128,7 +147,9 @@ abstract class ConditionAbstract implements ConditionInterface
*/
public function getServiceId()
{
return $this->serviceId;
throw new \Thelia\Exception\NotImplementedException(
'The getServiceId method must be implemented in ' . get_class()
);
}
/**
@@ -152,7 +173,7 @@ abstract class ConditionAbstract implements ConditionInterface
public function getSerializableCondition()
{
$serializableCondition = new SerializableCondition();
$serializableCondition->conditionServiceId = $this->serviceId;
$serializableCondition->conditionServiceId = $this->getServiceId();
$serializableCondition->operators = $this->operators;
$serializableCondition->values = $this->values;

View File

@@ -14,6 +14,8 @@ namespace Thelia\Condition\Implementation;
use Thelia\Condition\SerializableCondition;
use Thelia\Coupon\FacadeInterface;
use Thelia\Exception\InvalidConditionOperatorException;
use Thelia\Exception\InvalidConditionValueException;
/**
* Manage how the application checks its state in order to check if it matches the implemented condition
@@ -41,10 +43,12 @@ interface ConditionInterface
/**
* Check validators relevancy and store them
*
* @param array $operators Operators the Admin set in BackOffice
* @param array $values Values the Admin set in BackOffice
* @param array $operators an array of operators (greater than, less than, etc.) entered in the condition parameter input form, one for each condition defined by the Condition
* @param array $values an array of values entered in in the condition parameter input form, one for each condition defined by the Condition
*
* @throws InvalidConditionOperatorException
* @throws InvalidConditionValueException
*
* @throws \InvalidArgumentException
* @return $this
*/
public function setValidatorsFromForm(array $operators, array $values);
@@ -56,13 +60,6 @@ interface ConditionInterface
*/
public function isMatching();
/**
* Return all available Operators for this condition
*
* @return array Operators::CONST
*/
public function getAvailableOperators();
/**
* Get I18n name
*
@@ -107,5 +104,4 @@ interface ConditionInterface
* @return string HTML string
*/
public function drawBackOfficeInputs();
}

View File

@@ -12,6 +12,8 @@
namespace Thelia\Condition\Implementation;
use Thelia\Coupon\FacadeInterface;
/**
* Allow every one, perform no check
*
@@ -21,35 +23,29 @@ namespace Thelia\Condition\Implementation;
*/
class MatchForEveryone extends ConditionAbstract
{
/** @var string Service Id from Resources/config.xml */
protected $serviceId = 'thelia.condition.match_for_everyone';
/** @var array Available Operators (Operators::CONST) */
protected $availableOperators = [];
/**
* Check validators relevancy and store them
*
* @param array $operators Operators the Admin set in BackOffice
* @param array $values Values the Admin set in BackOffice
*
* @throws \InvalidArgumentException
* @return $this
* @inheritdoc
*/
public function setValidatorsFromForm(array $operators, array $values)
{
$this->setValidators();
public function __construct(FacadeInterface $facade) {
return $this;
// Define the allowed comparison operators
$this->availableOperators = [];
parent::__construct($facade);
}
/**
* Check validators relevancy and store them
*
* @throws \InvalidArgumentException
* @return $this
* @inheritdoc
*/
protected function setValidators()
public function getServiceId()
{
return 'thelia.condition.match_for_everyone';
}
/**
* @inheritdoc
*/
public function setValidatorsFromForm(array $operators, array $values)
{
$this->operators = [];
$this->values = [];
@@ -58,9 +54,7 @@ class MatchForEveryone extends ConditionAbstract
}
/**
* Test if Customer meets conditions
*
* @return bool
* @inheritdoc
*/
public function isMatching()
{
@@ -68,9 +62,7 @@ class MatchForEveryone extends ConditionAbstract
}
/**
* Get I18n name
*
* @return string
* @inheritdoc
*/
public function getName()
{
@@ -82,10 +74,7 @@ class MatchForEveryone extends ConditionAbstract
}
/**
* Get I18n tooltip
* Explain in detail what the Condition checks
*
* @return string
* @inheritdoc
*/
public function getToolTip()
{
@@ -99,10 +88,7 @@ class MatchForEveryone extends ConditionAbstract
}
/**
* Get I18n summary
* Explain briefly the condition with given values
*
* @return string
* @inheritdoc
*/
public function getSummary()
{
@@ -116,20 +102,15 @@ class MatchForEveryone extends ConditionAbstract
}
/**
* Generate inputs ready to be drawn
*
* @return array
*/
* @inheritdoc
*/
protected function generateInputs()
{
return [];
}
/**
* Draw the input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @return string HTML string
* @inheritdoc
*/
public function drawBackOfficeInputs()
{

View File

@@ -13,7 +13,7 @@
namespace Thelia\Condition\Implementation;
use Thelia\Condition\Operators;
use Thelia\Exception\InvalidConditionOperatorException;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\Currency;
use Thelia\Model\CurrencyQuery;
@@ -22,99 +22,64 @@ use Thelia\Model\CurrencyQuery;
* Check if a Checkout total amount match criteria
*
* @package Condition
* @author Guillaume MOREL <gmorel@openstudio.fr>
* @author Guillaume MOREL <gmorel@openstudio.fr>, Franck Allimant <franck@cqfdev.fr>
*
*/
class MatchForTotalAmount extends ConditionAbstract
{
/** Condition 1st parameter : price */
CONST INPUT1 = 'price';
CONST CART_TOTAL = 'price';
/** Condition 1st parameter : currency */
CONST INPUT2 = 'currency';
CONST CART_CURRENCY = 'currency';
/** @var string Service Id from Resources/config.xml */
protected $serviceId = 'thelia.condition.match_for_total_amount';
/** @var array Available Operators (Operators::CONST) */
protected $availableOperators = array(
self::INPUT1 => array(
Operators::INFERIOR,
Operators::INFERIOR_OR_EQUAL,
Operators::EQUAL,
Operators::SUPERIOR_OR_EQUAL,
Operators::SUPERIOR
),
self::INPUT2 => array(
Operators::EQUAL,
)
);
/**
* Check validators relevancy and store them
*
* @param array $operators Operators the Admin set in BackOffice
* @param array $values Values the Admin set in BackOffice
*
* @throws \InvalidArgumentException
* @return $this
*/
public function setValidatorsFromForm(array $operators, array $values)
public function __construct(FacadeInterface $facade)
{
$this->setValidators(
$operators[self::INPUT1],
$values[self::INPUT1],
$operators[self::INPUT2],
$values[self::INPUT2]
);
// Define the allowed comparison operators
$this->availableOperators = [
self::CART_TOTAL => [
Operators::INFERIOR,
Operators::INFERIOR_OR_EQUAL,
Operators::EQUAL,
Operators::SUPERIOR_OR_EQUAL,
Operators::SUPERIOR
],
self::CART_CURRENCY => [
Operators::EQUAL,
]
];
return $this;
parent::__construct($facade);
}
/**
* Check validators relevancy and store them
*
* @param string $priceOperator Price Operator ex <
* @param float $priceValue Price set to meet condition
* @param string $currencyOperator Currency Operator ex =
* @param string $currencyValue Currency set to meet condition
*
* @throws \Thelia\Exception\InvalidConditionOperatorException
* @return $this
* @inheritdoc
*/
protected function setValidators($priceOperator, $priceValue, $currencyOperator, $currencyValue)
public function getServiceId()
{
$isOperator1Legit = $this->isOperatorLegit(
$priceOperator,
$this->availableOperators[self::INPUT1]
);
if (!$isOperator1Legit) {
throw new InvalidConditionOperatorException(
get_class(), 'price'
);
}
return 'thelia.condition.match_for_total_amount';
}
$isOperator1Legit = $this->isOperatorLegit(
$currencyOperator,
$this->availableOperators[self::INPUT2]
);
if (!$isOperator1Legit) {
throw new InvalidConditionOperatorException(
get_class(), 'price'
);
}
/**
* @inheritdoc
*/
public function setValidatorsFromForm(array $operators, array $values)
{
$this
->checkComparisonOperatorValue($operators, self::CART_TOTAL)
->checkComparisonOperatorValue($operators, self::CART_CURRENCY);
$this->isPriceValid($priceValue);
$this->isPriceValid($values[self::CART_TOTAL]);
$this->isCurrencyValid($currencyValue);
$this->isCurrencyValid($values[self::CART_CURRENCY]);
$this->operators = array(
self::INPUT1 => $priceOperator,
self::INPUT2 => $currencyOperator,
self::CART_TOTAL => $operators[self::CART_TOTAL],
self::CART_CURRENCY => $operators[self::CART_CURRENCY],
);
$this->values = array(
self::INPUT1 => $priceValue,
self::INPUT2 => $currencyValue,
self::CART_TOTAL => $values[self::CART_TOTAL],
self::CART_CURRENCY => $values[self::CART_CURRENCY],
);
return $this;
@@ -129,25 +94,27 @@ class MatchForTotalAmount extends ConditionAbstract
{
$condition1 = $this->conditionValidator->variableOpComparison(
$this->facade->getCartTotalPrice(),
$this->operators[self::INPUT1],
$this->values[self::INPUT1]
$this->operators[self::CART_TOTAL],
$this->values[self::CART_TOTAL]
);
$condition2 = $this->conditionValidator->variableOpComparison(
$this->facade->getCheckoutCurrency(),
$this->operators[self::INPUT2],
$this->values[self::INPUT2]
);
if ($condition1 && $condition2) {
return true;
if ($condition1) {
$condition2 = $this->conditionValidator->variableOpComparison(
$this->facade->getCheckoutCurrency(),
$this->operators[self::CART_CURRENCY],
$this->values[self::CART_CURRENCY]
);
if ($condition2) {
return true;
}
}
return false;
}
/**
* Get I18n name
*
* @return string
* @inheritdoc
*/
public function getName()
{
@@ -159,10 +126,7 @@ class MatchForTotalAmount extends ConditionAbstract
}
/**
* Get I18n tooltip
* Explain in detail what the Condition checks
*
* @return string
* @inheritdoc
*/
public function getToolTip()
{
@@ -176,23 +140,21 @@ class MatchForTotalAmount extends ConditionAbstract
}
/**
* Get I18n summary
* Explain briefly the condition with given values
*
* @return string
* @inheritdoc
*/
public function getSummary()
{
$i18nOperator = Operators::getI18n(
$this->translator, $this->operators[self::INPUT1]
$this->translator,
$this->operators[self::CART_TOTAL]
);
$toolTip = $this->translator->trans(
'If cart total amount is <strong>%operator%</strong> %amount% %currency%',
array(
'%operator%' => $i18nOperator,
'%amount%' => $this->values[self::INPUT1],
'%currency%' => $this->values[self::INPUT2]
'%amount%' => $this->values[self::CART_TOTAL],
'%currency%' => $this->values[self::CART_CURRENCY]
),
'condition'
);
@@ -201,28 +163,28 @@ class MatchForTotalAmount extends ConditionAbstract
}
/**
* Generate inputs ready to be drawn
*
* @return array
* @inheritdoc
*/
protected function generateInputs()
{
$currencies = CurrencyQuery::create()->find();
$cleanedCurrencies = [];
/** @var Currency $currency */
foreach ($currencies as $currency) {
$cleanedCurrencies[$currency->getCode()] = $currency->getSymbol();
}
return array(
self::INPUT1 => array(
'availableOperators' => $this->availableOperators[self::INPUT1],
self::CART_TOTAL => array(
'availableOperators' => $this->availableOperators[self::CART_TOTAL],
'availableValues' => '',
'value' => '',
'selectedOperator' => ''
),
self::INPUT2 => array(
'availableOperators' => $this->availableOperators[self::INPUT2],
self::CART_CURRENCY => array(
'availableOperators' => $this->availableOperators[self::CART_CURRENCY],
'availableValues' => $cleanedCurrencies,
'value' => '',
'selectedOperator' => Operators::EQUAL
@@ -231,10 +193,7 @@ class MatchForTotalAmount extends ConditionAbstract
}
/**
* Draw the input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @return string HTML string
* @inheritdoc
*/
public function drawBackOfficeInputs()
{
@@ -242,32 +201,26 @@ class MatchForTotalAmount extends ConditionAbstract
->getTranslator()
->trans('Cart total amount is', [], 'condition');
$html = $this->drawBackOfficeBaseInputsText($labelPrice, self::INPUT1);
$html = $this->drawBackOfficeBaseInputsText($labelPrice, self::CART_TOTAL);
return $html;
}
/**
* Draw the base input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @param string $label I18n input label
* @param string $inputKey Input key (ex: self::INPUT1)
*
* @return string HTML string
* @inheritdoc
*/
protected function drawBackOfficeBaseInputsText($label, $inputKey)
{
return $this->facade->getParser()->render('coupon/condition-fragments/cart-total-amount-condition.html', [
'label' => $label,
'inputKey' => $inputKey,
'value' => isset($this->values[$inputKey]) ? $this->values[$inputKey] : '',
'field_1_name' => self::INPUT1,
'field_2_name' => self::INPUT2,
'operatorSelectHtml' => $this->drawBackOfficeInputOperators(self::INPUT1),
'currencySelectHtml' => $this->drawBackOfficeCurrencyInput(self::INPUT2),
return $this->facade->getParser()->render(
'coupon/condition-fragments/cart-total-amount-condition.html',
[
'label' => $label,
'inputKey' => $inputKey,
'value' => isset($this->values[$inputKey]) ? $this->values[$inputKey] : '',
'field_1_name' => self::CART_TOTAL,
'field_2_name' => self::CART_CURRENCY,
'operatorSelectHtml' => $this->drawBackOfficeInputOperators(self::CART_TOTAL),
'currencySelectHtml' => $this->drawBackOfficeCurrencyInput(self::CART_CURRENCY),
]
);
}

View File

@@ -13,6 +13,7 @@
namespace Thelia\Condition\Implementation;
use Thelia\Condition\Operators;
use Thelia\Coupon\FacadeInterface;
use Thelia\Exception\InvalidConditionOperatorException;
use Thelia\Exception\InvalidConditionValueException;
@@ -20,96 +21,73 @@ use Thelia\Exception\InvalidConditionValueException;
* Check a Checkout against its Product number
*
* @package Condition
* @author Guillaume MOREL <gmorel@openstudio.fr>
* @author Guillaume MOREL <gmorel@openstudio.fr>, Franck Allimant <franck@cqfdev.fr>
*
*/
class MatchForXArticles extends ConditionAbstract
{
/** Condition 1st parameter : quantity */
CONST INPUT1 = 'quantity';
/** @var string Service Id from Resources/config.xml */
protected $serviceId = 'thelia.condition.match_for_x_articles';
/** @var array Available Operators (Operators::CONST) */
protected $availableOperators = array(
self::INPUT1 => array(
Operators::INFERIOR,
Operators::INFERIOR_OR_EQUAL,
Operators::EQUAL,
Operators::SUPERIOR_OR_EQUAL,
Operators::SUPERIOR
)
);
CONST CART_QUANTITY = 'quantity';
/**
* Check validators relevancy and store them
*
* @param array $operators Operators the Admin set in BackOffice
* @param array $values Values the Admin set in BackOffice
*
* @throws \InvalidArgumentException
* @return $this
* @inheritdoc
*/
public function setValidatorsFromForm(array $operators, array $values)
public function __construct(FacadeInterface $facade)
{
$this->setValidators(
$operators[self::INPUT1],
$values[self::INPUT1]
$this->availableOperators = array(
self::CART_QUANTITY => array(
Operators::INFERIOR,
Operators::INFERIOR_OR_EQUAL,
Operators::EQUAL,
Operators::SUPERIOR_OR_EQUAL,
Operators::SUPERIOR
)
);
return $this;
parent::__construct($facade);
}
/**
* Check validators relevancy and store them
*
* @param string $quantityOperator Quantity Operator ex <
* @param int $quantityValue Quantity set to meet condition
*
* @throws \Thelia\Exception\InvalidConditionValueException
* @throws \Thelia\Exception\InvalidConditionOperatorException
* @return $this
* @inheritdoc
*/
protected function setValidators($quantityOperator, $quantityValue)
public function getServiceId()
{
$isOperator1Legit = $this->isOperatorLegit(
$quantityOperator,
$this->availableOperators[self::INPUT1]
);
if (!$isOperator1Legit) {
throw new InvalidConditionOperatorException(
get_class(), 'quantity'
);
}
return 'thelia.condition.match_for_x_articles';
}
if ((int) $quantityValue <= 0) {
/**
* @inheritdoc
*/
public function setValidatorsFromForm(array $operators, array $values)
{
$this->checkComparisonOperatorValue($operators, self::CART_QUANTITY);
if (intval($values[self::CART_QUANTITY]) <= 0) {
throw new InvalidConditionValueException(
get_class(), 'quantity'
);
}
$this->operators = array(
self::INPUT1 => $quantityOperator,
);
$this->values = array(
self::INPUT1 => $quantityValue,
);
$this->operators = [
self::CART_QUANTITY => $operators[self::CART_QUANTITY]
];
$this->values = [
self::CART_QUANTITY => $values[self::CART_QUANTITY]
];
return $this;
}
/**
* Test if Customer meets conditions
*
* @return bool
* @inheritdoc
*/
public function isMatching()
{
$condition1 = $this->conditionValidator->variableOpComparison(
$this->facade->getNbArticlesInCart(),
$this->operators[self::INPUT1],
$this->values[self::INPUT1]
$this->operators[self::CART_QUANTITY],
$this->values[self::CART_QUANTITY]
);
if ($condition1) {
@@ -120,9 +98,7 @@ class MatchForXArticles extends ConditionAbstract
}
/**
* Get I18n name
*
* @return string
* @inheritdoc
*/
public function getName()
{
@@ -134,10 +110,7 @@ class MatchForXArticles extends ConditionAbstract
}
/**
* Get I18n tooltip
* Explain in detail what the Condition checks
*
* @return string
* @inheritdoc
*/
public function getToolTip()
{
@@ -151,22 +124,19 @@ class MatchForXArticles extends ConditionAbstract
}
/**
* Get I18n summary
* Explain briefly the condition with given values
*
* @return string
* @inheritdoc
*/
public function getSummary()
{
$i18nOperator = Operators::getI18n(
$this->translator, $this->operators[self::INPUT1]
$this->translator, $this->operators[self::CART_QUANTITY]
);
$toolTip = $this->translator->trans(
'If cart item count is <strong>%operator%</strong> %quantity%',
array(
'%operator%' => $i18nOperator,
'%quantity%' => $this->values[self::INPUT1]
'%quantity%' => $this->values[self::CART_QUANTITY]
),
'condition'
);
@@ -175,15 +145,13 @@ class MatchForXArticles extends ConditionAbstract
}
/**
* Generate inputs ready to be drawn
*
* @return array
* @inheritdoc
*/
protected function generateInputs()
{
return array(
self::INPUT1 => array(
'availableOperators' => $this->availableOperators[self::INPUT1],
self::CART_QUANTITY => array(
'availableOperators' => $this->availableOperators[self::CART_QUANTITY],
'value' => '',
'selectedOperator' => ''
)
@@ -191,10 +159,7 @@ class MatchForXArticles extends ConditionAbstract
}
/**
* Draw the input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @return string HTML string
* @inheritdoc
*/
public function drawBackOfficeInputs()
{
@@ -202,19 +167,13 @@ class MatchForXArticles extends ConditionAbstract
->getTranslator()
->trans('Cart item count is', [], 'condition');
$html = $this->drawBackOfficeBaseInputsText($labelQuantity, self::INPUT1);
$html = $this->drawBackOfficeBaseInputsText($labelQuantity, self::CART_QUANTITY);
return $html;
}
/**
* Draw the base input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @param string $label I18n input label
* @param string $inputKey Input key (ex: self::INPUT1)
*
* @return string HTML string
* @inheritdoc
*/
protected function drawBackOfficeBaseInputsText($label, $inputKey)
{