Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
Implemented Tweakwise redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
Freek Gruntjes committed Oct 6, 2017
1 parent 07eaea3 commit 9b503d6
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 85 deletions.
1 change: 0 additions & 1 deletion src/Model/Catalog/Layer/FilterList/Tweakwise.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
107 changes: 107 additions & 0 deletions src/Model/Observer/AbstractProductNavigationRequestObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* Tweakwise & Emico (https://www.tweakwise.com/ & https://www.emico.nl/) - All Rights Reserved
*
* @copyright Copyright (c) 2017-2017 Tweakwise.com B.V. (https://www.tweakwise.com)
* @license Proprietary and confidential, Unauthorized copying of this file, via any medium is strictly prohibited
*/

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;

abstract class AbstractProductNavigationRequestObserver implements ObserverInterface
{
/**
* @var Config
*/
protected $config;

/**
* @var CurrentContext
*/
protected $context;

/**
* @var UrlInterface
*/
protected $urlBuilder;

/**
* @var Action
*/
protected $controller;

/**
* CatalogLastPageRedirect constructor.
*
* @param Config $config
* @param CurrentContext $context
* @param UrlInterface $urlBuilder
* @param Action $action
*/
public function __construct(Config $config, CurrentContext $context, UrlInterface $urlBuilder, Action $action)
{
$this->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);
}
88 changes: 5 additions & 83 deletions src/Model/Observer/CatalogLastPageRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -112,6 +34,6 @@ public function execute(Observer $observer)
'_query' => ['p' => $lastPage]
]);

$response->setRedirect($url);
$this->getHttpResponse()->setRedirect($url);
}
}
34 changes: 34 additions & 0 deletions src/Model/Observer/CatalogSearchRedirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Tweakwise & Emico (https://www.tweakwise.com/ & https://www.emico.nl/) - All Rights Reserved
*
* @copyright Copyright (c) 2017-2017 Tweakwise.com B.V. (https://www.tweakwise.com)
* @license Proprietary and confidential, Unauthorized copying of this file, via any medium is strictly prohibited
*/

namespace Emico\Tweakwise\Model\Observer;

use Magento\Framework\Event\Observer;

class CatalogSearchRedirect extends AbstractProductNavigationRequestObserver
{
/**
* {@inheritdoc}
*/
protected function _execute(Observer $observer)
{
$redirects = $this->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);
}
}
3 changes: 2 additions & 1 deletion src/etc/frontend/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<observer name="emico-tweakwise" instance="Emico\Tweakwise\Model\Observer\CatalogLastPageRedirect" />
</event>
<event name="controller_action_postdispatch_catalogsearch_result_index">
<observer name="emico-tweakwise" instance="Emico\Tweakwise\Model\Observer\CatalogLastPageRedirect" />
<observer name="emico-tweakwise-redirect" instance="Emico\Tweakwise\Model\Observer\CatalogSearchRedirect" />
<observer name="emico-tweakwise-last-page" instance="Emico\Tweakwise\Model\Observer\CatalogLastPageRedirect" />
</event>
</config>

0 comments on commit 9b503d6

Please sign in to comment.