Skip to content

Commit

Permalink
Fixed fallback (#70)
Browse files Browse the repository at this point in the history
* Fixed fallback
  • Loading branch information
ah-net authored Aug 23, 2023
1 parent 18049fd commit 5347a53
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 17 deletions.
13 changes: 13 additions & 0 deletions Block/LayeredNavigation/Navigation/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tweakwise\Magento2Tweakwise\Block\LayeredNavigation\Navigation;

use Tweakwise\Magento2Tweakwise\Exception\ApiException;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Filter\Item;
use Tweakwise\Magento2Tweakwise\Model\Client\Request\ProductSearchRequest;
use Tweakwise\Magento2Tweakwise\Model\Client\Type\FacetType\SettingsType;
Expand Down Expand Up @@ -70,6 +71,18 @@ protected function updateTemplate(CurrentContext $currentContext)
$isSearch = $currentContext->getRequest() instanceof ProductSearchRequest;
$isNavigation = !$isSearch;

if ($this->config->getTweakwiseExceptionTrown()) {
//no api response
return;
}

try {
$currentContext->getResponse();
} catch (ApiException $e) {
//no api response
return;
}

if ($isSearch && $searchEnabled) {
$this->_template = 'Tweakwise_Magento2Tweakwise::layer/state.phtml';
}
Expand Down
5 changes: 5 additions & 0 deletions Model/Catalog/Layer/FilterList/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public function aroundGetFilters(FilterList $subject, Closure $proceed, Layer $l
}
}

//no api responseA
if($this->config->getTweakwiseExceptionTrown()) {
return $proceed($layer);
}

try {
return $this->tweakwiseFilterList->getFilters($layer);
} catch (TweakwiseException $e) {
Expand Down
5 changes: 5 additions & 0 deletions Model/Catalog/Layer/ItemCollectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public function getCollection(Category $category)
}
}

//no api response
if($this->config->getTweakwiseExceptionTrown()) {
return $this->originalProvider->getCollection($category);
}

