diff --git a/Observer/Validate.php b/Observer/Validate.php index 1b56cdc..5ad0ca3 100644 --- a/Observer/Validate.php +++ b/Observer/Validate.php @@ -14,6 +14,7 @@ use Magento\Framework\App\ActionInterface; use Magento\Framework\App\Request\Http as Request; use Magento\Framework\App\Response\Http as Response; +use Magento\Framework\App\Response\RedirectInterface; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Message\ManagerInterface; @@ -29,6 +30,8 @@ abstract class Validate implements ObserverInterface protected Response $response; + protected RedirectInterface $redirect; + protected Validator $validator; protected Json $json; @@ -45,7 +48,6 @@ abstract class Validate implements ObserverInterface /** * @param ManagerInterface $messageManager - * @param Response $response * @param Validator $validator * @param Json $json * @param Config $config @@ -54,7 +56,7 @@ abstract class Validate implements ObserverInterface */ public function __construct( ManagerInterface $messageManager, - Response $response, + RedirectInterface $redirect, Validator $validator, Json $json, Config $config, @@ -62,7 +64,7 @@ public function __construct( array $data = [] ) { $this->messageManager = $messageManager; - $this->response = $response; + $this->redirect = $redirect; $this->validator = $validator; $this->json = $json; $this->config = $config; @@ -78,8 +80,9 @@ public function __construct( */ public function execute(Observer $observer): void { - $this->action = $observer->getEvent()->getData('controller_action'); - $this->request = $observer->getEvent()->getData('request'); + $this->action = $observer->getEvent()->getData('controller_action'); + $this->request = $observer->getEvent()->getData('request'); + $this->response = $this->action->getResponse(); if ($this->canValidate()) { try { @@ -114,9 +117,8 @@ public function getCfResponse(): ?string protected function error(Phrase $message): void { $this->messageManager->addErrorMessage($message); - $this->response->setRedirect($this->request?->getServer('HTTP_REFERER', '/') ?? '/'); + $this->redirect->redirect($this->response, $this->redirect->getRefererUrl()); - $this->response->sendResponse(); exit(); } diff --git a/Observer/Validate/Frontend.php b/Observer/Validate/Frontend.php index 86f3e84..667c6a0 100644 --- a/Observer/Validate/Frontend.php +++ b/Observer/Validate/Frontend.php @@ -12,7 +12,7 @@ use Magento\Customer\Controller\Ajax\Login as AjaxLoginPost; use Magento\Customer\Model\Session as CustomerSession; -use Magento\Framework\App\Response\Http as Response; +use Magento\Framework\App\Response\RedirectInterface; use Magento\Framework\Message\ManagerInterface; use Magento\Framework\Phrase; use Magento\Framework\Serialize\Serializer\Json; @@ -27,7 +27,6 @@ class Frontend extends Validate /** * @param ManagerInterface $messageManager - * @param Response $response * @param Validator $validator * @param Json $json * @param Config $config @@ -37,8 +36,8 @@ class Frontend extends Validate */ public function __construct( ManagerInterface $messageManager, - Response $response, Validator $validator, + RedirectInterface $redirect, Json $json, Config $config, CustomerSession $customerSession, @@ -47,7 +46,7 @@ public function __construct( ) { $this->customerSession = $customerSession; - parent::__construct($messageManager, $response, $validator, $json, $config, $persistor, $data); + parent::__construct($messageManager, $redirect, $validator, $json, $config, $persistor, $data); }