-
Notifications
You must be signed in to change notification settings - Fork 17
/
ItemCollectionProvider.php
115 lines (98 loc) · 3.34 KB
/
ItemCollectionProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved
*
* @copyright Copyright (c) 2017-2022 Tweakwise.com B.V. (https://www.tweakwise.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
namespace Tweakwise\Magento2Tweakwise\Model\Catalog\Layer;
use Tweakwise\Magento2Tweakwise\Exception\TweakwiseExceptionInterface;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Product\CollectionFactory;
use Tweakwise\Magento2Tweakwise\Model\Client\Request\ProductSearchRequest;
use Tweakwise\Magento2Tweakwise\Model\Config;
use Tweakwise\Magento2TweakwiseExport\Model\Logger;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Layer\ItemCollectionProviderInterface;
class ItemCollectionProvider implements ItemCollectionProviderInterface
{
/**
* @var Config
*/
protected $config;
/**
* @var Logger
*/
protected $log;
/**
* @var CollectionFactory
*/
protected $collectionFactory;
/**
* @var ItemCollectionProviderInterface
*/
protected $originalProvider;
/**
* @var NavigationContext
*/
protected $navigationContext;
/**
* Proxy constructor.
*
* @param Config $config
* @param Logger $log
* @param ItemCollectionProviderInterface $originalProvider
* @param CollectionFactory $collectionFactory
* @param NavigationContext $navigationContext
*/
public function __construct(
Config $config,
Logger $log,
ItemCollectionProviderInterface $originalProvider,
CollectionFactory $collectionFactory,
NavigationContext $navigationContext
) {
$this->config = $config;
$this->log = $log;
$this->collectionFactory = $collectionFactory;
$this->originalProvider = $originalProvider;
$this->navigationContext = $navigationContext;
}
/**
* {@inheritdoc}
*/
public function getCollection(Category $category)
{
if (!$this->config->isLayeredEnabled()) {
if (
!$this->config->isSearchEnabled() ||
!($this->navigationContext->getRequest() instanceof ProductSearchRequest)
) {
return $this->originalProvider->getCollection($category);
}
}
if (
!$this->config->isSearchEnabled() &&
($this->navigationContext->getRequest() instanceof ProductSearchRequest)
) {
return $this->originalProvider->getCollection($category);
}
//no api response
if ($this->config->getTweakwiseExceptionTrown()) {
return $this->originalProvider->getCollection($category);
}
try {
$collection = $this->collectionFactory->create(['navigationContext' => $this->navigationContext]);
if (count($collection->getItems()) < 1) {
$collection = $this->collectionFactory
->create(['navigationContext' => $this->navigationContext->resetPagination()])
;
}
$collection->clear();
return $collection;
} catch (TweakwiseExceptionInterface $e) {
$this->log->critical($e);
$this->config->setTweakwiseExceptionThrown();
return $this->originalProvider->getCollection($category);
}
}
}