diff --git a/Observer/CheckoutLoadBefore.php b/Observer/CheckoutLoadBefore.php index 36a8151..9ef2b96 100644 --- a/Observer/CheckoutLoadBefore.php +++ b/Observer/CheckoutLoadBefore.php @@ -6,9 +6,8 @@ namespace Aimes\CheckoutDesigns\Observer; -use Aimes\CheckoutDesigns\Api\CheckoutDesignInterface; use Aimes\CheckoutDesigns\Scope\Config; -use Magento\Customer\Model\Session as CustomerSession; +use Aimes\CheckoutDesigns\Scope\Customer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Exception\LocalizedException; @@ -19,19 +18,19 @@ class CheckoutLoadBefore implements ObserverInterface /** @var Config */ private $config; - /** @var CustomerSession */ - private $customerSession; + /** @var Customer */ + private $customer; /** * @param Config $config - * @param CustomerSession $customerSession + * @param Customer $customer */ public function __construct( Config $config, - CustomerSession $customerSession + Customer $customer ) { $this->config = $config; - $this->customerSession = $customerSession; + $this->customer = $customer; } /** @@ -45,7 +44,7 @@ public function execute(Observer $observer): void $route = $observer->getEvent()->getFullActionName(); if ($route === 'checkout_index_index') { - if ($design = $this->getDesign()) { + if ($design = $this->customer->getActiveDesign()) { $observer->getEvent() ->getLayout() ->getUpdate() @@ -53,24 +52,4 @@ public function execute(Observer $observer): void } } } - - /** - * @return CheckoutDesignInterface|null - * @throws LocalizedException - * @throws NoSuchEntityException - */ - private function getDesign(): ?CheckoutDesignInterface - { - $groupMapping = $this->config->getCustomerGroupMapping(); - - if ($groupMapping !== null) { - $currentCustomerGroup = $this->customerSession->getCustomerGroupId(); - - if (isset($groupMapping[$currentCustomerGroup])) { - return $this->config->getDesign($groupMapping[$currentCustomerGroup]); - } - } - - return $this->config->getDesign(); - } } diff --git a/Scope/Customer.php b/Scope/Customer.php new file mode 100644 index 0000000..491b6ee --- /dev/null +++ b/Scope/Customer.php @@ -0,0 +1,52 @@ +config = $config; + $this->customerSession = $customerSession; + } + + /** + * @return CheckoutDesignInterface|null + * @throws LocalizedException + * @throws NoSuchEntityException + */ + public function getActiveDesign(): ?CheckoutDesignInterface + { + $groupMapping = $this->config->getCustomerGroupMapping(); + + if ($groupMapping !== null) { + $currentCustomerGroup = $this->customerSession->getCustomerGroupId(); + + if (isset($groupMapping[$currentCustomerGroup])) { + return $this->config->getDesign($groupMapping[$currentCustomerGroup]); + } + } + + return $this->config->getDesign(); + } +}