Removed useless transactions

This commit is contained in:
Franck Allimant
2014-05-02 18:23:04 +02:00
parent 7996c866d3
commit 3b7589ec84

View File

@@ -1,223 +1,203 @@
<?php <?php
/**********************************************************************************/ /**********************************************************************************/
/* */ /* */
/* Thelia */ /* Thelia */
/* */ /* */
/* Copyright (c) OpenStudio */ /* Copyright (c) OpenStudio */
/* email : info@thelia.net */ /* email : info@thelia.net */
/* web : http://www.thelia.net */ /* web : http://www.thelia.net */
/* */ /* */
/* This program is free software; you can redistribute it and/or modify */ /* 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 */ /* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */ /* the Free Software Foundation; either version 3 of the License */
/* */ /* */
/* This program is distributed in the hope that it will be useful, */ /* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */ /* GNU General Public License for more details. */
/* */ /* */
/* You should have received a copy of the GNU General Public License */ /* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */ /* */
/**********************************************************************************/ /**********************************************************************************/
namespace Thelia\Model; namespace Thelia\Model;
use Propel\Runtime\Propel; use Propel\Runtime\Propel;
use Thelia\Model\Base\Coupon as BaseCoupon; use Thelia\Model\Base\Coupon as BaseCoupon;
use Thelia\Model\Exception\InvalidArgumentException; use Thelia\Model\Exception\InvalidArgumentException;
use Thelia\Model\Map\CouponTableMap; use Thelia\Model\Map\CouponTableMap;
/** /**
* Used to provide an effect (mostly a discount) * Used to provide an effect (mostly a discount)
* at the end of the Customer checkout tunnel * at the end of the Customer checkout tunnel
* It will be usable for a Customer only if it matches the Coupon criteria (Rules) * It will be usable for a Customer only if it matches the Coupon criteria (Rules)
* *
* @package Coupon * @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr> * @author Guillaume MOREL <gmorel@openstudio.fr>
* *
*/ */
class Coupon extends BaseCoupon class Coupon extends BaseCoupon
{ {
use \Thelia\Model\Tools\ModelEventDispatcherTrait; use \Thelia\Model\Tools\ModelEventDispatcherTrait;
/** /**
* Create or Update this Coupon * Create or Update this Coupon
* *
* @param string $code Coupon Code * @param string $code Coupon Code
* @param string $title Coupon title * @param string $title Coupon title
* @param array $effects Ready to be serialized in JSON effect params * @param array $effects Ready to be serialized in JSON effect params
* @param string $type Coupon type * @param string $type Coupon type
* @param bool $isRemovingPostage Is removing Postage * @param bool $isRemovingPostage Is removing Postage
* @param string $shortDescription Coupon short description * @param string $shortDescription Coupon short description
* @param string $description Coupon description * @param string $description Coupon description
* @param boolean $isEnabled Enable/Disable * @param boolean $isEnabled Enable/Disable
* @param \DateTime $expirationDate Coupon expiration date * @param \DateTime $expirationDate Coupon expiration date
* @param boolean $isAvailableOnSpecialOffers Is available on special offers * @param boolean $isAvailableOnSpecialOffers Is available on special offers
* @param boolean $isCumulative Is cumulative * @param boolean $isCumulative Is cumulative
* @param int $maxUsage Coupon quantity * @param int $maxUsage Coupon quantity
* @param string $defaultSerializedRule Serialized default rule added if none found * @param string $defaultSerializedRule Serialized default rule added if none found
* @param string $locale Coupon Language code ISO (ex: fr_FR) * @param string $locale Coupon Language code ISO (ex: fr_FR)
* *
* @throws \Exception * @throws \Exception
*/ */
public function createOrUpdate($code, $title, array $effects, $type, $isRemovingPostage, $shortDescription, $description, $isEnabled, $expirationDate, $isAvailableOnSpecialOffers, $isCumulative, $maxUsage, $defaultSerializedRule, $locale = null) public function createOrUpdate($code, $title, array $effects, $type, $isRemovingPostage, $shortDescription, $description, $isEnabled, $expirationDate, $isAvailableOnSpecialOffers, $isCumulative, $maxUsage, $defaultSerializedRule, $locale = null)
{ {
$this->setCode($code) $this
->setType($type) ->setCode($code)
->setEffects($effects) ->setType($type)
->setIsRemovingPostage($isRemovingPostage) ->setEffects($effects)
->setIsEnabled($isEnabled) ->setIsRemovingPostage($isRemovingPostage)
->setExpirationDate($expirationDate) ->setIsEnabled($isEnabled)
->setIsAvailableOnSpecialOffers($isAvailableOnSpecialOffers) ->setExpirationDate($expirationDate)
->setIsCumulative($isCumulative) ->setIsAvailableOnSpecialOffers($isAvailableOnSpecialOffers)
->setMaxUsage($maxUsage); ->setIsCumulative($isCumulative)
$this->setTitle($title) ->setMaxUsage($maxUsage)
->setShortDescription($shortDescription) ->setLocale($locale)
->setDescription($description); ->setTitle($title)
->setShortDescription($shortDescription)
// If no rule given, set default rule ->setDescription($description);
if (null === $this->getSerializedConditions()) {
$this->setSerializedConditions($defaultSerializedRule); // If no rule given, set default rule
} if (null === $this->getSerializedConditions()) {
$this->setSerializedConditions($defaultSerializedRule);
// Set object language (i18n) }
if (!is_null($locale)) {
$this->setLocale($locale); $this->save();
} }
$con = Propel::getWriteConnection(CouponTableMap::DATABASE_NAME); /**
$con->beginTransaction(); * Create or Update this coupon condition
try { *
$this->save($con); * @param string $serializableConditions Serialized conditions ready to be saved
$con->commit(); * @param string $locale Coupon Language code ISO (ex: fr_FR)
*
} catch (\Exception $e) { * @throws \Exception
$con->rollback(); */
throw $e; public function createOrUpdateConditions($serializableConditions, $locale)
} {
} $this->setSerializedConditions($serializableConditions);
/** // Set object language (i18n)
* Create or Update this coupon condition if (!is_null($locale)) {
* $this->setLocale($locale);
* @param string $serializableConditions Serialized conditions ready to be saved }
* @param string $locale Coupon Language code ISO (ex: fr_FR)
* $this->save();
* @throws \Exception }
*/
public function createOrUpdateConditions($serializableConditions, $locale) /**
{ * Set Coupon amount
$this->setSerializedConditions($serializableConditions); *
* @param float $amount Amount deduced from the Cart
// Set object language (i18n) *
if (!is_null($locale)) { * @return $this
$this->setLocale($locale); */
} public function setAmount($amount)
{
$con = Propel::getWriteConnection(CouponTableMap::DATABASE_NAME); $effects = $this->unserializeEffects($this->getSerializedEffects());
$con->beginTransaction(); $effects['amount'] = floatval($amount);
try { $this->setEffects($effects);
$this->save($con);
$con->commit(); return $this;
} catch (\Exception $e) { }
$con->rollback();
throw $e; /**
} * Get the amount removed from the coupon to the cart
} *
* @return float
/** */
* Set Coupon amount public function getAmount()
* {
* @param float $amount Amount deduced from the Cart $amount = $this->getEffects()['amount'];
*
* @return $this return floatval($amount);
*/ }
public function setAmount($amount)
{ /**
$effects = $this->unserializeEffects($this->getSerializedEffects()); * Get the Coupon effects
$effects['amount'] = floatval($amount); *
$this->setEffects($effects); * @return array
* @throws Exception\InvalidArgumentException
return $this; */
} public function getEffects()
{
/** $effects = $this->unserializeEffects($this->getSerializedEffects());
* Get the amount removed from the coupon to the cart
* if (null === $effects['amount']) {
* @return float throw new InvalidArgumentException('Missing key \'amount\' in Coupon effect coming from database');
*/ }
public function getAmount()
{ return $effects;
$amount = $this->getEffects()['amount']; }
return floatval($amount); /**
} * Get the Coupon effects
*
/** * @param array $effects Effect ready to be serialized
* Get the Coupon effects * Needs at least the key 'amount'
* * with the amount removed from the cart
* @return array *
* @throws Exception\InvalidArgumentException * @throws Exception\InvalidArgumentException
*/ * @return $this
public function getEffects() */
{ public function setEffects(array $effects)
$effects = $this->unserializeEffects($this->getSerializedEffects()); {
if (null === $effects['amount']) {
if (null === $effects['amount']) { throw new InvalidArgumentException('Missing key \'amount\' in Coupon effect ready to be serialized array');
throw new InvalidArgumentException('Missing key \'amount\' in Coupon effect coming from database'); }
}
$this->setSerializedEffects($this->serializeEffects($effects));
return $effects;
} return $this;
}
/**
* Get the Coupon effects /**
* * Return unserialized effects
* @param array $effects Effect ready to be serialized *
* Needs at least the key 'amount' * @param string $serializedEffects Serialized effect string to unserialize
* with the amount removed from the cart *
* * @return array
* @throws Exception\InvalidArgumentException */
* @return $this public function unserializeEffects($serializedEffects)
*/ {
public function setEffects(array $effects) $effects = json_decode($serializedEffects, true);
{
if (null === $effects['amount']) { return $effects;
throw new InvalidArgumentException('Missing key \'amount\' in Coupon effect ready to be serialized array'); }
}
/**
$this->setSerializedEffects($this->serializeEffects($effects)); * Return serialized effects
*
return $this; * @param array $unserializedEffects Unserialized array string to serialize
} *
* @return string
/** */
* Return unserialized effects public function serializeEffects(array $unserializedEffects)
* {
* @param string $serializedEffects Serialized effect string to unserialize $effects = json_encode($unserializedEffects);
*
* @return array return $effects;
*/ }
public function unserializeEffects($serializedEffects) }
{
$effects = json_decode($serializedEffects, true);
return $effects;
}
/**
* Return serialized effects
*
* @param array $unserializedEffects Unserialized array string to serialize
*
* @return string
*/
public function serializeEffects(array $unserializedEffects)
{
$effects = json_encode($unserializedEffects);
return $effects;
}
}