Added the AbstractDeliveryModule class, and the isValidDelivery method.

This commit is contained in:
Franck Allimant
2014-04-17 18:57:04 +02:00
parent 834045366e
commit e0079096dd
3 changed files with 178 additions and 109 deletions

View File

@@ -1,106 +1,111 @@
<?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\Core\Template\Loop; namespace Thelia\Core\Template\Loop;
use Thelia\Core\Template\Element\LoopResult; use Thelia\Core\Template\Element\LoopResult;
use Thelia\Core\Template\Element\LoopResultRow; use Thelia\Core\Template\Element\LoopResultRow;
use Thelia\Core\Template\Loop\Argument\Argument; use Thelia\Core\Template\Loop\Argument\Argument;
use Thelia\Exception\OrderException; use Thelia\Exception\OrderException;
use Thelia\Model\CountryQuery; use Thelia\Model\CountryQuery;
use Thelia\Module\BaseModule; use Thelia\Model\Module;
use Thelia\Module\DeliveryModuleInterface; use Thelia\Module\BaseModule;
use Thelia\Module\DeliveryModuleInterface;
/**
* Class Delivery /**
* @package Thelia\Core\Template\Loop * Class Delivery
* @author Manuel Raynaud <mraynaud@openstudio.fr> * @package Thelia\Core\Template\Loop
* @author Etienne Roudeix <eroudeix@gmail.com> * @author Manuel Raynaud <mraynaud@openstudio.fr>
*/ * @author Etienne Roudeix <eroudeix@gmail.com>
class Delivery extends BaseSpecificModule */
{ class Delivery extends BaseSpecificModule
{
public function getArgDefinitions()
{ public function getArgDefinitions()
$collection = parent::getArgDefinitions(); {
$collection = parent::getArgDefinitions();
$collection->addArgument(
Argument::createIntTypeArgument("country") $collection->addArgument(
); Argument::createIntTypeArgument("country")
);
return $collection;
} return $collection;
}
public function parseResults(LoopResult $loopResult)
{ public function parseResults(LoopResult $loopResult)
$countryId = $this->getCountry(); {
if (null !== $countryId) { $countryId = $this->getCountry();
$country = CountryQuery::create()->findPk($countryId); if (null !== $countryId) {
if (null === $country) { $country = CountryQuery::create()->findPk($countryId);
throw new \InvalidArgumentException('Cannot found country id: `' . $countryId . '` in delivery loop'); if (null === $country) {
} throw new \InvalidArgumentException('Cannot found country id: `' . $countryId . '` in delivery loop');
} else { }
$country = $this->container->get('thelia.taxEngine')->getDeliveryCountry(); } else {
} $country = $this->container->get('thelia.taxEngine')->getDeliveryCountry();
}
foreach ($loopResult->getResultDataCollection() as $deliveryModule) {
$loopResultRow = new LoopResultRow($deliveryModule); /** @var Module $deliveryModule */
foreach ($loopResult->getResultDataCollection() as $deliveryModule) {
$moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode())); $loopResultRow = new LoopResultRow($deliveryModule);
if (false === $moduleInstance instanceof DeliveryModuleInterface) { /** @var DeliveryModuleInterface $moduleInstance */
throw new \RuntimeException(sprintf("delivery module %s is not a Thelia\Module\DeliveryModuleInterface", $deliveryModule->getCode())); $moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode()));
}
if (false === $moduleInstance instanceof DeliveryModuleInterface) {
try { throw new \RuntimeException(sprintf("delivery module %s is not a Thelia\Module\DeliveryModuleInterface", $deliveryModule->getCode()));
$postage = $moduleInstance->getPostage($country); }
} catch (OrderException $e) {
switch ($e->getCode()) { try {
case OrderException::DELIVERY_MODULE_UNAVAILABLE: // Check if module is valid, by calling isValidDelivery(),
/* do not show this delivery module */ // or catching a DELIVERY_MODULE_UNAVAILABLE OrderException.
continue(2);
break; if ($moduleInstance->isValidDelivery($country)) {
default:
throw $e; $postage = $moduleInstance->getPostage($country);
}
} $loopResultRow
->set('ID', $deliveryModule->getId())
$loopResultRow ->set('TITLE', $deliveryModule->getVirtualColumn('i18n_TITLE'))
->set('ID', $deliveryModule->getId()) ->set('CHAPO', $deliveryModule->getVirtualColumn('i18n_CHAPO'))
->set('TITLE', $deliveryModule->getVirtualColumn('i18n_TITLE')) ->set('DESCRIPTION', $deliveryModule->getVirtualColumn('i18n_DESCRIPTION'))
->set('CHAPO', $deliveryModule->getVirtualColumn('i18n_CHAPO')) ->set('POSTSCRIPTUM', $deliveryModule->getVirtualColumn('i18n_POSTSCRIPTUM'))
->set('DESCRIPTION', $deliveryModule->getVirtualColumn('i18n_DESCRIPTION')) ->set('POSTAGE', $postage)
->set('POSTSCRIPTUM', $deliveryModule->getVirtualColumn('i18n_POSTSCRIPTUM')) ;
->set('POSTAGE', $postage)
; $loopResult->addRow($loopResultRow);
}
$loopResult->addRow($loopResultRow); } catch (OrderException $ex) {
} // Re-throw an unknown exception
if ($ex->getCode() !== OrderException::DELIVERY_MODULE_UNAVAILABLE) {
return $loopResult; throw $ex;
} }
}
protected function getModuleType() }
{
return BaseModule::DELIVERY_MODULE_TYPE; return $loopResult;
} }
}
protected function getModuleType()
{
return BaseModule::DELIVERY_MODULE_TYPE;
}
}

View File

@@ -0,0 +1,51 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* 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 */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Module;
use Thelia\Model\Country;
abstract class AbstractDeliveryModule extends BaseModule implements DeliveryModuleInterface
{
/**
* This method is called by the Delivery loop, to check if the current module has to be displayed to the customer.
* Override it to implements your delivery rules/
*
* If you return true, the delivery method will de displayed to the customer
* If you return false, the delivery method will not be displayed
*
* @param Country $country the country to deliver to.
*
* @return boolean
*/
public abstract function isValidDelivery(Country $country);
/**
* Calculate and return delivery price in the shop's default currency
*
* @param Country $country the country to deliver to.
*
* @return float the delivery price
*/
public abstract function getPostage(Country $country);
}

View File

@@ -28,11 +28,24 @@ use Thelia\Model\Country;
interface DeliveryModuleInterface extends BaseModuleInterface interface DeliveryModuleInterface extends BaseModuleInterface
{ {
/** /**
* calculate and return delivery price * This method is called by the Delivery loop, to check if the current module has to be displayed to the customer.
* Override it to implements your delivery rules/
* *
* @param Country $country * If you return true, the delivery method will de displayed to the customer
* If you return false, the delivery method will not be displayed
* *
* @return mixed * @param Country $country the country to deliver to.
*
* @return boolean
*/
public function isValidDelivery(Country $country);
/**
* Calculate and return delivery price in the shop's default currency
*
* @param Country $country the country to deliver to.
*
* @return float the delivery price
*/ */
public function getPostage(Country $country); public function getPostage(Country $country);
} }