From 683664cb0734e9f69cff723bc7542c76a9edaac5 Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Thu, 15 May 2014 16:50:34 +0200 Subject: [PATCH] Added Model\Module::getModuleInstance() method --- core/lib/Thelia/Action/Module.php | 2 +- core/lib/Thelia/Core/Template/Loop/Delivery.php | 2 +- core/lib/Thelia/Core/Template/Loop/Payment.php | 2 +- .../Template/Smarty/Plugins/CartPostage.php | 2 +- core/lib/Thelia/Model/Module.php | 17 +++++++++++++++++ .../modules/Front/Controller/CartController.php | 2 +- .../Front/Controller/CouponController.php | 4 +++- .../Front/Controller/OrderController.php | 3 ++- 8 files changed, 27 insertions(+), 7 deletions(-) diff --git a/core/lib/Thelia/Action/Module.php b/core/lib/Thelia/Action/Module.php index 7cabcc2fa..0cb1853df 100644 --- a/core/lib/Thelia/Action/Module.php +++ b/core/lib/Thelia/Action/Module.php @@ -161,7 +161,7 @@ class Module extends BaseAction implements EventSubscriberInterface ); } - $paymentModuleInstance = $this->container->get(sprintf('module.%s', $paymentModule->getCode())); + $paymentModuleInstance = $paymentModule->getModuleInstance($this->container); $response = $paymentModuleInstance->pay($order); diff --git a/core/lib/Thelia/Core/Template/Loop/Delivery.php b/core/lib/Thelia/Core/Template/Loop/Delivery.php index 6bdcf1a6e..5f5316545 100644 --- a/core/lib/Thelia/Core/Template/Loop/Delivery.php +++ b/core/lib/Thelia/Core/Template/Loop/Delivery.php @@ -64,7 +64,7 @@ class Delivery extends BaseSpecificModule $loopResultRow = new LoopResultRow($deliveryModule); /** @var DeliveryModuleInterface $moduleInstance */ - $moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode())); + $moduleInstance = $deliveryModule->getModuleInstance($this->container); if (false === $moduleInstance instanceof DeliveryModuleInterface) { throw new \RuntimeException(sprintf("delivery module %s is not a Thelia\Module\DeliveryModuleInterface", $deliveryModule->getCode())); diff --git a/core/lib/Thelia/Core/Template/Loop/Payment.php b/core/lib/Thelia/Core/Template/Loop/Payment.php index af43b9ff0..05ea09537 100644 --- a/core/lib/Thelia/Core/Template/Loop/Payment.php +++ b/core/lib/Thelia/Core/Template/Loop/Payment.php @@ -37,7 +37,7 @@ class Payment extends BaseSpecificModule implements PropelSearchLoopInterface foreach ($loopResult->getResultDataCollection() as $paymentModule) { $loopResultRow = new LoopResultRow($paymentModule); - $moduleInstance = $this->container->get(sprintf('module.%s', $paymentModule->getCode())); + $moduleInstance = $paymentModule->getModuleInstance($this->container); if (false === $moduleInstance instanceof PaymentModuleInterface) { throw new \RuntimeException(sprintf("payment module %s is not a Thelia\Module\PaymentModuleInterface", $paymentModule->getCode())); diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/CartPostage.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/CartPostage.php index 9bf98dd43..50f65673d 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/CartPostage.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/CartPostage.php @@ -169,7 +169,7 @@ class CartPostage extends AbstractSmartyPlugin foreach ($deliveryModules as $deliveryModule) { /** @var DeliveryModuleInterface $moduleInstance */ - $moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode())); + $moduleInstance = $deliveryModule->getModuleInstance($this->container); if (false === $moduleInstance instanceof DeliveryModuleInterface) { throw new \RuntimeException(sprintf("delivery module %s is not a Thelia\Module\DeliveryModuleInterface", $deliveryModule->getCode())); diff --git a/core/lib/Thelia/Model/Module.php b/core/lib/Thelia/Model/Module.php index c5b7257c3..7edd4ad51 100644 --- a/core/lib/Thelia/Model/Module.php +++ b/core/lib/Thelia/Model/Module.php @@ -3,10 +3,12 @@ namespace Thelia\Model; use Propel\Runtime\Connection\ConnectionInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; use Thelia\Core\Template\TemplateDefinition; use Thelia\Model\Base\Module as BaseModule; use Thelia\Model\Tools\ModelEventDispatcherTrait; use Thelia\Model\Tools\PositionManagementTrait; +use Thelia\Module\BaseModuleInterface; class Module extends BaseModule { @@ -182,6 +184,21 @@ class Module extends BaseModule return $moduleReflection->implementsInterface("Thelia\Module\PaymentModuleInterface"); } + /** + * @param ContainerInterface $container the Thelia container + * @return BaseModuleInterface a module instance + * @throws \InvalidArgumentException if the module could not be found in the container/ + */ + public function getModuleInstance(ContainerInterface $container) { + + $instance = $this->container->get(sprintf('module.%s', $this->getCode())); + + if ($instance == null) { + throw new \InvalidArgumentException(sprintf('Undefined module in container: "%s"', $this->getCode())); + } + + return $instance; + } /** * @return BaseModule a new module instance. */ diff --git a/local/modules/Front/Controller/CartController.php b/local/modules/Front/Controller/CartController.php index 587866950..431124dbe 100644 --- a/local/modules/Front/Controller/CartController.php +++ b/local/modules/Front/Controller/CartController.php @@ -177,7 +177,7 @@ class CartController extends BaseFrontController $deliveryAddress = AddressQuery::create()->findPk($order->chosenDeliveryAddress); if (null !== $deliveryModule && null !== $deliveryAddress) { - $moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode())); + $moduleInstance = $deliveryModule->getModuleInstance($this->container); $orderEvent = new OrderEvent($order); diff --git a/local/modules/Front/Controller/CouponController.php b/local/modules/Front/Controller/CouponController.php index 3c3c025ab..03c57edba 100644 --- a/local/modules/Front/Controller/CouponController.php +++ b/local/modules/Front/Controller/CouponController.php @@ -69,12 +69,14 @@ class CouponController extends BaseFrontController /* recalculate postage amount */ $order = $this->getSession()->getOrder(); + if (null !== $order) { $deliveryModule = $order->getModuleRelatedByDeliveryModuleId(); $deliveryAddress = AddressQuery::create()->findPk($order->chosenDeliveryAddress); if (null !== $deliveryModule && null !== $deliveryAddress) { - $moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode())); + + $moduleInstance = $deliveryModule->getModuleInstance($this->container); $orderEvent = new OrderEvent($order); diff --git a/local/modules/Front/Controller/OrderController.php b/local/modules/Front/Controller/OrderController.php index 2c6f3072e..c593b958a 100644 --- a/local/modules/Front/Controller/OrderController.php +++ b/local/modules/Front/Controller/OrderController.php @@ -88,7 +88,8 @@ class OrderController extends BaseFrontController } /* get postage amount */ - $moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode())); + $moduleInstance = $deliveryModule->getModuleInstance($this->container); + $postage = $moduleInstance->getPostage($deliveryAddress->getCountry()); $orderEvent = $this->getOrderEvent();