Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
VisibilityFilter.php
Go to the documentation of this file.
1 <?php
8 
14 use Magento\Eav\Model\Config as EavConfig;
15 
24 {
28  const VISIBILITY_FILTER_FIELD = 'visibility';
29 
36  const FILTER_BY_JOIN = 'join_filter';
37 
44  const FILTER_BY_WHERE = 'where_filter';
45 
49  private $resourceConnection;
50 
54  private $conditionManager;
55 
59  private $storeManager;
60 
64  private $eavConfig;
65 
72  public function __construct(
73  ResourceConnection $resourceConnection,
74  ConditionManager $conditionManager,
75  StoreManagerInterface $storeManager,
76  EavConfig $eavConfig
77  ) {
78  $this->resourceConnection = $resourceConnection;
79  $this->conditionManager = $conditionManager;
80  $this->storeManager = $storeManager;
81  $this->eavConfig = $eavConfig;
82  }
83 
94  public function apply(Select $select, FilterInterface $filter, $type)
95  {
96  if ($type !== self::FILTER_BY_JOIN && $type !== self::FILTER_BY_WHERE) {
97  throw new \InvalidArgumentException(sprintf('Invalid filter type: %s', $type));
98  }
99 
100  $select = clone $select;
101 
102  $type === self::FILTER_BY_JOIN
103  ? $this->applyFilterByJoin($filter, $select)
104  : $this->applyFilterByWhere($filter, $select);
105 
106  return $select;
107  }
108 
118  private function applyFilterByJoin(FilterInterface $filter, Select $select)
119  {
120  $mainTableAlias = $this->extractTableAliasFromSelect($select);
121 
122  $select->joinInner(
123  ['visibility_filter' => $this->resourceConnection->getTableName('catalog_product_index_eav')],
124  $this->conditionManager->combineQueries(
125  [
126  sprintf('%s.entity_id = visibility_filter.entity_id', $mainTableAlias),
127  $this->conditionManager->generateCondition(
128  'visibility_filter.attribute_id',
129  '=',
130  $this->getVisibilityAttributeId()
131  ),
132  $this->conditionManager->generateCondition(
133  'visibility_filter.value',
134  is_array($filter->getValue()) ? 'in' : '=',
135  $filter->getValue()
136  ),
137  $this->conditionManager->generateCondition(
138  'visibility_filter.store_id',
139  '=',
140  $this->storeManager->getStore()->getId()
141  ),
142  ],
144  ),
145  []
146  );
147  }
148 
158  private function applyFilterByWhere(FilterInterface $filter, Select $select)
159  {
160  $mainTableAlias = $this->extractTableAliasFromSelect($select);
161 
162  $select->where(
163  $this->conditionManager->combineQueries(
164  [
165  $this->conditionManager->generateCondition(
166  sprintf('%s.attribute_id', $mainTableAlias),
167  '=',
168  $this->getVisibilityAttributeId()
169  ),
170  $this->conditionManager->generateCondition(
171  sprintf('%s.value', $mainTableAlias),
172  is_array($filter->getValue()) ? 'in' : '=',
173  $filter->getValue()
174  ),
175  $this->conditionManager->generateCondition(
176  sprintf('%s.store_id', $mainTableAlias),
177  '=',
178  $this->storeManager->getStore()->getId()
179  ),
180  ],
182  )
183  );
184  }
185 
192  private function getVisibilityAttributeId()
193  {
194  $attr = $this->eavConfig->getAttribute(
195  \Magento\Catalog\Model\Product::ENTITY,
196  self::VISIBILITY_FILTER_FIELD
197  );
198 
199  return (int) $attr->getId();
200  }
201 
208  private function extractTableAliasFromSelect(Select $select)
209  {
210  $fromArr = array_filter(
211  $select->getPart(Select::FROM),
212  function ($fromPart) {
213  return $fromPart['joinType'] === Select::FROM;
214  }
215  );
216 
217  return $fromArr ? array_keys($fromArr)[0] : null;
218  }
219 }
apply(Select $select, FilterInterface $filter, $type)
$attr
Definition: text.phtml:8
$storeManager
const FROM
Definition: Select.php:49
$type
Definition: item.phtml:13
const SQL_AND
Definition: Select.php:77
__construct(ResourceConnection $resourceConnection, ConditionManager $conditionManager, StoreManagerInterface $storeManager, EavConfig $eavConfig)