From 9b503d66a0bed906f6f49bd2c46753d19cfa2a3e Mon Sep 17 00:00:00 2001 From: Freek Gruntjes Date: Fri, 6 Oct 2017 16:27:06 +0200 Subject: [PATCH] Implemented Tweakwise redirects --- .../Catalog/Layer/FilterList/Tweakwise.php | 1 - ...stractProductNavigationRequestObserver.php | 107 ++++++++++++++++++ .../Observer/CatalogLastPageRedirect.php | 88 +------------- src/Model/Observer/CatalogSearchRedirect.php | 34 ++++++ src/etc/frontend/events.xml | 3 +- 5 files changed, 148 insertions(+), 85 deletions(-) create mode 100644 src/Model/Observer/AbstractProductNavigationRequestObserver.php create mode 100644 src/Model/Observer/CatalogSearchRedirect.php diff --git a/src/Model/Catalog/Layer/FilterList/Tweakwise.php b/src/Model/Catalog/Layer/FilterList/Tweakwise.php index 2606a2ea..18b5235c 100644 --- a/src/Model/Catalog/Layer/FilterList/Tweakwise.php +++ b/src/Model/Catalog/Layer/FilterList/Tweakwise.php @@ -8,7 +8,6 @@ namespace Emico\Tweakwise\Model\Catalog\Layer\FilterList; -use Emico\Tweakwise\Exception\RuntimeException; use Emico\Tweakwise\Model\Catalog\Layer\FilterFactory; use Emico\Tweakwise\Model\Catalog\Layer\NavigationContext\CurrentContext; use Magento\Catalog\Model\Layer; diff --git a/src/Model/Observer/AbstractProductNavigationRequestObserver.php b/src/Model/Observer/AbstractProductNavigationRequestObserver.php new file mode 100644 index 00000000..7554c79f --- /dev/null +++ b/src/Model/Observer/AbstractProductNavigationRequestObserver.php @@ -0,0 +1,107 @@ +config = $config; + $this->context = $context; + $this->urlBuilder = $urlBuilder; + $this->controller = $action; + } + + /** + * @return Response|null + */ + protected function getHttpResponse() + { + $response = $this->controller->getResponse(); + if (!$response instanceof Response) { + return null; + } + + return $response; + } + + /** + * @return bool + */ + protected function hasTweakwiseResponse() + { + if (!$this->config->isLayeredEnabled()) { + return false; + } + + if (!$this->context->getContext()->hasResponse()) { + return false; + } + + return true; + } + + /** + * {@inheritdoc} + */ + public function execute(Observer $observer) + { + $response = $this->getHttpResponse(); + if (!$response) { + return; + } + + if (!$this->hasTweakwiseResponse()) { + return; + } + + $this->_execute($observer); + } + + /** + * @param Observer $observer + */ + abstract protected function _execute(Observer $observer); +} diff --git a/src/Model/Observer/CatalogLastPageRedirect.php b/src/Model/Observer/CatalogLastPageRedirect.php index 91ce4162..e3112a79 100644 --- a/src/Model/Observer/CatalogLastPageRedirect.php +++ b/src/Model/Observer/CatalogLastPageRedirect.php @@ -8,98 +8,20 @@ namespace Emico\Tweakwise\Model\Observer; -use Emico\Tweakwise\Model\Catalog\Layer\NavigationContext\CurrentContext; -use Emico\Tweakwise\Model\Config; -use Magento\Framework\App\Action\Action; use Magento\Framework\Event\Observer; -use Magento\Framework\Event\ObserverInterface; -use Magento\Framework\HTTP\PhpEnvironment\Response; -use Magento\Framework\UrlInterface; -class CatalogLastPageRedirect implements ObserverInterface +class CatalogLastPageRedirect extends AbstractProductNavigationRequestObserver { - /** - * @var Config - */ - protected $config; - - /** - * @var CurrentContext - */ - protected $context; - - /** - * @var UrlInterface - */ - protected $urlBuilder; - - /** - * CatalogLastPageRedirect constructor. - * - * @param Config $config - * @param CurrentContext $context - * @param UrlInterface $urlBuilder - */ - public function __construct(Config $config, CurrentContext $context, UrlInterface $urlBuilder) - { - $this->config = $config; - $this->context = $context; - $this->urlBuilder = $urlBuilder; - } - - - /** - * @param mixed $controller - * @return Response|null - */ - protected function getHttpResponse($controller) - { - if (!$controller instanceof Action) { - return null; - } - - $response = $controller->getResponse(); - if (!$response instanceof Response) { - return null; - } - - return $response; - } - - /** - * @return bool - */ - protected function hasTweakwiseResponse() - { - if (!$this->config->isLayeredEnabled()) { - return false; - } - - if (!$this->context->getContext()->hasResponse()) { - return false; - } - - return true; - } - /** * {@inheritdoc} */ - public function execute(Observer $observer) + protected function _execute(Observer $observer) { - $response = $this->getHttpResponse($observer->getData('controller_action')); - if (!$response) { - return; - } - - if (!$this->hasTweakwiseResponse()) { - return; - } - $properties = $this->context->getResponse()->getProperties(); - if (!$properties->getNrofitems()) { + if (!$properties->getNumberOfItems()) { return; } + $lastPage = $properties->getNumberOfPages(); $page = $properties->getCurrentPage(); if ($page <= $lastPage) { @@ -112,6 +34,6 @@ public function execute(Observer $observer) '_query' => ['p' => $lastPage] ]); - $response->setRedirect($url); + $this->getHttpResponse()->setRedirect($url); } } diff --git a/src/Model/Observer/CatalogSearchRedirect.php b/src/Model/Observer/CatalogSearchRedirect.php new file mode 100644 index 00000000..87ffcbd7 --- /dev/null +++ b/src/Model/Observer/CatalogSearchRedirect.php @@ -0,0 +1,34 @@ +context->getResponse()->getRedirects(); + if (!$redirects) { + return; + } + + + $redirect = current($redirects); + $url = $redirect->getUrl(); + if (strpos($url, 'http') !== 0) { + $url = $this->urlBuilder->getUrl($url); + } + + $this->getHttpResponse()->setRedirect($url); + } +} diff --git a/src/etc/frontend/events.xml b/src/etc/frontend/events.xml index 20c92b6b..fda6bbac 100644 --- a/src/etc/frontend/events.xml +++ b/src/etc/frontend/events.xml @@ -4,6 +4,7 @@ - + +