From e0079096dd0659193681eb55419df14cc4c45d8a Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Thu, 17 Apr 2014 18:57:04 +0200 Subject: [PATCH] Added the AbstractDeliveryModule class, and the isValidDelivery method. --- .../Thelia/Core/Template/Loop/Delivery.php | 217 +++++++++--------- .../Thelia/Module/AbstractDeliveryModule.php | 51 ++++ .../Thelia/Module/DeliveryModuleInterface.php | 19 +- 3 files changed, 178 insertions(+), 109 deletions(-) create mode 100644 core/lib/Thelia/Module/AbstractDeliveryModule.php diff --git a/core/lib/Thelia/Core/Template/Loop/Delivery.php b/core/lib/Thelia/Core/Template/Loop/Delivery.php index 2e7391c95..22ac946e7 100644 --- a/core/lib/Thelia/Core/Template/Loop/Delivery.php +++ b/core/lib/Thelia/Core/Template/Loop/Delivery.php @@ -1,106 +1,111 @@ -. */ -/* */ -/*************************************************************************************/ - -namespace Thelia\Core\Template\Loop; -use Thelia\Core\Template\Element\LoopResult; -use Thelia\Core\Template\Element\LoopResultRow; -use Thelia\Core\Template\Loop\Argument\Argument; -use Thelia\Exception\OrderException; -use Thelia\Model\CountryQuery; -use Thelia\Module\BaseModule; -use Thelia\Module\DeliveryModuleInterface; - -/** - * Class Delivery - * @package Thelia\Core\Template\Loop - * @author Manuel Raynaud - * @author Etienne Roudeix - */ -class Delivery extends BaseSpecificModule -{ - - public function getArgDefinitions() - { - $collection = parent::getArgDefinitions(); - - $collection->addArgument( - Argument::createIntTypeArgument("country") - ); - - return $collection; - } - - public function parseResults(LoopResult $loopResult) - { - $countryId = $this->getCountry(); - if (null !== $countryId) { - $country = CountryQuery::create()->findPk($countryId); - if (null === $country) { - throw new \InvalidArgumentException('Cannot found country id: `' . $countryId . '` in delivery loop'); - } - } else { - $country = $this->container->get('thelia.taxEngine')->getDeliveryCountry(); - } - - foreach ($loopResult->getResultDataCollection() as $deliveryModule) { - $loopResultRow = new LoopResultRow($deliveryModule); - - $moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode())); - - if (false === $moduleInstance instanceof DeliveryModuleInterface) { - throw new \RuntimeException(sprintf("delivery module %s is not a Thelia\Module\DeliveryModuleInterface", $deliveryModule->getCode())); - } - - try { - $postage = $moduleInstance->getPostage($country); - } catch (OrderException $e) { - switch ($e->getCode()) { - case OrderException::DELIVERY_MODULE_UNAVAILABLE: - /* do not show this delivery module */ - continue(2); - break; - default: - throw $e; - } - } - - $loopResultRow - ->set('ID', $deliveryModule->getId()) - ->set('TITLE', $deliveryModule->getVirtualColumn('i18n_TITLE')) - ->set('CHAPO', $deliveryModule->getVirtualColumn('i18n_CHAPO')) - ->set('DESCRIPTION', $deliveryModule->getVirtualColumn('i18n_DESCRIPTION')) - ->set('POSTSCRIPTUM', $deliveryModule->getVirtualColumn('i18n_POSTSCRIPTUM')) - ->set('POSTAGE', $postage) - ; - - $loopResult->addRow($loopResultRow); - } - - return $loopResult; - } - - protected function getModuleType() - { - return BaseModule::DELIVERY_MODULE_TYPE; - } -} +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Template\Loop; +use Thelia\Core\Template\Element\LoopResult; +use Thelia\Core\Template\Element\LoopResultRow; +use Thelia\Core\Template\Loop\Argument\Argument; +use Thelia\Exception\OrderException; +use Thelia\Model\CountryQuery; +use Thelia\Model\Module; +use Thelia\Module\BaseModule; +use Thelia\Module\DeliveryModuleInterface; + +/** + * Class Delivery + * @package Thelia\Core\Template\Loop + * @author Manuel Raynaud + * @author Etienne Roudeix + */ +class Delivery extends BaseSpecificModule +{ + + public function getArgDefinitions() + { + $collection = parent::getArgDefinitions(); + + $collection->addArgument( + Argument::createIntTypeArgument("country") + ); + + return $collection; + } + + public function parseResults(LoopResult $loopResult) + { + $countryId = $this->getCountry(); + if (null !== $countryId) { + $country = CountryQuery::create()->findPk($countryId); + if (null === $country) { + throw new \InvalidArgumentException('Cannot found country id: `' . $countryId . '` in delivery loop'); + } + } else { + $country = $this->container->get('thelia.taxEngine')->getDeliveryCountry(); + } + + /** @var Module $deliveryModule */ + foreach ($loopResult->getResultDataCollection() as $deliveryModule) { + $loopResultRow = new LoopResultRow($deliveryModule); + + /** @var DeliveryModuleInterface $moduleInstance */ + $moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode())); + + if (false === $moduleInstance instanceof DeliveryModuleInterface) { + throw new \RuntimeException(sprintf("delivery module %s is not a Thelia\Module\DeliveryModuleInterface", $deliveryModule->getCode())); + } + + try { + // Check if module is valid, by calling isValidDelivery(), + // or catching a DELIVERY_MODULE_UNAVAILABLE OrderException. + + if ($moduleInstance->isValidDelivery($country)) { + + $postage = $moduleInstance->getPostage($country); + + $loopResultRow + ->set('ID', $deliveryModule->getId()) + ->set('TITLE', $deliveryModule->getVirtualColumn('i18n_TITLE')) + ->set('CHAPO', $deliveryModule->getVirtualColumn('i18n_CHAPO')) + ->set('DESCRIPTION', $deliveryModule->getVirtualColumn('i18n_DESCRIPTION')) + ->set('POSTSCRIPTUM', $deliveryModule->getVirtualColumn('i18n_POSTSCRIPTUM')) + ->set('POSTAGE', $postage) + ; + + $loopResult->addRow($loopResultRow); + } + } catch (OrderException $ex) { + // Re-throw an unknown exception + if ($ex->getCode() !== OrderException::DELIVERY_MODULE_UNAVAILABLE) { + throw $ex; + } + } + } + + return $loopResult; + } + + protected function getModuleType() + { + return BaseModule::DELIVERY_MODULE_TYPE; + } +} diff --git a/core/lib/Thelia/Module/AbstractDeliveryModule.php b/core/lib/Thelia/Module/AbstractDeliveryModule.php new file mode 100644 index 000000000..80c0072f2 --- /dev/null +++ b/core/lib/Thelia/Module/AbstractDeliveryModule.php @@ -0,0 +1,51 @@ +. */ +/* */ +/*************************************************************************************/ + +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); +} \ No newline at end of file diff --git a/core/lib/Thelia/Module/DeliveryModuleInterface.php b/core/lib/Thelia/Module/DeliveryModuleInterface.php index 17b000d4f..2cabd5f87 100644 --- a/core/lib/Thelia/Module/DeliveryModuleInterface.php +++ b/core/lib/Thelia/Module/DeliveryModuleInterface.php @@ -28,11 +28,24 @@ use Thelia\Model\Country; 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); }