try {
$collection = $this->collectionFactory->create(['navigationContext' => $this->navigationContext]);

Expand Down
12 changes: 10 additions & 2 deletions Model/Catalog/Layer/NavigationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Tweakwise\Magento2Tweakwise\Model\Catalog\Layer;

use Tweakwise\Magento2Tweakwise\Exception\ApiException;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\NavigationContext\CurrentContext;
use Tweakwise\Magento2Tweakwise\Model\Client;
use Tweakwise\Magento2Tweakwise\Model\Client\Request\ProductNavigationRequest;
Expand Down Expand Up @@ -146,14 +147,21 @@ public function getRequest(): ProductNavigationRequest
/**
* @return ProductNavigationResponse
*/
public function getResponse(): ProductNavigationResponse
public function getResponse(): ProductNavigationResponse|null
{
if (!$this->response) {
$request = $this->getRequest();

$this->initializeRequest($request);

$this->response = $this->client->request($request);
if (!$this->config->getTweakwiseExceptionTrown()) {
try{
$this->response = $this->client->request($request);
}catch (ApiException $e) {
//no api response
throw $e;
}
}
}

return $this->response;
Expand Down
1 change: 1 addition & 0 deletions Model/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ public function request(Request $request, bool $async = false)
try {
return $this->doRequest($request, $async);
} catch (ApiException $e) {
$this->config->setTweakwiseExceptionThrown(true);
$this->log->throwException($e);
} finally {
Profiler::stop('tweakwise::request::' . $request->getPath());
Expand Down
4 changes: 2 additions & 2 deletions Model/Client/EndpointManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class EndpointManager
public const DOWN_PERIOD = 300; // 5 minutes
public const VARIABLE_NAME = '__tw_primary_down_timer';

public const SERVER_URL = 'https://gateway.tweakwisenavigator.net';
public const FALLBACK_SERVER_URL = 'https://gateway.tweakwisenavigator.com';
public const SERVER_URL = 'http://gateway.tweakwisenavigator.net';
public const FALLBACK_SERVER_URL = 'http://gateway.tweakwisenavigator.com';

/**
* @var VariableFactory
Expand Down
5 changes: 5 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public function setTweakwiseExceptionThrown($thrown = true)
return $this;
}

public function getTweakwiseExceptionTrown()
{
return $this->tweakwiseExceptionThrown;
}

/**
* @deprecated
* @see \Tweakwise\Magento2Tweakwise\Model\Client\EndpointManager::getServerUrl()
Expand Down
5 changes: 5 additions & 0 deletions Model/Observer/CatalogLastPageRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public function execute(Observer $observer)
return;
}

if ($this->config->getTweakwiseExceptionTrown()) {
//no api response
return;
}

$properties = $this->context->getResponse()->getProperties();
if (!$properties->getNumberOfItems()) {
return;
Expand Down
14 changes: 13 additions & 1 deletion Model/Observer/CatalogSearchRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Tweakwise\Magento2Tweakwise\Model\Observer;

use Tweakwise\Magento2Tweakwise\Exception\ApiException;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\NavigationContext;
use Tweakwise\Magento2Tweakwise\Model\Config;
use Magento\Framework\App\Action\Action;
Expand Down Expand Up @@ -60,7 +61,18 @@ public function execute(Observer $observer)
return;
}

$redirects = $this->context->getResponse()->getRedirects();
if ($this->config->getTweakwiseExceptionTrown()) {
//no api response
return;
}

try{
$redirects = $this->context->getResponse()->getRedirects();
}catch (ApiException $e) {
//no api response
return;
}

if (!$redirects) {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions Model/Observer/NavigationHtmlOverride.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public function execute(Observer $observer)
return;
}

if ($this->config->getTweakwiseExceptionTrown()) {
return;
}

$searchEnabled = $this->config->isSearchEnabled();
$navigationEnabled = $this->config->isLayeredEnabled();

Expand Down
14 changes: 2 additions & 12 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,28 +274,18 @@
<!-- Tweakwise virtual collection provider types used in overrides of magento collection providers -->
<virtualType name="Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\ItemCollectionProvider\Category" type="Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\ItemCollectionProvider">
<arguments>
<argument name="originalProvider" xsi:type="object">Magento\CatalogSearch\Model\Layer\Category\ItemCollectionProvider</argument>
<argument name="originalProvider" xsi:type="object">elasticsearchLayerCategoryItemCollectionProvider</argument>
<argument name="navigationContext" xsi:type="object">Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\NavigationContext\Category</argument>
</arguments>
</virtualType>
<virtualType name="Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\ItemCollectionProvider\Search" type="Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\ItemCollectionProvider">
<arguments>
<argument name="originalProvider" xsi:type="object">Magento\CatalogSearch\Model\Layer\Search\ItemCollectionProvider</argument>
<argument name="originalProvider" xsi:type="object">elasticsearchLayerSearchItemCollectionProvider</argument>
<argument name="navigationContext" xsi:type="object">Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\NavigationContext\Search</argument>
</arguments>
</virtualType>

<!-- Override virtual types of magento to inject our product collection -->
<virtualType name="Magento\CatalogSearch\Model\Layer\Search\Context">
<arguments>
<argument name="collectionProvider" xsi:type="object">Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\ItemCollectionProvider\Search</argument>
</arguments>
</virtualType>
<virtualType name="Magento\CatalogSearch\Model\Layer\Category\Context">
<arguments>
<argument name="collectionProvider" xsi:type="object">Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\ItemCollectionProvider\Category</argument>
</arguments>
</virtualType>
<virtualType name="Magento\Elasticsearch\Model\Layer\Search\Context">
<arguments>
<argument name="collectionProvider" xsi:type="object">Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\ItemCollectionProvider\Search</argument>
Expand Down

0 comments on commit 5347a53

Please sign in to comment.